Skip to contentSkip to conversation

fstest: fix compilation warnings

PATCH In review Anand Jain 2 participants 6 messages Jan 29, 2023 – Jan 30, 2023

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

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
author
#1 lore raw

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
index 83f8cf556325..fd64df0f674e 100644
@@ -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
author
#2 lore raw

PATCH 2/4 fstests: fstest.c, fix compile warnings replace sprintf with snprintf

In reply to Anand Jain

Fixes 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
index e4b9e081144a..4f6dc643dd12 100644
@@ -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
author
#3 lore raw

PATCH 3/4 fstests: t_getcwd.c, fix a warning related to buffer overflow

In reply to Anand Jain

GCC 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
index 27255bd024e1..e9b5d7feb31f 100644
@@ -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
author
#4 lore raw

PATCH 4/4 fstests: aiodio_sparse2.c, fix compiler warning buffer overflow

In reply to Anand Jain

The 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
index 51ede5bb6c8b..685e3b9dce48 100644
@@ -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
Reviewed-by on the v1 series
#5 lore raw
In reply to Anand Jain
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 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