cgit

commit 08a2b1b8f812c6d77489467c8ff120979c297bed

Author: Jason A. Donenfeld <Jason@zx2c4.com>

Fix gcc 8.1.1 compiler warnings

    CC ../shared.o
../shared.c: In function ‘expand_macro’:
../shared.c:487:3: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
   strncpy(name, value, len);
   ^~~~~~~~~~~~~~~~~~~~~~~~~
../shared.c:484:9: note: length computed here
   len = strlen(value);
         ^~~~~~~~~~~~~
../ui-shared.c: In function ‘cgit_repobasename’:
../ui-shared.c:136:2: warning: ‘strncpy’ specified bound 1024 equals destination size [-Wstringop-truncation]
  strncpy(rvbuf, reponame, sizeof(rvbuf));
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CC ../ui-ssdiff.o
../ui-ssdiff.c: In function ‘replace_tabs’:
../ui-ssdiff.c:142:4: warning: ‘strncat’ output truncated copying between 1 and 8 bytes from a string of length 8 [-Wstringop-truncation]
    strncat(result, spaces, 8 - (strlen(result) % 8));
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>

 shared.c | 7 ++++---
 ui-shared.c | 19 ++++++++++++-------
 ui-ssdiff.c | 12 +++++++-----


diff --git a/shared.c b/shared.c
index f7b64cf317ced6a7dd5ce33fce48e74c149c0e5f..609bd2a30670e5eab7974593fd98c6d9e95c24bc 100644
--- a/shared.c
+++ b/shared.c
@@ -476,15 +476,16 @@  */
 static char *expand_macro(char *name, int maxlength)
 {
 	char *value;
-	int len;
+	size_t len;
 
 	len = 0;
 	value = getenv(name);
 	if (value) {
-		len = strlen(value);
+		len = strlen(value) + 1;
 		if (len > maxlength)
 			len = maxlength;
-		strncpy(name, value, len);
+		strlcpy(name, value, len);
+		--len;
 	}
 	return name + len;
 }




diff --git a/ui-shared.c b/ui-shared.c
index 066a470e5dd419b503bd4ff80646028079f844ad..739505a865cc7660621f28666474ec9c6d624718 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -133,20 +133,25 @@ 	/* I assume we don't need to store more than one repo basename */
 	static char rvbuf[1024];
 	int p;
 	const char *rv;
-	strncpy(rvbuf, reponame, sizeof(rvbuf));
-	if (rvbuf[sizeof(rvbuf)-1])
+	size_t len;
+
+	len = strlcpy(rvbuf, reponame, sizeof(rvbuf));
+	if (len >= sizeof(rvbuf))
 		die("cgit_repobasename: truncated repository name '%s'", reponame);
-	p = strlen(rvbuf)-1;
+	p = len - 1;
 	/* strip trailing slashes */
-	while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
+	while (p && rvbuf[p] == '/')
+		rvbuf[p--] = '\0';
 	/* strip trailing .git */
 	if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
-		p -= 3; rvbuf[p--] = 0;
+		p -= 3;
+		rvbuf[p--] = '\0';
 	}
 	/* strip more trailing slashes if any */
-	while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
+	while (p && rvbuf[p] == '/')
+		rvbuf[p--] = '\0';
 	/* find last slash in the remaining string */
-	rv = strrchr(rvbuf,'/');
+	rv = strrchr(rvbuf, '/');
 	if (rv)
 		return ++rv;
 	return rvbuf;




diff --git a/ui-ssdiff.c b/ui-ssdiff.c
index 7f261ed53a0077fa3fd4a8dab3db1fcdcf247a8c..68c2044dcc75c8ab58df847c36e0debe3665acba 100644
--- a/ui-ssdiff.c
+++ b/ui-ssdiff.c
@@ -114,11 +114,10 @@ static char *replace_tabs(char *line)
 {
 	char *prev_buf = line;
 	char *cur_buf;
-	int linelen = strlen(line);
+	size_t linelen = strlen(line);
 	int n_tabs = 0;
 	int i;
 	char *result;
-	char *spaces = "        ";
 
 	if (linelen == 0) {
 		result = xmalloc(1);
@@ -126,20 +125,23 @@ 		result[0] = '\0';
 		return result;
 	}
 
-	for (i = 0; i < linelen; i++)
+	for (i = 0; i < linelen; i++) {
 		if (line[i] == '\t')
 			n_tabs += 1;
+	}
 	result = xmalloc(linelen + n_tabs * 8 + 1);
 	result[0] = '\0';
 
-	while (1) {
+	for (;;) {
 		cur_buf = strchr(prev_buf, '\t');
 		if (!cur_buf) {
 			strcat(result, prev_buf);
 			break;
 		} else {
 			strncat(result, prev_buf, cur_buf - prev_buf);
-			strncat(result, spaces, 8 - (strlen(result) % 8));
+			linelen = strlen(result);
+			memset(&result[linelen], ' ', 8 - (linelen % 8));
+			result[linelen + 8 - (linelen % 8)] = '\0';
 		}
 		prev_buf = cur_buf + 1;
 	}