messaging_dgm: avoid GCC snprintf warnings in messaging_dgm_out_create
authorAndrew Bartlett <abartlet@samba.org>
Thu, 9 Feb 2017 01:03:33 +0000 (14:03 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 10 Feb 2017 08:05:31 +0000 (09:05 +0100)
We are trying to put something that (in theory) could be 109 bytes
long, into the sockaddr_un.sun_path field which has a fixed size of
108 bytes. The "in theory" part is that one of the components is a
pid, which although stored as 32 bits is in practice 16 bits, so the
maximum size is not actually hit.

This is all very annoying, because the length is checked anyway and
all this achieves is silencing a warning.

Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Fri Feb 10 09:05:31 CET 2017 on sn-devel-144

source3/lib/messages_dgm.c

index 49b390317e4a202b4c55887d666cdbf62200864b..dff8357f60d99b2b69d12a8cfa3f7864cc95698f 100644 (file)
@@ -212,6 +212,7 @@ static int messaging_dgm_out_create(TALLOC_CTX *mem_ctx,
        struct sockaddr_un addr = { .sun_family = AF_UNIX };
        int ret = ENOMEM;
        int out_pathlen;
+       char addr_buf[sizeof(addr.sun_path) + (3 * sizeof(unsigned) + 2)];
 
        out = talloc(mem_ctx, struct messaging_dgm_out);
        if (out == NULL) {
@@ -224,7 +225,7 @@ static int messaging_dgm_out_create(TALLOC_CTX *mem_ctx,
                .cookie = 1
        };
 
-       out_pathlen = snprintf(addr.sun_path, sizeof(addr.sun_path),
+       out_pathlen = snprintf(addr_buf, sizeof(addr_buf),
                               "%s/%u", ctx->socket_dir.buf, (unsigned)pid);
        if (out_pathlen < 0) {
                goto errno_fail;
@@ -234,6 +235,8 @@ static int messaging_dgm_out_create(TALLOC_CTX *mem_ctx,
                goto fail;
        }
 
+       memcpy(addr.sun_path, addr_buf, out_pathlen + 1);
+
        out->queue = tevent_queue_create(out, addr.sun_path);
        if (out->queue == NULL) {
                ret = ENOMEM;