tipc: fix -Wstringop-truncation warnings
authorWenlin Kang <wenlin.kang@windriver.com>
Thu, 12 Nov 2020 09:34:42 +0000 (17:34 +0800)
committerJakub Kicinski <kuba@kernel.org>
Fri, 13 Nov 2020 22:17:49 +0000 (14:17 -0800)
Replace strncpy() with strscpy(), fixes the following warning:

In function 'bearer_name_validate',
    inlined from 'tipc_enable_bearer' at net/tipc/bearer.c:246:7:
net/tipc/bearer.c:141:2: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
  strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Wenlin Kang <wenlin.kang@windriver.com>
Acked-by: Ying Xue <ying.xue@windriver.com>
Link: https://lore.kernel.org/r/20201112093442.8132-1-wenlin.kang@windriver.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/tipc/bearer.c

index 6504141..2241d5a 100644 (file)
@@ -139,10 +139,7 @@ static int bearer_name_validate(const char *name,
        u32 if_len;
 
        /* copy bearer name & ensure length is OK */
-       name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
-       /* need above in case non-Posix strncpy() doesn't pad with nulls */
-       strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
-       if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
+       if (strscpy(name_copy, name, TIPC_MAX_BEARER_NAME) < 0)
                return 0;
 
        /* ensure all component parts of bearer name are present */