f2fs: Replace strncpy with memcpy
PATCH 1/1 f2fs: Replace strncpy with memcpy
gcc 8.1.0 complains:
fs/f2fs/namei.c: In function 'f2fs_update_extension_list':fs/f2fs/namei.c:257:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthfs/f2fs/namei.c:249:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its length
Using strncpy() is indeed less than perfect since the length of data to be copied has already been determined with strlen(). Replace strncpy() with memcpy() to address the warning and optimize the code a little.
--- fs/f2fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/namei.c +2 -2
@@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,return -EINVAL ;if (hot) { -strncpy (extlist[count], name,strlen (name)); +memcpy (extlist[count], name,strlen (name)); sbi->raw_super->hot_ext_count = hot_count +1 ; }else {char buf[F2FS_MAX_EXTENSION ][F2FS_EXTENSION_LEN ];
@@ -254,7 +254,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,memcpy (buf, &extlist[cold_count],F2FS_EXTENSION_LEN * hot_count);memset (extlist[cold_count],0 ,F2FS_EXTENSION_LEN ); -strncpy (extlist[cold_count], name,strlen (name)); +memcpy (extlist[cold_count], name,strlen (name));memcpy (&extlist[cold_count +1 ], buf,F2FS_EXTENSION_LEN * hot_count); sbi->raw_super->extension_count =cpu_to_le32 (cold_count +1 );
Signature · 1 line
2.7.4
Quoted 24 lines · Guenter Roeck
gcc 8.1.0 complains:
fs/f2fs/namei.c: In function 'f2fs_update_extension_list':fs/f2fs/namei.c:257:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthfs/f2fs/namei.c:249:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthUsing
strncpy()is indeed less than perfect since the length of data to be copied has already been determined withstrlen(). Replacestrncpy()withmemcpy()to address the warning and optimize the code a little.Signed-off-by Guenter Roeck <[email protected]>--- fs/f2fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/f2fs/namei.c +0
-0
fs/f2fs/namei.c int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, · line 249
Quoted 5 lines
fs/f2fs/namei.c +1
-1@@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,return -EINVAL ;if (hot) { -strncpy (extlist[count], name,strlen (name)); +memcpy (extlist[count], name,strlen (name));
How about replacing with strcpy(extlist[count], name)? Because name length has already been checked before f2fs_update_extension_list, it should be valid, and will not cause overflow during copying.
Thanks,
Quoted 13 lines
fs/f2fs/namei.c +1
-1sbi->raw_super->hot_ext_count = hot_count +1 ; }else {char buf[F2FS_MAX_EXTENSION ][F2FS_EXTENSION_LEN ];@@ -254,7 +254,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,memcpy (buf, &extlist[cold_count],F2FS_EXTENSION_LEN * hot_count);memset (extlist[cold_count],0 ,F2FS_EXTENSION_LEN ); -strncpy (extlist[cold_count], name,strlen (name)); +memcpy (extlist[cold_count], name,strlen (name));memcpy (&extlist[cold_count +1 ], buf,F2FS_EXTENSION_LEN * hot_count); sbi->raw_super->extension_count =cpu_to_le32 (cold_count +1 );
Quoted 27 lines · Chao Yu
Quoted 26 lines · Guenter Roeck
gcc 8.1.0 complains:
fs/f2fs/namei.c: In function 'f2fs_update_extension_list':fs/f2fs/namei.c:257:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthfs/f2fs/namei.c:249:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthUsing
strncpy()is indeed less than perfect since the length of data to be copied has already been determined withstrlen(). Replacestrncpy()withmemcpy()to address the warning and optimize the code a little.Signed-off-by Guenter Roeck <[email protected]>--- fs/f2fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/f2fs/namei.c +0
-0@@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,return -EINVAL ;
Quoted 8 lines
fs/f2fs/namei.c int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, · line 249
Quoted 3 lines
fs/f2fs/namei.c +1
-1if (hot) { -strncpy (extlist[count], name,strlen (name)); +memcpy (extlist[count], name,strlen (name));How about replacing with
strcpy(extlist[count], name)? Because name length has already been checked beforef2fs_update_extension_list, it should be valid, and will not cause overflow during copying.
Your call; feel free to submit an alternative. Since it is different files, static analysis might not know and complain, though. You might want to make sure that this doesn't happen, and also add a comment explaining the reason for using strcpy().
Thanks,
Guenter
Chao Yu
Reviewed-byQuoted 37 lines · Guenter Roeck
Quoted 27 lines · Chao Yu
Quoted 26 lines · Guenter Roeck
gcc 8.1.0 complains:
fs/f2fs/namei.c: In function 'f2fs_update_extension_list':fs/f2fs/namei.c:257:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthfs/f2fs/namei.c:249:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthUsing
strncpy()is indeed less than perfect since the length of data to be copied has already been determined withstrlen(). Replacestrncpy()withmemcpy()to address the warning and optimize the code a little.Signed-off-by Guenter Roeck <[email protected]>--- fs/f2fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/f2fs/namei.c +0
-0@@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,return -EINVAL ;Quoted 8 lines
fs/f2fs/namei.c int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, · line 249
Quoted 3 lines
fs/f2fs/namei.c +1
-1if (hot) { -strncpy (extlist[count], name,strlen (name)); +memcpy (extlist[count], name,strlen (name));How about replacing with
strcpy(extlist[count], name)? Because name length has already been checked beforef2fs_update_extension_list, it should be valid, and will not cause overflow during copying.
Quoted 3 lines
Your call; feel free to submit an alternative. Since it is different files, static analysis might not know and complain, though. You might want to make sure that this doesn't happen, and also add a comment explaining the reason for using
strcpy().
Yeah, that could be changed in another patch, but it will be trivial. Anyway, to fix this gcc complaint, this patch looks good to me, thanks for the patch. :)
Thanks,
Quoted 5 lines
Thanks,
Guenter
A message that is not in the archive (sent off-list or not yet received): CAD14+f0eRF9y-QNMXwXEymQGOZ_D22sbdqwAuXg5Cv=SXryApQ@mail.gmail.com
Quoted 7 lines · Ju Hyung Park
I must be missing something here.
Shouldn't the third argument be 'strlen(name) + 1', if we were to use
memcpy()?
strcpy()/strncpy() appends null terminator to the destination whereasmemcpy()doesn't.
strncpy() does not append a null character if the passed length is smaller than or equal to the string length. strncpy(dest, "x", strlen("x")) copies exactly one character. The code as written specifically does _not_ append a terminator to the end of the copied string.
Guenter
Quoted 77 lines
Thanks,
Quoted 73 lines · Chao Yu
Quoted 21 lines · Guenter Roeck
Quoted 20 lines · Chao Yu
Quoted 19 lines · Guenter Roeck
gcc 8.1.0 complains:
fs/f2fs/namei.c: In function 'f2fs_update_extension_list':fs/f2fs/namei.c:257:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthfs/f2fs/namei.c:249:3: warning:
'strncpy' output truncated before terminating nul copying as many bytes from a string as its lengthUsing
strncpy()is indeed less than perfect since the length of data to be copied has already been determined withstrlen(). Replacestrncpy()withmemcpy()to address the warning and optimize the code a little.Signed-off-by Guenter Roeck <[email protected]>--- fs/f2fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 5 lines
Quoted 5 lines
fs/f2fs/namei.c int f2fs_update_extension_list(struct
Quoted 5 lines
fs/f2fs/namei.c +0
-0@@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct
f2fs_sb_info*sbi, const char *name,Quoted 7 lines
Quoted 7 lines
fs/f2fs/namei.c int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, · line 249
Quoted 5 lines
fs/f2fs/namei.c +1
-1return -EINVAL ;if (hot) { -strncpy (extlist[count], name,strlen (name)); +memcpy (extlist[count], name,strlen (name));How about replacing with
strcpy(extlist[count], name)? Because namelength has
Quoted 1 line
Quoted 1 line
already been checked before
f2fs_update_extension_list, it should bevalid, and
Quoted 4 lines
Quoted 2 lines
will not cause overflow during copying.
Your call; feel free to submit an alternative. Since it is different
files, static
Quoted 1 line
analysis might not know and complain, though. You might want to make
sure that this
Quoted 1 line
doesn't happen, and also add a comment explaining the reason for using
strcpy().Yeah, that could be changed in another patch, but it will be trivial. Anyway, to
fix this gcc complaint, this patch looks good to me, thanks for the patch. :)Reviewed-by Chao Yu <[email protected]>Thanks,
Quoted 5 lines
Thanks,
Guenter------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Linux-f2fs-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel