From: Dmitry V. Levin Date: Wed, 7 Feb 2018 12:32:57 +0000 (+0000) Subject: tests: rewrite net-accept-connect.c without strncpy X-Git-Tag: v4.21~55 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=85bd0dcea1a72ce4f1d4a247682155b0246a3ab9;p=strace tests: rewrite net-accept-connect.c without strncpy gcc8 -Wall -Werror rejects our method of struct sockaddr_un.sun_path initialization because the field lacks __nonstring__ attribute. As we calculate the length of the string being copied anyway, workaround this gcc+glibc bug by changing the code to use this pre-calculated length and get rid of strncpy completely. * tests/net-accept-connect.c (main): Use memcpy to initialize sun_path. --- diff --git a/tests/net-accept-connect.c b/tests/net-accept-connect.c index 4045e056..64a5fcdf 100644 --- a/tests/net-accept-connect.c +++ b/tests/net-accept-connect.c @@ -49,15 +49,16 @@ main(int ac, const char **av) struct sockaddr_un addr = { .sun_family = AF_UNIX, }; - socklen_t len; assert(ac == 2); - assert(strlen(av[1]) > 0); + socklen_t len = strlen(av[1]); + assert(len > 0 && len <= sizeof(addr.sun_path)); - strncpy(addr.sun_path, av[1], sizeof(addr.sun_path)); - len = offsetof(struct sockaddr_un, sun_path) + strlen(av[1]) + 1; - if (len > sizeof(addr)) - len = sizeof(addr); + if (++len > sizeof(addr.sun_path)) + len = sizeof(addr.sun_path); + + memcpy(addr.sun_path, av[1], len); + len += offsetof(struct sockaddr_un, sun_path); unlink(av[1]); close(0);