[PATCH] tipc: fix -Wstringop-truncation warnings

2020-11-12 Thread Kang Wenlin
From: Wenlin Kang 

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 
---
 net/tipc/bearer.c | 5 +
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/net/tipc/bearer.c b/net/tipc/bearer.c
index 650414110452..2241d5a38f7b 100644
--- a/net/tipc/bearer.c
+++ b/net/tipc/bearer.c
@@ -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 */
-- 
2.17.1



[PATCH] ext4: fix -Wstringop-truncation warnings

2020-11-12 Thread Kang Wenlin
From: Wenlin Kang 

The strncpy() function may create a unterminated string,
use strscpy_pad() instead.

This fixes the following warning:

fs/ext4/super.c: In function '__save_error_info':
fs/ext4/super.c:349:2: warning: 'strncpy' specified bound 32 equals destination 
size [-Wstringop-truncation]
  strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
  ^~~
fs/ext4/super.c:353:3: warning: 'strncpy' specified bound 32 equals destination 
size [-Wstringop-truncation]
   strncpy(es->s_first_error_func, func,
   ^
sizeof(es->s_first_error_func));
~~~

Signed-off-by: Wenlin Kang 
---
 fs/ext4/super.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index c3b864588a0b..3ade6e1f7a4d 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -446,7 +446,7 @@ static void __save_error_info(struct super_block *sb, int 
error,
return;
es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
ext4_update_tstamp(es, s_last_error_time);
-   strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
+   strscpy_pad(es->s_last_error_func, func, sizeof(es->s_last_error_func));
es->s_last_error_line = cpu_to_le32(line);
es->s_last_error_ino = cpu_to_le32(ino);
es->s_last_error_block = cpu_to_le64(block);
@@ -507,7 +507,7 @@ static void __save_error_info(struct super_block *sb, int 
error,
if (!es->s_first_error_time) {
es->s_first_error_time = es->s_last_error_time;
es->s_first_error_time_hi = es->s_last_error_time_hi;
-   strncpy(es->s_first_error_func, func,
+   strscpy_pad(es->s_first_error_func, func,
sizeof(es->s_first_error_func));
es->s_first_error_line = cpu_to_le32(line);
es->s_first_error_ino = es->s_last_error_ino;
-- 
2.17.1