diff options
author | Andy Green <andy@warmcat.com> | 2018-06-13 07:34:07 +0800 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2018-06-15 17:24:59 +0200 |
commit | c2add96c8dc9d5346725080fad774810c14839a6 (patch) | |
tree | 2acfa46a563f340193bdc15f9e929f85c0a912f8 | |
parent | fdeb1f28a9e4c2c6527dc950a9fc41cdbaf51d4f (diff) | |
download | cgit-ch/gcc-8.tar.gz |
gcc8.1: fix strncat warningch/gcc-8
../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));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Actually the strncat that was there before intends that its
stock of spaces gets truncated, and it's not a problem.
However gcc8.1 is also right, normally truncation is undesirable.
Make the code do the padding explicitly.
Signed-off-by: Andy Green <andy@warmcat.com>
-rw-r--r-- | ui-ssdiff.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/ui-ssdiff.c b/ui-ssdiff.c index 7f261ed..e520b95 100644 --- a/ui-ssdiff.c +++ b/ui-ssdiff.c @@ -118,7 +118,6 @@ static char *replace_tabs(char *line) int n_tabs = 0; int i; char *result; - char *spaces = " "; if (linelen == 0) { result = xmalloc(1); @@ -138,8 +137,17 @@ static char *replace_tabs(char *line) strcat(result, prev_buf); break; } else { + char *p; + int n; + strncat(result, prev_buf, cur_buf - prev_buf); - strncat(result, spaces, 8 - (strlen(result) % 8)); + + n = strlen(result); + p = result + n; + n = 8 - (n % 8); + while (n--) + *p++ = ' '; + *p = '\0'; } prev_buf = cur_buf + 1; } |