[PATCH 1/4] fsx: fix strncpy usage error

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]



From: Darrick J. Wong <darrick.wong@xxxxxxxxxx>

We shouldn't feed sizeof() to strncpy as the string length.  Just use
snprintf, which at least doesn't have the zero termination problems.

In file included from /usr/include/string.h:495,
                 from ../src/global.h:73,
                 from fsx.c:16:
In function 'strncpy',
    inlined from 'main' at fsx.c:2944:5:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: warning:
'__builtin_strncpy' specified bound 4096 equals destination size
[-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function 'strncpy',
    inlined from 'main' at fsx.c:2914:4:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: warning:
'__builtin_strncpy' specified bound 1024 equals destination size
[-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Darrick J. Wong <darrick.wong@xxxxxxxxxx>
---
 ltp/fsx.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)


diff --git a/ltp/fsx.c b/ltp/fsx.c
index 0abd7de1..cd0bae55 100644
--- a/ltp/fsx.c
+++ b/ltp/fsx.c
@@ -2769,8 +2769,7 @@ main(int argc, char **argv)
 			randomoplen = 0;
 			break;
 		case 'P':
-			strncpy(dname, optarg, sizeof(dname));
-			strcat(dname, "/");
+			snprintf(dname, sizeof(dname), "%s/", optarg);
 			dirpath = strlen(dname);
 			break;
                 case 'R':
@@ -2799,7 +2798,7 @@ main(int argc, char **argv)
 			break;
 		case 255:  /* --record-ops */
 			if (optarg)
-				strncpy(opsfile, optarg, sizeof(opsfile));
+				snprintf(opsfile, sizeof(opsfile), "%s", optarg);
 			recordops = opsfile;
 			break;
 		case 256:  /* --replay-ops */




[Index of Archives]     [Linux Filesystems Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux