From: "cron2 (Code Review)" cron2 upl=
oaded patch set #6 to the change originally created by flichtenhel=
d. The following approvals got outdated and were remove=
d:
Code-Review+2 by ordexsitnl: Clean up type handling
- Make some type casts explicit. Due to the types used
in our networ=
king API and the netlink APIs respectively
this can't be avoided.<=
br>- In many cases just use correct types from the start, e.g.
where w=
e use constants anyway.
Change-Id: I20205ebd06bbf7cbee8c9be93f399961=
f5b74fcc
Signed-off-by: Frank Lichtenheld <frank@...2641...>
Ac=
ked-by: Antonio Quartulli <antonio@...2726...>
Gerrit URL: https:/=
/gerrit.openvpn.net/c/openvpn/+/1251
Message-Id: <20251007122747.1606=
4-1-gert@...1296...>
URL: https://sourceforge.net/p/openvpn/mailman/m=
essage/59243289/
Signed-off-by: Gert Doering <gert@...1296...>
=
---
M src/openvpn/networking_sitnl.c
1 file changed, 46 insertions(+)=
, 49 deletions(-)git pull ssh://gerrit.openvpn.=
net:29418/openvpn refs/changes/51/1251/6
diff --git a/src/openvpn/n=
etworking_sitnl.c b/src/openvpn/networking_sitnl.c
index 18=
15faf..b3adb16 100644
--- a/src/openvpn/networking_sitnl.c<=
/span>
+++ b/src/openvpn/networking_sitnl.c
@@ -59=
,9 +59,9 @@
_nest; =
\
})
-#define SITNL_NEST_END(_msg, _nest) =
\
- { =
\
- _nes=
t->rta_len =3D (void *)sitnl_nlmsg_tail(_msg) - (void *)_nest; \<=
br>+#define SITNL_NEST_END(_msg=
, _nest) \
+ { =
\
<=
span style=3D"color: hsl(120, 100%, 40%);">+ _nest->rta_len =3D (=
unsigned short)((void *)sitnl_nlmsg_tail(_msg) - (void *)_nest); \ }
/* This function was orig=
inally implemented as a macro, but compiling with
@@ -131,2=
9 +131,24 @@
inet_address_t gw;
};
-#if de=
fined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion&quo=
t;
-#endif-
/** * Helper function used to easily add attributes to a rtnl message
*/
static int
-sitnl_addattr(struct nlmsghdr *n, int maxlen, int t=
ype, const void *data, int alen)
+sitnl_addattr(struct nlmsghdr *n, size_t maxlen, unsigned shor=
t type, const void *data, size_t alen)
{
- int len =3D RTA_LENGTH(alen);
- struct rtattr *rta;
+ size_t len =3D RTA_=
LENGTH(alen);
- if ((int)(NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len)) >=
maxlen)
+ if ((NL=
MSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len)) > maxlen)
=
{
- msg(M_WA=
RN, "%s: rtnl: message exceeded bound of %d", __func__, maxlen);<=
/span>
+ msg(M_WARN, =
"%s: rtnl: message exceeded bound of %zu", __func__, maxlen);
return -EMSGSIZE;
}
- rta =3D sitn=
l_nlmsg_tail(n);
+ =
struct rtattr *rta =3D sitnl_nlmsg_tail(n);
rta->r=
ta_type =3D type;
- =
rta->rta_len =3D len;
+ ASSERT(len <=3D USHRT_MAX);
+ rta->rta_len =3D (unsigned short)len;
=
if (!data)
{@@ -252,11 +247,10 @@
sitnl_send(struct nlmsghdr *payl=
oad, pid_t peer, unsigned int groups, sitnl_parse_reply_cb cb, void *arg_cb)
{
- int len, rem_len, fd, ret, rcv_len;+ int fd, ret;
<=
span> struct sockaddr_nl nladdr;
struct nlmsgerr *=
err;
struct nlmsghdr *h;
- unsigned int seq;
char buf=
[1024 * 16];
struct iovec iov =3D {
=
.iov_base =3D payload,
@@ -275,7 +269,11 @@ nladdr.nl_pid =3D peer;
nladdr.nl_groups =
=3D groups;
- payload->nlmsg_seq =3D seq =3D time(NULL);
+ /* NB: We currently do not verify=
seq and pid on answers.
+ * If we ever want to start with that we probably need to come up<=
/span>
+ * with somethin=
g better than "seconds since epoch"...
+ */
+ payload->nlmsg_seq =3D (uint32_t)time(NULL);=
/* no need to send reply */
@@ -290,16 +288,14 @@
=
return -errno;
}
- ret =3D sitnl_bind(fd, 0);=
- if (ret < 0)+ if (sitnl_bind(fd, 0) &l=
t; 0)
{
msg(M_WARN | M_ERRNO,=
"%s: can't bind rtnl socket", __func__);
=
ret =3D -errno;
goto out;
=
}
=
- ret =3D sendmsg(fd, &nlmsg, 0);
- if (ret < 0)
+ if (sendmsg(fd, &nlmsg, 0) < 0)
msg(M_WARN | M_ERRNO, "%s: rtnl: er=
ror on sendmsg()", __func__);
ret =3D -errno;=
@@ -318,8 +314,8 @@
*/
msg(D_RTNL, "%s: checking for received messages",=
__func__);
iov.iov_len =3D sizeof(buf);
- rcv_len =3D recvmsg(fd,=
&nlmsg, 0);
- =
msg(D_RTNL, "%s: rtnl: received %d bytes", __func__, rcv_len);=
+ ssize_t rcv=
_len =3D recvmsg(fd, &nlmsg, 0);
+ msg(D_RTNL, "%s: rtnl: received %zd bytes&quo=
t;, __func__, rcv_len);
if (rcv_len < 0)=
{
if ((errno =3D=3D EINTR) =
|| (errno =3D=3D EAGAIN))
@@ -350,8 +346,8 @@
while (r=
cv_len >=3D (int)sizeof(*h))
{
- len =3D h->nlmsg_len;=
span>
- rem_len =3D=
len - sizeof(*h);
+ =
uint32_t len =3D h->nlmsg_len;
+ ssize_t rem_len =3D len - sizeof(*h);<=
/span>
if ((rem_len < 0) || (len=
> rcv_len))
{
@@ -361,7 +3=
57,7 @@
ret =3D -EIO;
=
goto out;
}
- msg(M_WARN, &qu=
ot;%s: malformed message: len=3D%d", __func__, len);
+ msg(M_WARN, "%s:=
malformed message: len=3D%u", __func__, len);
=
ret =3D -EIO;
goto out; }
@@ -388,7 +384,7 @@
=
if (h->nlmsg_type =3D=3D NLMSG_ERROR)
=
{
err =3D (struct nlmsgerr *)NLMSG=
_DATA(h);
- =
if (rem_len < (int)sizeof(struct nlmsgerr))
+ if (rem_len < sizeof(stru=
ct nlmsgerr))
{
=
msg(M_WARN, "%s: ERROR truncated", __func__); ret =3D -EIO;
@@ -443,7 +439,7=
@@
if (rcv_len)
{
- =
msg(M_WARN, "%s: rtnl: %d not parsed bytes", __func__, rcv_len)=
;
+ msg(M_=
WARN, "%s: rtnl: %zd not parsed bytes", __func__, rcv_len);
ret =3D -1;
goto out;<=
/span>
}
@@ -456,7 +452,7 @@
typedef struct
{
- int addr_size;
+ size_t addr_size;
=
inet_address_t gw;
char iface[IFNAMSIZ];
<=
span> bool default_only;
@@ -469,7 +465,7 @@
=
route_res_t *res =3D arg;
struct rtmsg *r =
=3D NLMSG_DATA(n);
struct rtattr *rta =3D RTM_RTA(r);<=
/span>
- int len =3D n->=
nlmsg_len - NLMSG_LENGTH(sizeof(*r));
+ size_t len =3D n->nlmsg_len - NLMSG_LENGTH(sizeof(=
*r));
unsigned int table, ifindex =3D 0;
@@ -547,7 +543=
,8 @@
req.n.nlmsg_type =3D RTM_GETROUTE;
- req.r.rtm_family =3D af_family=
;
+ ASSERT(af_fami=
ly <=3D UCHAR_MAX);
+ req.r.rtm_family =3D (unsigned char)af_family;
switch (af_family)
{
@@ -761,7 +758,7 @@
}
=
static int
-sitnl_addr_=
set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family,
=
+sitnl_addr_set(uint16_t cmd, u=
int16_t flags, int ifindex, sa_family_t af_family,
=
const inet_address_t *local, const inet_address_t *remote, int pref=
ixlen)
{
struct sitnl_addr_req req;=
span>
@@ -775,7 +772,8 @@
req.n.nlmsg_flags =
=3D NLM_F_REQUEST | flags;
req.i.ifa=
_index =3D ifindex;
- =
req.i.ifa_family =3D af_family;
+ ASSERT(af_family <=3D UINT8_MAX);
+ req.i.ifa_family =3D (uint8_t)af_fam=
ily;
switch (af_family) {
@@ -797,7 +795,8 @@
{
prefixlen =3D size * 8;
}- req.i.ifa_prefixlen =3D pr=
efixlen;
+ ASSERT(=
prefixlen <=3D UINT8_MAX);
+ req.i.ifa_prefixlen =3D (uint8_t)prefixlen;
=
if (remote)
{
@@=
-890,9 +889,9 @@
}
sta=
tic int
-sitnl_route_se=
t(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, const void *=
dst,
+sitnl_route_set=
(uint16_t cmd, uint16_t flags, int ifindex, sa_family_t af_family, const vo=
id *dst,
int prefixlen, const void *gw, en=
um rt_class_t table, int metric,
- enum rt_scope_t scope, int protocol, int type)=
span>
+ enum =
rt_scope_t scope, unsigned char protocol, unsigned char type)
struct sitnl_route_req req;
=
int ret =3D -1, size;
@@ -917,15 +916,17 @@
req.n.nlmsg_flags =3D=
NLM_F_REQUEST | flags;
- req.r.rtm_family =3D af_family;
- req.r.rtm_scope =3D scope;
=
+ ASSERT(af_family <=3D U=
CHAR_MAX);
+ req.r=
.rtm_family =3D (unsigned char)af_family;
+ req.r.rtm_scope =3D (unsigned char)scope;<=
br> req.r.rtm_protocol =3D protocol;
req.r.r=
tm_type =3D type;
- =
req.r.rtm_dst_len =3D prefixlen;
+ ASSERT(prefixlen >=3D 0 && prefixlen <=3D UC=
HAR_MAX);
+ req.r.=
rtm_dst_len =3D (unsigned char)prefixlen;
- if (table < 256)
{
=
- req.r.rtm_table =3D table;
+ req.r.rtm_table =3D (unsigned char)table;
}
else
{@@ -1348,7 +1349,7 @@
}
static int
-sitnl_=
parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta, int le=
n,
+sitnl_parse_rtatt=
r_flags(struct rtattr *tb[], size_t max, struct rtattr *rta, size_t len,
unsigned short flags)
unsigned short type;
@@ -1369,14=
+1370,14 @@
if (len)
- msg(D=
_ROUTE, "%s: %d bytes not parsed! (rta_len=3D%d)", __func__, len,=
rta->rta_len);
+ =
msg(D_ROUTE, "%s: %zu bytes not parsed! (rta_len=3D%u)", _=
_func__, len, rta->rta_len);
}
return 0;
}
static int
-sit=
nl_parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)<=
/span>
+sitnl_parse_rtattr(s=
truct rtattr *tb[], size_t max, struct rtattr *rta, size_t len)
<=
span> {
return sitnl_parse_rtattr_flags(tb, max, rta, =
len, 0);
}
@@ -1474,10 +1475,6 @@ return sitnl_send(&req.n, 0, 0, NULL, NULL);
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
#endif /* !ENABLE_SITNL */
=
#endif /* TARGET_LINUX */
To view, visit change 1251. To unsubscribe, or for help writing mail filt= ers, visit setti= ngs.