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 3/4] fstests: t_getcwd.c, fix a warning related to buffer overflow
Date: Sun, 29 Jan 2023 10:42:32 +0800	[thread overview]
Message-ID: <7c1a98e17fdd0248c7d68302c28bd910883274ab.1674870429.git.anand.jain@oracle.com> (raw)
In-Reply-To: <cover.1674870429.git.anand.jain@oracle.com>

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


  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 ` [PATCH 2/4] fstests: fstest.c, fix compile warnings replace sprintf with snprintf Anand Jain
2023-01-29  2:42 ` Anand Jain [this message]
2023-01-29  2:42 ` [PATCH 4/4] fstests: aiodio_sparse2.c, fix compiler warning buffer overflow 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=7c1a98e17fdd0248c7d68302c28bd910883274ab.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.