]> granicus.if.org Git - strace/commitdiff
tests: rewrite net-accept-connect.c without strncpy
authorDmitry V. Levin <ldv@altlinux.org>
Wed, 7 Feb 2018 12:32:57 +0000 (12:32 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Wed, 7 Feb 2018 12:32:57 +0000 (12:32 +0000)
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.

tests/net-accept-connect.c

index 4045e056f41bf626ab8937b35839782c7eea4351..64a5fcdf1b15e51a3dc87180a3827191b90c1bb4 100644 (file)
@@ -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);