All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] fstest: fix compilation warnings
@ 2023-01-29  2:42 Anand Jain
  2023-01-29  2:42 ` [PATCH 1/4] fstests: doio.c, fix missing initialization of -C arg Anand Jain
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Anand Jain @ 2023-01-29  2:42 UTC (permalink / raw)
  To: fstests; +Cc: zlang

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(-)

-- 
2.38.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] fstests: doio.c, fix missing initialization of -C arg
  2023-01-29  2:42 [PATCH 0/4] fstest: fix compilation warnings Anand Jain
@ 2023-01-29  2:42 ` Anand Jain
  2023-01-29  2:42 ` [PATCH 2/4] fstests: fstest.c, fix compile warnings replace sprintf with snprintf Anand Jain
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Anand Jain @ 2023-01-29  2:42 UTC (permalink / raw)
  To: fstests; +Cc: zlang

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);
     |				~~~~~~~~~~

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 ltp/doio.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/ltp/doio.c b/ltp/doio.c
index 83f8cf556325..fd64df0f674e 100644
--- a/ltp/doio.c
+++ b/ltp/doio.c
@@ -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;
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] fstests: fstest.c, fix compile warnings replace sprintf with snprintf
  2023-01-29  2:42 [PATCH 0/4] fstest: fix compilation warnings Anand Jain
  2023-01-29  2:42 ` [PATCH 1/4] fstests: doio.c, fix missing initialization of -C arg Anand Jain
@ 2023-01-29  2:42 ` Anand Jain
  2023-01-29  2:42 ` [PATCH 3/4] fstests: t_getcwd.c, fix a warning related to buffer overflow Anand Jain
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Anand Jain @ 2023-01-29  2:42 UTC (permalink / raw)
  To: fstests; +Cc: zlang

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);

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 src/fstest.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/fstest.c b/src/fstest.c
index e4b9e081144a..4f6dc643dd12 100644
--- a/src/fstest.c
+++ b/src/fstest.c
@@ -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);
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] fstests: t_getcwd.c, fix a warning related to buffer overflow
  2023-01-29  2:42 [PATCH 0/4] fstest: fix compilation warnings Anand Jain
  2023-01-29  2:42 ` [PATCH 1/4] fstests: doio.c, fix missing initialization of -C arg Anand Jain
  2023-01-29  2:42 ` [PATCH 2/4] fstests: fstest.c, fix compile warnings replace sprintf with snprintf Anand Jain
@ 2023-01-29  2:42 ` Anand Jain
  2023-01-29  2:42 ` [PATCH 4/4] fstests: aiodio_sparse2.c, fix compiler warning " Anand Jain
  2023-01-30 20:11 ` [PATCH 0/4] fstest: fix compilation warnings Bill O'Donnell
  4 siblings, 0 replies; 6+ messages in thread
From: Anand Jain @ 2023-01-29  2:42 UTC (permalink / raw)
  To: fstests; +Cc: zlang

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.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 src/t_getcwd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/t_getcwd.c b/src/t_getcwd.c
index 27255bd024e1..e9b5d7feb31f 100644
--- a/src/t_getcwd.c
+++ b/src/t_getcwd.c
@@ -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) {
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] fstests: aiodio_sparse2.c, fix compiler warning buffer overflow
  2023-01-29  2:42 [PATCH 0/4] fstest: fix compilation warnings Anand Jain
                   ` (2 preceding siblings ...)
  2023-01-29  2:42 ` [PATCH 3/4] fstests: t_getcwd.c, fix a warning related to buffer overflow Anand Jain
@ 2023-01-29  2:42 ` Anand Jain
  2023-01-30 20:11 ` [PATCH 0/4] fstest: fix compilation warnings Bill O'Donnell
  4 siblings, 0 replies; 6+ messages in thread
From: Anand Jain @ 2023-01-29  2:42 UTC (permalink / raw)
  To: fstests; +Cc: zlang

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.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 src/aio-dio-regress/aiodio_sparse2.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/aio-dio-regress/aiodio_sparse2.c b/src/aio-dio-regress/aiodio_sparse2.c
index 51ede5bb6c8b..685e3b9dce48 100644
--- a/src/aio-dio-regress/aiodio_sparse2.c
+++ b/src/aio-dio-regress/aiodio_sparse2.c
@@ -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);
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/4] fstest: fix compilation warnings
  2023-01-29  2:42 [PATCH 0/4] fstest: fix compilation warnings Anand Jain
                   ` (3 preceding siblings ...)
  2023-01-29  2:42 ` [PATCH 4/4] fstests: aiodio_sparse2.c, fix compiler warning " Anand Jain
@ 2023-01-30 20:11 ` Bill O'Donnell
  4 siblings, 0 replies; 6+ messages in thread
From: Bill O'Donnell @ 2023-01-30 20:11 UTC (permalink / raw)
  To: Anand Jain; +Cc: fstests, zlang

On Sun, Jan 29, 2023 at 10:42:29AM +0800, Anand Jain wrote:
> 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...
Reviewed-by: Bill O'Donnell <bodonnel@redhat.com>

> 
> 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(-)
> 
> -- 
> 2.38.1
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-01-30 20:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-29  2:42 [PATCH 0/4] fstest: fix compilation warnings Anand Jain
2023-01-29  2:42 ` [PATCH 1/4] fstests: doio.c, fix missing initialization of -C arg Anand Jain
2023-01-29  2:42 ` [PATCH 2/4] fstests: fstest.c, fix compile warnings replace sprintf with snprintf Anand Jain
2023-01-29  2:42 ` [PATCH 3/4] fstests: t_getcwd.c, fix a warning related to buffer overflow Anand Jain
2023-01-29  2:42 ` [PATCH 4/4] fstests: aiodio_sparse2.c, fix compiler warning " Anand Jain
2023-01-30 20:11 ` [PATCH 0/4] fstest: fix compilation warnings Bill O'Donnell

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.