Fix compiler warning from GCC9 with -D_FORTIFY_SOURCE=2
authorFabian Keil <fk@fabiankeil.de>
Sat, 5 Dec 2020 13:33:38 +0000 (14:33 +0100)
committerFabian Keil <fk@fabiankeil.de>
Mon, 7 Dec 2020 15:01:33 +0000 (16:01 +0100)
   pcrs.c:284:14: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=]
     284 |       text = strncpy(text, replacement, length + 1);
         |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   pcrs.c: In function 'pcrs_compile':
   pcrs.c:268:13: note: length computed here
     268 |    length = strlen(replacement);
         |             ^~~~~~~~~~~~~~~~~~~

While the warning is correct, the length of the destination
argument also depends on the length of the source argument
so we're good.

Using strlcpy() instead of strncpy() suppresses the warning
and 'text' is filled with zeros anyway so the difference
doesn't matter.

Reported by Lee.

pcrs.c

diff --git a/pcrs.c b/pcrs.c
index ca6ba68..8310098 100644 (file)
--- a/pcrs.c
+++ b/pcrs.c
@@ -281,7 +281,7 @@ static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int tr
     */
    if (trivialflag)
    {
     */
    if (trivialflag)
    {
-      text = strncpy(text, replacement, length + 1);
+      strlcpy(text, replacement, length + 1);
       k = (int)length;
    }
 
       k = (int)length;
    }