avoid using inet_ntoa() and inet_aton()
cover avoid using inet_ntoa() and inet_aton()
some static checkers, like rpminspect, complain about use of "forbidden" functions inet_aton() and inet_ntoa(). Use inet_pton() and inet_ntop() instead.
Davide Caratti (2):
avoid using inet_ntoa()
avoid using inet_aton()
ell/acd.c | 5 +++-- ell/dhcp-lease.c | 3 ++- ell/dhcp-server.c | 25 ++++++++++++++----------- ell/dhcp.c | 17 +++++++++++++++-- ell/rtnl.c | 7 +++++-- examples/https-server-test.c | 4 +++- 6 files changed, 42 insertions(+), 19 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/2 avoid using inet_ntoa()
use inet_ntop() and a temporary buffer, like it's done elsewhere for IPv6.
--- ell/acd.c | 5 +++-- ell/dhcp-lease.c | 3 ++- ell/dhcp-server.c | 11 +++++++---- ell/dhcp.c | 17 +++++++++++++++-- ell/rtnl.c | 7 +++++-- examples/https-server-test.c | 4 +++- 6 files changed, 35 insertions(+), 12 deletions(-)
ell/acd.c +3 -2
@@ -57,10 +57,11 @@#define IP_STR (uint_ip) \ ({ \ + char _buf[INET_ADDRSTRLEN]; \ struct in_addr _in; \ - char *_out; \ + const char *_out; \ _in.s_addr = uint_ip; \ - _out = inet_ntoa(_in); \ + _out = inet_ntop(AF_INET, &_in, _buf, INET_ADDRSTRLEN) ?: "(inv)"; \ _out; \ })
ell/dhcp-lease.c +2 -1
@@ -178,12 +178,13 @@ error:static inline char *get_ip (uint32_t ip) {struct in_addr addr; +char buf[INET_ADDRSTRLEN ];if (ip ==0 )return NULL ; addr.s_addr = ip; -return l_strdup (inet_ntoa (addr)); +return l_strdup (inet_ntop (AF_INET , &addr, buf,INET_ADDRSTRLEN )); } LIB_EXPORT char *l_dhcp_lease_get_address(const struct l_dhcp_lease *lease)
ell/dhcp-server.c +7 -4
@@ -91,10 +91,11 @@ struct l_dhcp_server {#define IP_STR (uint_ip) \ ({ \ + char _buf[INET_ADDRSTRLEN]; \ struct in_addr _in; \ - char *_out; \ + const char *_out; \ _in.s_addr = uint_ip; \ - _out = inet_ntoa(_in); \ + _out = inet_ntop(AF_INET, &_in, _buf, INET_ADDRSTRLEN) ?: "(inv)"; \ _out; \ })
@@ -750,6 +751,7 @@ LIB_EXPORT void l_dhcp_server_destroy(struct l_dhcp_server *server) LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server) { +char buf[INET_ADDRSTRLEN ];struct in_addr ia;if (unlikely (!server))
@@ -838,9 +840,10 @@ LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server) ia.s_addr = server->address;/* In case of unit testing we don't want this to be a fatal error */ -if (!l_acd_start (server->acd,inet_ntoa (ia))) { +if (!inet_ntop (AF_INET , &ia, buf,INET_ADDRSTRLEN ) || + !l_acd_start (server->acd, buf)) {SERVER_DEBUG ("Failed to start ACD on %s, continuing without" , -IP_STR (server->address)); +IP_STR (server->address));l_acd_destroy (server->acd); server->acd =NULL ;
ell/dhcp.c +15 -2
@@ -55,6 +55,17 @@#define BITS_PER_LONG (sizeof(unsigned long) * 8) +#define IP_STR (uint_ip) \ +({ \ + char _buf[INET_ADDRSTRLEN]; \ + struct in_addr _in; \ + const char *_out; \ + _in.s_addr = uint_ip; \ + _out = inet_ntop(AF_INET, &_in, _buf, INET_ADDRSTRLEN) ?: "(inv)"; \ + _out; \ +}) + +enum dhcp_state {DHCP_STATE_INIT ,DHCP_STATE_SELECTING ,
@@ -778,6 +789,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata)struct l_dhcp_client *client = userdata;const struct dhcp_message *message = data;struct dhcp_message_iter iter; +char buf[INET_ADDRSTRLEN ];uint8_t msg_type =0 ;uint8_t t, l;const void *v;
@@ -911,9 +923,10 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata) ia.s_addr = client->lease->address;/* For unit testing we don't want this to be a fatal error */ -if (!l_acd_start (client->acd,inet_ntoa (ia))) { +if (!inet_ntop (AF_INET , &ia, buf,INET_ADDRSTRLEN ) || + !l_acd_start (client->acd, buf)) {CLIENT_DEBUG ("Failed to start ACD on %s, continuing" , -inet_ntoa (ia)); +IP_STR (ia.s_addr));l_acd_destroy (client->acd); client->acd =NULL ; }
ell/rtnl.c +5 -2
@@ -752,6 +752,7 @@ LIB_EXPORT uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex, LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,char **label,char **ip,char **broadcast) { +char buf[INET_ADDRSTRLEN ];struct in_addr in_addr;struct rtattr *attr;
@@ -763,7 +764,8 @@ LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,break ; in_addr = *((struct in_addr *)RTA_DATA (attr)); - *ip =l_strdup (inet_ntoa (in_addr)); + *ip =l_strdup (inet_ntop (AF_INET , &in_addr, buf, +INET_ADDRSTRLEN ));break ;case IFA_BROADCAST :
@@ -771,7 +773,8 @@ LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,break ; in_addr = *((struct in_addr *)RTA_DATA (attr)); - *broadcast =l_strdup (inet_ntoa (in_addr)); + *broadcast =l_strdup (inet_ntop (AF_INET , &in_addr, buf, +INET_ADDRSTRLEN ));break ;case IFA_LABEL :
examples/https-server-test.c +3 -1
@@ -199,12 +199,14 @@ int main(int argc, char *argv[]) https_tls_ready, https_tls_disconnected, NULL);if (getenv ("TLS_DEBUG" )) { +char b[INET_ADDRSTRLEN ];char *str;l_tls_set_debug (tls, https_tls_debug_cb,NULL ,NULL ); str =l_strdup_printf ("/tmp/ell-certchain-%s.pem" , -inet_ntoa (client_addr.sin_addr)); +inet_ntop (AF_INET , &client_addr.sin_addr, + b,INET_ADDRSTRLEN ) ?:"NA" );l_tls_set_cert_dump_path (tls, str);l_free (str); }
Signature · 1 line
2.31.1
PATCH 2/2 avoid using inet_aton()
In reply to Davide Carattiuse inet_pton(AF_INET,...), like it's done elsewhere for IPv6.
--- ell/dhcp-server.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
ell/dhcp-server.c +7 -7
@@ -773,7 +773,7 @@ LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server)/* Assign a default netmask if not already */ if (!server->netmask) { -if (inet_aton ("255.255.255.0" , &ia) <0 ) +if (inet_pton (AF_INET ,"255.255.255.0" , &ia) !=1 )return false; server->netmask = ia.s_addr;
@@ -890,12 +890,12 @@ LIB_EXPORT bool l_dhcp_server_set_ip_range(struct l_dhcp_server *server,if (unlikely (!server || !start_ip || !end_ip))return false; -if (inet_aton (start_ip, &_host_addr) ==0 ) +if (inet_pton (AF_INET , start_ip, &_host_addr) !=1 )return false; start =ntohl (_host_addr.s_addr); -if (inet_aton ((const char *) end_ip, &_host_addr) ==0 ) +if (inet_pton (AF_INET , (const char *) end_ip, &_host_addr) !=1 )return false; server->start_ip = start;
@@ -952,7 +952,7 @@ LIB_EXPORT bool l_dhcp_server_set_ip_address(struct l_dhcp_server *server,if (unlikely (!server))return false; -if (inet_aton (ip, &ia) <0 ) +if (inet_pton (AF_INET , ip, &ia) !=1 )return false; server->address = ia.s_addr;
@@ -980,7 +980,7 @@ LIB_EXPORT bool l_dhcp_server_set_netmask(struct l_dhcp_server *server,if (unlikely (!server || !mask))return false; -if (inet_aton (mask, &ia) <0 ) +if (inet_pton (AF_INET , mask, &ia) !=1 )return false; server->netmask = ia.s_addr;
@@ -996,7 +996,7 @@ LIB_EXPORT bool l_dhcp_server_set_gateway(struct l_dhcp_server *server,if (unlikely (!server || !ip))return false; -if (inet_aton (ip, &ia) <0 ) +if (inet_pton (AF_INET , ip, &ia) !=1 )return false; server->gateway = ia.s_addr;
@@ -1017,7 +1017,7 @@ LIB_EXPORT bool l_dhcp_server_set_dns(struct l_dhcp_server *server, char **dns)for (i =0 ; dns[i]; i++) {struct in_addr ia; -if (inet_aton (dns[i], &ia) <0 ) +if (inet_pton (AF_INET , dns[i], &ia) !=1 ) goto failed; dns_list[i] = ia.s_addr;
Signature · 1 line
2.31.1
Denis Kenzior
AppliedRe: [PATCH 2/2] avoid using inet_aton()
Hi Davide,
Quoted 3 lines · Davide Caratti
use
inet_pton(AF_INET,...), like it's done elsewhere for IPv6.Signed-off-by Davide Caratti <[email protected]>
We do not use Signed-off-by here, so I dropped this.
Quoted 4 lines
--- ell/dhcp-server.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
Applied, thanks.
Regards,
-Denis
Re: [PATCH 1/2] avoid using inet_ntoa()
In reply to Davide CarattiHi Davide,
Quoted 10 lines · Davide Caratti
use
inet_ntop()and a temporary buffer, like it's done elsewhere for IPv6.Signed-off-by Davide Caratti <[email protected]>--- ell/acd.c | 5 +++-- ell/dhcp-lease.c | 3 ++- ell/dhcp-server.c | 11 +++++++---- ell/dhcp.c | 17 +++++++++++++++-- ell/rtnl.c | 7 +++++-- examples/https-server-test.c | 4 +++-
Strictly speaking the examples/ change should be in its own patch. See HACKING.
Quoted 66 lines
6 files changed, 35 insertions(+), 12 deletions(-)ell/acd.c +3
-2@@ -57,10 +57,11 @@#define IP_STR (uint_ip) \ ({ \ + char _buf[INET_ADDRSTRLEN]; \ struct in_addr _in; \ - char *_out; \ + const char *_out; \ _in.s_addr = uint_ip; \ - _out = inet_ntoa(_in); \ + _out = inet_ntop(AF_INET, &_in, _buf, INET_ADDRSTRLEN) ?: "(inv)"; \ _out; \ })ell/dhcp-lease.c +2
-1@@ -178,12 +178,13 @@ error:static inline char *get_ip (uint32_t ip) {struct in_addr addr; +char buf[INET_ADDRSTRLEN ];if (ip ==0 )return NULL ; addr.s_addr = ip; -return l_strdup (inet_ntoa (addr)); +return l_strdup (inet_ntop (AF_INET , &addr, buf,INET_ADDRSTRLEN )); } LIB_EXPORT char *l_dhcp_lease_get_address(const struct l_dhcp_lease *lease)ell/dhcp-server.c +4
-2@@ -91,10 +91,11 @@ struct l_dhcp_server {#define IP_STR (uint_ip) \ ({ \ + char _buf[INET_ADDRSTRLEN]; \ struct in_addr _in; \ - char *_out; \ + const char *_out; \ _in.s_addr = uint_ip; \ - _out = inet_ntoa(_in); \ + _out = inet_ntop(AF_INET, &_in, _buf, INET_ADDRSTRLEN) ?: "(inv)"; \ _out; \ })@@ -750,6 +751,7 @@ LIB_EXPORT void l_dhcp_server_destroy(struct l_dhcp_server *server) LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server) { +char buf[INET_ADDRSTRLEN ];struct in_addr ia;if (unlikely (!server))
ell/dhcp-server.c LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server) · line 844
Quoted 6 lines
ell/dhcp-server.c +2
-1@@ -838,9 +840,10 @@ LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server) ia.s_addr = server->address;/* In case of unit testing we don't want this to be a fatal error */ -if (!l_acd_start (server->acd,inet_ntoa (ia))) { +if (!inet_ntop (AF_INET , &ia, buf,INET_ADDRSTRLEN ) || + !l_acd_start (server->acd, buf)) {
Note that we use only tabs for indentation... See doc/coding-style.txt for details
ell/dhcp-server.c LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server) · line 846
Quoted 3 lines
ell/dhcp-server.c +1
-1SERVER_DEBUG ("Failed to start ACD on %s, continuing without" , -IP_STR (server->address)); +IP_STR (server->address));
as above
Quoted 12 lines
ell/dhcp-server.c +0
-0l_acd_destroy (server->acd); server->acd =NULL ;ell/dhcp.c +1
-0@@ -55,6 +55,17 @@#define BITS_PER_LONG (sizeof(unsigned long) * 8) +#define IP_STR (uint_ip) \
ell/dhcp.c line 68
Quoted 10 lines
ell/dhcp.c +10
-0+({ \ +char _buf[INET_ADDRSTRLEN ]; \ +struct in_addr _in; \ +const char *_out; \ + _in.s_addr = uint_ip; \ + _out =inet_ntop (AF_INET , &_in, _buf,INET_ADDRSTRLEN ) ?:"(inv)" ; \ + _out; \ +}) + +
no double empty lines please
Quoted 12 lines
ell/dhcp.c +1
-0enum dhcp_state {DHCP_STATE_INIT ,DHCP_STATE_SELECTING ,@@ -778,6 +789,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata)struct l_dhcp_client *client = userdata;const struct dhcp_message *message = data;struct dhcp_message_iter iter; +char buf[INET_ADDRSTRLEN ];uint8_t msg_type =0 ;uint8_t t, l;const void *v;
ell/dhcp.c static void dhcp_client_rx_message(const void *data, size_t len, void *userdata) · line 929
Quoted 9 lines
ell/dhcp.c +3
-2@@ -911,9 +923,10 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata) ia.s_addr = client->lease->address;/* For unit testing we don't want this to be a fatal error */ -if (!l_acd_start (client->acd,inet_ntoa (ia))) { +if (!inet_ntop (AF_INET , &ia, buf,INET_ADDRSTRLEN ) || + !l_acd_start (client->acd, buf)) {CLIENT_DEBUG ("Failed to start ACD on %s, continuing" , -inet_ntoa (ia)); +IP_STR (ia.s_addr));
Not sure introducing IP_STR macro just for this is worth it, but ok...
same here for indentation
Quoted 41 lines
ell/dhcp.c +0
-0l_acd_destroy (client->acd); client->acd =NULL ; }ell/rtnl.c +5
-2@@ -752,6 +752,7 @@ LIB_EXPORT uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex, LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,char **label,char **ip,char **broadcast) { +char buf[INET_ADDRSTRLEN ];struct in_addr in_addr;struct rtattr *attr;@@ -763,7 +764,8 @@ LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,break ; in_addr = *((struct in_addr *)RTA_DATA (attr)); - *ip =l_strdup (inet_ntoa (in_addr)); + *ip =l_strdup (inet_ntop (AF_INET , &in_addr, buf, + INET_ADDRSTRLEN)); >break ;case IFA_BROADCAST :@@ -771,7 +773,8 @@ LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,break ; in_addr = *((struct in_addr *)RTA_DATA (attr)); - *broadcast =l_strdup (inet_ntoa (in_addr)); + *broadcast =l_strdup (inet_ntop (AF_INET , &in_addr, buf, +INET_ADDRSTRLEN ));break ;case IFA_LABEL :examples/https-server-test.c +0
-0@@ -199,12 +199,14 @@ int main(int argc, char *argv[]) https_tls_ready, https_tls_disconnected, NULL);
examples/https-server-test.c int main(int argc, char *argv[]) · line 209
Quoted 10 lines
examples/https-server-test.c +3
-1if (getenv ("TLS_DEBUG" )) { +char b[INET_ADDRSTRLEN ];char *str;l_tls_set_debug (tls, https_tls_debug_cb,NULL ,NULL ); str =l_strdup_printf ("/tmp/ell-certchain-%s.pem" , -inet_ntoa (client_addr.sin_addr)); +inet_ntop (AF_INET , &client_addr.sin_addr, + b,INET_ADDRSTRLEN ) ?:"NA" );
Looks like indentation problem here too. Maybe you want to put the inet_ntop business outside of l_strdup_printf call to make things more readable?
examples/https-server-test.c int main(int argc, char *argv[]) · line 212
Quoted 4 lines
examples/https-server-test.c +0
-0l_tls_set_cert_dump_path (tls, str);l_free (str); }
Regards,
-Denis
Re: [PATCH 1/2] avoid using inet_ntoa()
hello Denis, thanks for reviewing!
Il giorno ven 4 giu 2021 alle ore 18:01 Denis Kenzior <[email protected]> ha scritto:
Quoted 2 lines
Hi Davide,
[...]
Quoted 3 lines
Strictly speaking the examples/ change should be in its own patch. See HACKING.
I should have read it, as well as the coding style rules, before submitting _ sorry for not doing that.
I will address coding style and remove SOB line in the v2.
[...]
Quoted 10 lines
(quoted diff) +0
-0@@ -911,9 +923,10 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata) > ia.s_addr = client->lease->address;Quoted 2 lines
/* For unit testing we don't want this to be a fatal error*/
ell/dhcp.c static void dhcp_client_rx_message(const void *data, size_t len, void *userdata)
Quoted 4 lines
ell/dhcp.c +2
-1-if (!l_acd_start (client->acd,inet_ntoa (ia))) { +if (!inet_ntop (AF_INET , &ia, buf,INET_ADDRSTRLEN ) || + !l_acd_start (client->acd, buf)) { CLIENT_DEBUG("Failed to start ACD on %s,
Quoted 6 lines
continuing",
ell/dhcp.c static void dhcp_client_rx_message(const void *data, size_t len, void *userdata) · line 929
Quoted 2 lines
ell/dhcp.c +1
-1- inet_ntoa(ia)); + IP_STR(ia.s_addr));Not sure introducing
IP_STRmacro just for this is worth it, but ok...
that might be useful for other future CLIENT_DEBUG() users.
[...]
Quoted 4 lines
Looks like indentation problem here too. Maybe you want to put the
inet_ntop
business outside ofl_strdup_printfcall to make things more readable?
yes, it seems to look better - I will also fix this.
By the way, looking at the inet_ntop() documentation [1], I think that I can also avoid things like
inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN) ? : "(inv)";
because the first argument is hardcoded to AF_INET and also we are sure that 'buf' is INET_ADDRSTRLEN long.
Under these conditions, the return value of inet_ntop() should always be 'buf'. Correct?
--
davide
[1] https://man7.org/linux/man-pages/man3/inet_ntop.3.html
RispondiInoltra
Re: [PATCH 1/2] avoid using inet_ntoa()
Hi Davide,
Quoted 2 lines
that might be useful for other future CLIENT_DEBUG() users.
Fair enough, Wish there was a printf extension for ipv4 and ipv6 addresses like printk uses... Oh well.
Quoted 11 lines
yes, it seems to look better - I will also fix this.
By the way, looking at theinet_ntop()documentation [1], I think that I can also avoid things likeinet_ntop(AF_INET, &ia, buf,
INET_ADDRSTRLEN) ? : "(inv)";because the first argument is hardcoded to
AF_INETand also we are sure that 'buf' isINET_ADDRSTRLENlong.
Under these conditions, the return value ofinet_ntop()should always be 'buf'. Correct?
I would assume so. You can tell for sure by looking at glibc implementation for example.
Oh, one other thing. Have you checked the scope rules for GCC statement expressions? inet_ntoa uses a static buffer (which glibc further enhances by marking it as thread-specific storage). So the returned pointer is guaranteed to be valid after inet_ntoa returns. The statement expression doesn't seem to do that, so that seems suspicious?
Regards,
-Denis
Re: [PATCH 1/2] avoid using inet_ntoa()
Il giorno lun 7 giu 2021 alle ore 20:41 Denis Kenzior <[email protected]> ha scritto:
Quoted 9 lines
I would assume so. You can tell for sure by looking at glibc implementation for example.
Oh, one other thing. Have you checked the scope rules for GCC statement expressions?
inet_ntoauses a static buffer (which glibc further enhances by marking it as thread-specific storage). So the returned pointer is guaranteed to be valid afterinet_ntoareturns. The statement expression doesn't seem to do that, so that seems suspicious?
... you are right. GCC documentation [1] says:
"In a statement expression, any temporaries created within a statement are destroyed at that statement’s end."
so, we can't do IP_STR() that way. And using inet_ntop() + static buffer, like it's done by iproute2 [2] would actually silence the rpminspect warning I'm seeing, but not fix the actual
problem: AFAIK use of a static buffer is one of the 2 reasons why inet_ntoa() has been "obsoleted".
At this point, probably the cleanest thing to do is to replace IP_STR() with a proper call to inet_ntop(), preceded by declaration of buf[INET_ADDRSTRLEN] (like it's already done in some hunks of this patch). Luckily, it's just 10 places (versus 6 users of inet_ntoa(), still looks feasible).
Il giorno lun 7 giu 2021 alle ore 20:41 Denis Kenzior <[email protected]> ha scritto:
Quoted 6 lines
Quoted 3 lines
that might be useful for other future
CLIENT_DEBUG()users.Fair enough, Wish there was a printf extension for ipv4 and ipv6 addresses like printk uses... Oh well.
yes, that would really make this game less boring :)
[1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
[2] https://github.com/shemminger/iproute2/blob/main/lib/utils.c#L980-L1020
Re: [PATCH 1/2] avoid using inet_ntoa()
Hi Davide,
Quoted 13 lines · d. caratti
Il giorno lun 7 giu 2021 alle ore 20:41 Denis Kenzior <[email protected]> ha scritto:
Quoted 9 lines
I would assume so. You can tell for sure by looking at glibc implementation for example.
Oh, one other thing. Have you checked the scope rules for GCC statement expressions?
inet_ntoauses a static buffer (which glibc further enhances by marking it as thread-specific storage). So the returned pointer is guaranteed to be valid afterinet_ntoareturns. The statement expression doesn't seem to do that, so that seems suspicious?
Quoted 5 lines
... you are right. GCC documentation [1] says:
"In a statement expression, any temporaries created within a statement are destroyed at that statement’s end."
That is what I was afraid of.
Quoted 5 lines
so, we can't do
IP_STR()that way. And usinginet_ntop()+ static buffer, like it's done by iproute2 [2] would actually silence the rpminspect warning I'm seeing, but not fix the actual
problem: AFAIK use of a static buffer is one of the 2 reasons whyinet_ntoa()has been "obsoleted".
That would be unfortunate. I'm really not sure whether this needs to be 'fixed' anyway. ell is meant for single-threaded, event driven apps. So our use of inet_ntoa is just fine. In fact we do similar stuff all over the place.
Quoted 6 lines
At this point, probably the cleanest thing to do is to replace
IP_STR()with a proper call toinet_ntop(), preceded by declaration of buf[INET_ADDRSTRLEN] (like it's already done in some hunks of this patch). Luckily, it's just 10 places (versus 6 users ofinet_ntoa(), still looks feasible).
Perhaps we can do something similar to MAC_STR instead?
Quoted 7 lines
Il giorno lun 7 giu 2021 alle ore 20:41 Denis Kenzior <[email protected]> ha scritto:
Quoted 4 lines
Quoted 3 lines
that might be useful for other future
CLIENT_DEBUG()users.
Quoted 8 lines
Quoted 2 lines
Fair enough, Wish there was a printf extension for ipv4 and ipv6 addresses like printk uses... Oh well.
yes, that would really make this game less boring :)
[1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
[2] https://github.com/shemminger/iproute2/blob/main/lib/utils.c#L980-L1020
Yep, that's the other thing we can do. Just re-implement inet_ntoa or make our own version that covers v4/v6 with a static buffer. Not sure that is any better than just using inet_ntoa though ;)
Regards,
-Denis
Re: [PATCH 1/2] avoid using inet_ntoa()
Il giorno mar 8 giu 2021 alle ore 18:22 Denis Kenzior <[email protected]> ha scritto:
Quoted 10 lines
Hi Davide,
Quoted 6 lines · d. caratti
Il giorno lun 7 giu 2021 alle ore 20:41 Denis Kenzior <[email protected]> ha scritto:
Quoted 3 lines
I would assume so. You can tell for sure by looking at glibc implementation for example.
this:
yes, the ternary operator is not needed as long as the address family is hardcoded to AF_INET and 'buf ' fits the size of 'tmp' in inet_ntop4().
Quoted 20 lines
Quoted 11 lines
Quoted 5 lines
Oh, one other thing. Have you checked the scope rules for GCC statement expressions?
inet_ntoauses a static buffer (which glibc further enhances by marking it as thread-specific storage). So the returned pointer is guaranteed to be valid afterinet_ntoareturns. The statement expression doesn't seem to do that, so that seems suspicious?... you are right. GCC documentation [1] says:
"In a statement expression, any temporaries created within a statement are destroyed at that statement’s end."
That is what I was afraid of.
Quoted 5 lines
so, we can't do
IP_STR()that way. And usinginet_ntop()+ static buffer, like it's done by iproute2 [2] would actually silence the rpminspect warning I'm seeing, but not fix the actual
problem: AFAIK use of a static buffer is one of the 2 reasons whyinet_ntoa()has been "obsoleted".
Quoted 3 lines
That would be unfortunate. I'm really not sure whether this needs to be 'fixed' anyway. ell is meant for single-threaded, event driven apps. So our use of
inet_ntoais just fine. In fact we do similar stuff all over the place.
there's nothing wrong with using inet_ntoa() as long as the caller doesn't need to be "IPv6 friendly" and concurrency is not an issue. However,
this would result in disabling the whole fedora "badfuncs" CI, that now is failing just because of inet_ntoa().
Quoted 7 lines
Quoted 5 lines
At this point, probably the cleanest thing to do is to replace
IP_STR()with a proper call toinet_ntop(), preceded by declaration of buf[INET_ADDRSTRLEN] (like it's already done in some hunks of this patch). Luckily, it's just 10 places (versus 6 users ofinet_ntoa(), still looks feasible).Perhaps we can do something similar to
MAC_STRinstead?
that's similar to the NIPQUAD / NIPQUAD_FMT thing that was done in Linux before the %pI4 format specified was introduced in printk (
https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/commit/?id=3685f25de1b0447fff381c420de
) :
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define IP "%u.%u.%u.%u"
#define IP_STR(uint_ip) ((unsigned char *) &uint_ip)[0], \
((unsigned char *) &uint_ip)[1], \
((unsigned char *) &uint_ip)[2], \
((unsigned char *) &uint_ip)[3]int main(int argc, const char *argv[])
{
struct in_addr ia = { .s_addr = 0x010200c0 };
char buf[INET_ADDRSTRLEN];
printf("test 0 %s\n", inet_ntoa(ia));
printf("test 1 %s\n", inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN));
printf("test 2 " IP "\n", IP_STR(ia.s_addr));
return 0;
}Quoted 11 lines
Quoted 11 lines
Il giorno lun 7 giu 2021 alle ore 20:41 Denis Kenzior <[email protected]> ha scritto:
Quoted 6 lines
Quoted 3 lines
that might be useful for other future
CLIENT_DEBUG()users.Fair enough, Wish there was a printf extension for ipv4 and ipv6 addresses like printk uses... Oh well.
yes, that would really make this game less boring :)
Quoted 7 lines
Quoted 3 lines
[1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
[2] https://github.com/shemminger/iproute2/blob/main/lib/utils.c#L980-L1020Yep, that's the other thing we can do. Just re-implement
inet_ntoaor make our own version that covers v4/v6 with a static buffer. Not sure that is any better than just usinginet_ntoathough ;)
what about converting inet_ntoa() into inet_ntop(), and re-writing IP_STR() as above?
thanks!
Signature · 1 line
davide
Re: [PATCH 1/2] avoid using inet_ntoa()
Hi Davide,
Quoted 10 lines
Quoted 3 lines
That would be unfortunate. I'm really not sure whether this needs to be 'fixed' anyway. ell is meant for single-threaded, event driven apps. So our use of
inet_ntoais just fine. In fact we do similar stuff all over the place.there's nothing wrong with using
inet_ntoa()as long as the caller doesn't need to be "IPv6 friendly" and concurrency is not an issue. However,
this would result in disabling the whole fedora "badfuncs" CI, that now is failing just because ofinet_ntoa().
Is this a worthy goal in and of itself though? What other 'badfuncs' are there?
Quoted 24 lines
Quoted 7 lines
Quoted 5 lines
At this point, probably the cleanest thing to do is to replace
IP_STR()with a proper call toinet_ntop(), preceded by declaration of buf[INET_ADDRSTRLEN] (like it's already done in some hunks of this patch). Luckily, it's just 10 places (versus 6 users ofinet_ntoa(), still looks feasible).Perhaps we can do something similar to
MAC_STRinstead?that's similar to the NIPQUAD /
NIPQUAD_FMTthing that was done in Linux before the %pI4 format specified was introduced in printk (
https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/commit/?id=3685f25de1b0447fff381c420de
) :#include <
stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>#define IP "%u.%u.%u.%u"
#define IP_STR(uint_ip) ((unsigned char *) &uint_ip)[0], \ ((unsigned char *) &uint_ip)[1], \ ((unsigned char *) &uint_ip)[2], \ ((unsigned char *) &uint_ip)[3]int
main(int argc, const char *argv[])
Quoted 11 lines
{
struct in_addr ia = { .s_addr = 0x010200c0 };
char buf[INET_ADDRSTRLEN];
printf("test 0 %s\n", inet_ntoa(ia));
printf("test 1 %s\n", inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN));
printf("test 2 " IP "\n", IP_STR(ia.s_addr));
return 0;
}I think this or something like this would be quite reasonable to use in / for any debug messages where we print an ip.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Il giorno lun 7 giu 2021 alle ore 20:41 Denis Kenzior <[email protected]> ha scritto:
Quoted 6 lines
Quoted 3 lines
that might be useful for other future
CLIENT_DEBUG()users.Fair enough, Wish there was a printf extension for ipv4 and ipv6 addresses like printk uses... Oh well.
yes, that would really make this game less boring :)
[1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
[2] https://github.com/shemminger/iproute2/blob/main/lib/utils.c#L980-L1020
Quoted 6 lines
Quoted 3 lines
Yep, that's the other thing we can do. Just re-implement
inet_ntoaor make our own version that covers v4/v6 with a static buffer. Not sure that is any better than just usinginet_ntoathough ;)what about converting
inet_ntoa()intoinet_ntop(), and re-writingIP_STR()as above?
I guess that would be OK. We just have to remember to not use inet_ntoa in the future...
Regards,
-Denis