[Pcre-svn] [1727] code/trunk: A small fix to pcregrep to avo…

Top Page
Delete this message
Author: Subversion repository
Date:  
To: pcre-svn
Subject: [Pcre-svn] [1727] code/trunk: A small fix to pcregrep to avoid compiler warnings for -Wformat-overflow= 2.
Revision: 1727
          http://vcs.pcre.org/viewvc?view=rev&revision=1727
Author:   ph10
Date:     2018-02-25 12:23:55 +0000 (Sun, 25 Feb 2018)
Log Message:
-----------
A small fix to pcregrep to avoid compiler warnings for -Wformat-overflow=2.


Modified Paths:
--------------
    code/trunk/ChangeLog
    code/trunk/pcregrep.c


Modified: code/trunk/ChangeLog
===================================================================
--- code/trunk/ChangeLog    2018-02-20 16:33:43 UTC (rev 1726)
+++ code/trunk/ChangeLog    2018-02-25 12:23:55 UTC (rev 1727)
@@ -49,7 +49,9 @@
 crash. This issue was fixed for other kinds of repeat in release 8.37 by change
 38, but repeating character classes were overlooked.


+6. A small fix to pcregrep to avoid compiler warnings for -Wformat-overflow=2.

+
Version 8.41 05-July-2017
-------------------------


Modified: code/trunk/pcregrep.c
===================================================================
--- code/trunk/pcregrep.c    2018-02-20 16:33:43 UTC (rev 1726)
+++ code/trunk/pcregrep.c    2018-02-25 12:23:55 UTC (rev 1727)
@@ -2527,7 +2527,14 @@
     }
   }


-sprintf(buffer, "%s%.*s%s", prefix[popts], patlen, ps, suffix[popts]);
+if (snprintf(buffer, PATBUFSIZE, "%s%.*s%s", prefix[popts], patlen, ps,
+      suffix[popts]) > PATBUFSIZE)
+  {
+  fprintf(stderr, "pcregrep: Buffer overflow while compiling \"%s\"\n",
+    ps);
+  return FALSE;
+  }
+
 p->compiled = pcre_compile(buffer, options, &error, &errptr, pcretables);
 if (p->compiled != NULL) return TRUE;


@@ -2763,8 +2770,15 @@
         int arglen = (argequals == NULL || equals == NULL)?
           (int)strlen(arg) : (int)(argequals - arg);


-        sprintf(buff1, "%.*s", baselen, op->long_name);
-        sprintf(buff2, "%s%.*s", buff1, fulllen - baselen - 2, opbra + 1);
+        if (snprintf(buff1, sizeof(buff1), "%.*s", baselen, op->long_name) >
+              (int)sizeof(buff1) ||
+            snprintf(buff2, sizeof(buff2), "%s%.*s", buff1,
+              fulllen - baselen - 2, opbra + 1) > (int)sizeof(buff2))
+          {
+          fprintf(stderr, "pcregrep: Buffer overflow when parsing %s option\n",
+            op->long_name);
+          pcregrep_exit(2);
+          }


         if (strncmp(arg, buff1, arglen) == 0 ||
            strncmp(arg, buff2, arglen) == 0)