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.
This commit is contained in:
Дмитрий Левин 2018-02-07 12:32:57 +00:00
parent 11d1c182ba
commit 85bd0dcea1

View 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);