List for cgit developers and users
 help / color / mirror / Atom feed
* [PATCH 1/2] gcc8.1: fix strncpy bounds warnings
@ 2018-06-12 23:33 andy
  2018-06-12 23:34 ` [PATCH 2/2] gcc8.1: fix strcat warning andy
  2018-06-16 13:04 ` [PATCH 1/2] gcc8.1: fix strncpy bounds warnings john
  0 siblings, 2 replies; 13+ messages in thread
From: andy @ 2018-06-12 23:33 UTC (permalink / raw)


These warnings are coming on default Fedora 28 build and probably others using gcc 8.1

../shared.c: In function ?expand_macro?:
../shared.c:483:3: warning: ?strncpy? specified bound depends on the length of the source argument [-Wstringop-overflow=]
   strncpy(name, value, len);
   ^~~~~~~~~~~~~~~~~~~~~~~~~
../shared.c:480:9: note: length computed here
   len = strlen(value);
         ^~~~~~~~~~~~~

strncpy with a computed length via strlen is usually
not the right thing.

../ui-shared.c: In function ?cgit_repobasename?:
../ui-shared.c:135:2: warning: ?strncpy? specified bound 1024 equals destination size [-Wstringop-truncation]
  strncpy(rvbuf, reponame, sizeof(rvbuf));
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

add one char of padding and adjust so the code does the same.

Signed-off-by: Andy Green <andy at warmcat.com>
---
 shared.c    |    2 +-
 ui-shared.c |    7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/shared.c b/shared.c
index 21ac8f4..477db0a 100644
--- a/shared.c
+++ b/shared.c
@@ -480,7 +480,7 @@ static char *expand_macro(char *name, int maxlength)
 		len = strlen(value);
 		if (len > maxlength)
 			len = maxlength;
-		strncpy(name, value, len);
+		memcpy(name, value, len);
 	}
 	return name + len;
 }
diff --git a/ui-shared.c b/ui-shared.c
index 9d8f66b..6656bd5 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -129,11 +129,12 @@ char *cgit_pageurl(const char *reponame, const char *pagename,
 const char *cgit_repobasename(const char *reponame)
 {
 	/* I assume we don't need to store more than one repo basename */
-	static char rvbuf[1024];
+	static char rvbuf[1025];
 	int p;
 	const char *rv;
-	strncpy(rvbuf, reponame, sizeof(rvbuf));
-	if (rvbuf[sizeof(rvbuf)-1])
+
+	strncpy(rvbuf, reponame, sizeof(rvbuf) - 1);
+	if (rvbuf[sizeof(rvbuf) - 2])
 		die("cgit_repobasename: truncated repository name '%s'", reponame);
 	p = strlen(rvbuf)-1;
 	/* strip trailing slashes */



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

end of thread, other threads:[~2018-06-26 11:36 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-12 23:33 [PATCH 1/2] gcc8.1: fix strncpy bounds warnings andy
2018-06-12 23:34 ` [PATCH 2/2] gcc8.1: fix strcat warning andy
2018-06-16 13:11   ` john
2018-06-16 21:52     ` list
2018-06-17  2:28       ` andy
2018-06-16 13:04 ` [PATCH 1/2] gcc8.1: fix strncpy bounds warnings john
2018-06-16 13:07   ` [PATCH 1/2] shared: allocate return value from expand_macros() john
2018-06-16 13:07     ` [PATCH 2/2] shared: use strbuf for expanding macros john
2018-06-16 13:12   ` [PATCH 1/2] gcc8.1: fix strncpy bounds warnings andy
2018-06-16 14:14     ` john
2018-06-26 11:36   ` [PATCH v2 0/2] " andy
2018-06-26 11:36     ` [PATCH v2 1/2] " andy
2018-06-26 11:36     ` [PATCH v2 2/2] cgit_repobasename: convert to allocated result andy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).