ctdb-protocol: Fix compilation issue with strncpy()
authorMartin Schwenke <martin@meltin.net>
Thu, 26 Jul 2018 01:01:30 +0000 (11:01 +1000)
committerAmitay Isaacs <amitay@samba.org>
Fri, 27 Jul 2018 03:45:20 +0000 (05:45 +0200)
When configured with --picky-developer and using -O3 with gcc 8.1:

../protocol/protocol_util.c: In function ‘ctdb_sock_addr_from_string’:
../protocol/protocol_util.c:282:2: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
  strncpy(s, str, len+1);
  ^~~~~~~~~~~~~~~~~~~~~~
../protocol/protocol_util.c:277:8: note: length computed here
  len = strlen(str);
        ^~~~~~~~~~~

Use strlcpy() instead and check the result.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13545

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/protocol/protocol_util.c

index c75555fa7348118ce9ecafdbc51a2323b2abbaed..1b9a46ef8e2c0b13685f83847b98d73c451d85e5 100644 (file)
@@ -274,13 +274,11 @@ int ctdb_sock_addr_from_string(const char *str,
 
        /* Parse out port number and then IP address */
 
-       len = strlen(str);
+       len = strlcpy(s, str, sizeof(s));
        if (len >= sizeof(s)) {
                return EINVAL;
        }
 
-       strncpy(s, str, len+1);
-
        p = rindex(s, ':');
        if (p == NULL) {
                return EINVAL;