]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 4875 pt1: GCC-8 compile errors with -O3 optimization (#287)
authorAmos Jeffries <yadij@users.noreply.github.com>
Tue, 18 Sep 2018 17:43:59 +0000 (17:43 +0000)
committerAmos Jeffries <yadij@users.noreply.github.com>
Sat, 29 Sep 2018 13:45:21 +0000 (01:45 +1200)
 Use xstrncpy instead of strncat for String appending

Our xstrncpy() is safer, not assuming the existing char*
is nul-terminated and accounting explicitly for the
nul-terminator byte.

GCC-8 -O3 optimizations were exposing a strncat() output
truncation of the terminator when insufficient space was
available in the String buffer.

We suspect the GCC error to be a false-positive for -O3
builds and, even it it is accurate, these changes should
not affect builds with lower optimization levels.

src/String.cc

index 8c80c633b6a8cc94cb8ab95cee2f3b8048cc5427..4e4d23d086fec7a128a2cd63bdb3320a8cfa5685 100644 (file)
@@ -164,8 +164,8 @@ String::append( char const *str, int len)
     assert(str && len >= 0);
 
     PROF_start(StringAppend);
-    if (len_ + len < size_) {
-        strncat(buf_, str, len);
+    if (len_ + len + 1 /*'\0'*/ < size_) {
+        xstrncpy(buf_+len_, str, len+1);
         len_ += len;
     } else {
         // Create a temporary string and absorb it later.