All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: fstests@vger.kernel.org
Cc: zlang@kernel.org
Subject: [PATCH 2/4] fstests: fstest.c, fix compile warnings replace sprintf with snprintf
Date: Sun, 29 Jan 2023 10:42:31 +0800	[thread overview]
Message-ID: <db18b852d84ded8c360b0ef8c94a16f038e4b795.1674870429.git.anand.jain@oracle.com> (raw)
In-Reply-To: <cover.1674870429.git.anand.jain@oracle.com>

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


  parent reply	other threads:[~2023-01-29  2:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=db18b852d84ded8c360b0ef8c94a16f038e4b795.1674870429.git.anand.jain@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=fstests@vger.kernel.org \
    --cc=zlang@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.