Blob Blame History Raw
From 875a7d84d6cc77431db27eeb140d9e94e4584e31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
Date: Tue, 14 Feb 2017 17:00:44 +0100
Subject: [PATCH] Silent a GCC 7 warning about too small buffer for printing an
 integer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GCC 7 reports this warning:

pcregrep.c:3194:68: warning: '%d' directive writing between 1 and 10 bytes into
a region of size 8 [-Wformat-overflow=]
     if (patterns->next == NULL) s[0] = 0; else sprintf(s, " number %d", j);
                                                                    ^~
pcregrep.c:3194:59: note: directive argument in the range [1, 2147483647]
     if (patterns->next == NULL) s[0] = 0; else sprintf(s, " number %d", j);
                                                           ^~~~~~~~~~~~
because the buffer s[] has only 16 characters. With 32-bit integers,
one needs up to 19 bytes to represent the sprintf() text.

This patch fixes it by avoiding the buffer at all.

Signed-off-by: Petr Písař <ppisar@redhat.com>
---
 pcregrep.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/pcregrep.c b/pcregrep.c
index 3cd70ee..87a3c2e 100644
--- a/pcregrep.c
+++ b/pcregrep.c
@@ -3190,9 +3190,11 @@ for (j = 1, cp = patterns; cp != NULL; j++, cp = cp->next)
   cp->hint = pcre_study(cp->compiled, study_options, &error);
   if (error != NULL)
     {
-    char s[16];
-    if (patterns->next == NULL) s[0] = 0; else sprintf(s, " number %d", j);
-    fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);
+    if (patterns->next == NULL)
+      fprintf(stderr, "pcregrep: Error while studying regex: %s\n", error);
+    else
+      fprintf(stderr, "pcregrep: Error while studying regex number %d: %s\n",
+        j, error);
     goto EXIT2;
     }
 #ifdef SUPPORT_PCREGREP_JIT
-- 
2.7.4