fstest: fix compilation warnings
cover fstest: fix compilation warnings
This patch set addresses several compilation warnings and is based on the for-next.
Anand Jain (4):
fstests: doio.c, fix missing initialization of -C arg
fstests: fstest.c, fix compile warnings replace sprintf with snprintf
fstests: t_getcwd.c, fix a warning related to buffer overflow
fstests: aiodio_sparse2.c, fix compiler warning buffer overflow
ltp/doio.c | 1 + src/aio-dio-regress/aiodio_sparse2.c | 1 + src/fstest.c | 17 +++++++++++++---- src/t_getcwd.c | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-)
Signature · 1 line
2.38.1
PATCH 1/4 fstests: doio.c, fix missing initialization of -C arg
Resolves GCC warnings on null values for %tok.
doio.c: In function 'parse_cmdline':
doio.c:3113:33: warning: '%s' directive argument is null [-Wformat-overflow=]
3113 | fprintf(stderr,
| ^~~~~~~~~~~~~~~
3114 | "%s: Illegal -C arg (%s). Must be one of: ",
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3115 | Prog, tok);
| ~~~~~~~~~~--- ltp/doio.c | 1 + 1 file changed, 1 insertion(+)
ltp/doio.c +1 -0
@@ -3106,6 +3106,7 @@ char *opts;case 'C' : C_opt++; + tok =strtok (optarg,"," );for (s=checkmap; s->string !=NULL ; s++)if (!strcmp (s->string, optarg))break ;
Signature · 1 line
2.38.1
PATCH 2/4 fstests: fstest.c, fix compile warnings replace sprintf with snprintf
In reply to Anand JainFixes the buffer overflow warnings, by using snprintf instead of sprintf.
fstest.c:95:20: warning: '/file' directive writing 5 bytes into a region of size between 1 and 1024 [-Wformat-overflow=]
sprintf(fname, "%s/file%d", dir, fnum);
^~~~~fstest.c:166:20: warning: '/file' directive writing 5 bytes into a region of size between 1 and 1024 [-Wformat-overflow=]
sprintf(fname, "%s/file%d", dir, fnum);
--- src/fstest.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
src/fstest.c +13 -4
@@ -88,11 +88,16 @@ static void check_buffer(uchar *buf, int loop, int child, int fnum, int ofs)static void create_file (const char *dir,int loop,int child,int fnum) {char *buf; -int size, fd; +int size, fd, ret;char fname[1024 ]; buf =x_malloc (block_size); -sprintf (fname,"%s/file%d" , dir, fnum); + ret =snprintf (fname,sizeof (fname),"%s/file%d" , dir, fnum); +if (ret <0 || ret >=sizeof (fname)) { +fprintf (stderr,"file path '%s' too long %d\n" , dir, ret); +exit (1 ); + } + fd =open (fname,O_RDWR |O_CREAT |O_TRUNC | (use_sync?O_SYNC :0 ),0644 );if (fd ==-1 ) {perror (fname);
@@ -158,12 +163,16 @@ bozo!static void check_file (const char *dir,int loop,int child,int fnum) {uchar *buf; -int size, fd; +int size, fd, ret;char fname[1024 ]; buf =x_malloc (block_size); -sprintf (fname,"%s/file%d" , dir, fnum); + ret =snprintf (fname,sizeof (fname),"%s/file%d" , dir, fnum); +if (ret <0 || ret >=sizeof (fname)) { +fprintf (stderr,"file path is '%s' too long %d\n" , dir, ret); +exit (1 ); + } fd =open (fname,O_RDONLY );if (fd ==-1 ) {perror (fname);
Signature · 1 line
2.38.1
PATCH 3/4 fstests: t_getcwd.c, fix a warning related to buffer overflow
In reply to Anand JainGCC reports warning related to 'strncpy' and its specified bound being equal to the destination buffer size.
t_getcwd.c: In function 'do_rename':
t_getcwd.c:40:2: warning: 'strncpy' specified bound 256 equals destination size [-Wstringop-truncation]
strncpy(c_name, prefix, BUF_SIZE);
A buffer overflow is unlikely here because the only caller for do_rename() sends a 16 char long %prefix and BUF_SIZE is defined as 256.
The change is made to reduce the buffer length in order to silence compilation warning.
--- src/t_getcwd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/t_getcwd.c +2 -2
@@ -30,14 +30,14 @@ int test_getcwd(char *init_cwd)return 0 ; } -void do_rename (char *prefix) +static void do_rename (char *prefix) {int i =0 ;int fd;char c_name[BUF_SIZE ];char n_name[BUF_SIZE ]; -strncpy (c_name, prefix,BUF_SIZE ); +strncpy (c_name, prefix,BUF_SIZE -1 ); fd =open (c_name,O_CREAT |O_RDWR ,0644 );if (fd <0 ) {
Signature · 1 line
2.38.1
PATCH 4/4 fstests: aiodio_sparse2.c, fix compiler warning buffer overflow
In reply to Anand JainThe warning is due to 'strncpy' with a maximum number of characters equal to the destination buffer size, without space for null termination.
aiodio_sparse2.c: In function 'main':
aiodio_sparse2.c:404:9: warning: 'strncpy' specified bound 4096 equals destination size [-Wstringop-truncation]
404 | strncpy(filename, argv[argc-1], PATH_MAX);
However, PATH_MAX is including null termination at the end. Anyways, fix warning by setting NULL.
--- src/aio-dio-regress/aiodio_sparse2.c | 1 + 1 file changed, 1 insertion(+)
src/aio-dio-regress/aiodio_sparse2.c +1 -0
@@ -402,6 +402,7 @@ int main(int argc, char **argv) }strncpy (filename, argv[argc-1 ],PATH_MAX ); + filename[PATH_MAX -1 ] ='\0' ;if (alignment ==0 ) alignment =get_logical_block_size (filename);
Signature · 1 line
2.38.1
Quoted 2 lines · Anand Jain
This patch set addresses several compilation warnings and is based on the for-next.
Good idea. The warnings have become pretty distracting. Thanks. For the series...
Quoted 16 lines
Anand Jain (4):
fstests:doio.c, fix missing initialization of -C arg
fstests:fstest.c, fix compile warnings replace sprintf with snprintf
fstests:t_getcwd.c, fix a warning related to buffer overflow
fstests:aiodio_sparse2.c, fix compiler warning buffer overflowltp/doio.c | 1 + src/aio-dio-regress/aiodio_sparse2.c | 1 + src/fstest.c | 17 +++++++++++++---- src/t_getcwd.c | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-)Signature · 1 line
2.38.1