bug-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Fix warning in find_percent_cached with GCC 10.2.1


From: Nick Gasson
Subject: [PATCH] Fix warning in find_percent_cached with GCC 10.2.1
Date: Mon, 12 Apr 2021 21:52:27 +0800

I tried building the latest git version of make and got the following
warning-as-error:

src/read.c: In function ‘find_percent_cached’:
cc1: error: function may return address of local variable 
[-Werror=return-local-addr]
In file included from src/makeint.h:31,
                 from src/read.c:17:
lib/alloca.h:49:18: note: declared here
   49 | #  define alloca __builtin_alloca
src/read.c:2515:19: note: in expansion of macro ‘alloca’
 2515 |             new = alloca (slen + 1);
      |                   ^~~~~~
cc1: all warnings being treated as errors

GCC is complaining about the alloca-ed string `new' escaping through the
`return p' at the end of the function but its analysis cannot see that
it will always be replaced by the copy from `strcache_add' before then.

This patch replaces the alloca with a normal malloc which silences the
warning and seems safer generally.  This code path is only triggered
when there is an escaped % in the middle of the string so shouldn't
affect performance.  None of the existing tests hit this so I've added
one that does.
---
 src/read.c                  | 3 ++-
 tests/scripts/misc/general4 | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/read.c b/src/read.c
index c0e3315f4139..dc975f84819f 100644
--- a/src/read.c
+++ b/src/read.c
@@ -2512,7 +2512,7 @@ find_percent_cached (const char **string)
         if (! new)
           {
             slen = strlen (*string);
-            new = alloca (slen + 1);
+            new = xmalloc (slen + 1);
             memcpy (new, *string, slen + 1);
             p = new + (p - *string);
             *string = new;
@@ -2539,6 +2539,7 @@ find_percent_cached (const char **string)
       *string = strcache_add (*string);
       if (p)
         p = *string + (p - new);
+      free (new);
     }
 
   /* If we didn't find a %, return NULL.  Otherwise return a ptr to it.  */
diff --git a/tests/scripts/misc/general4 b/tests/scripts/misc/general4
index 0077c896c794..bae7fe3571de 100644
--- a/tests/scripts/misc/general4
+++ b/tests/scripts/misc/general4
@@ -125,4 +125,10 @@ a: ; @echo hi
 !,
               '', "hi\n");
 
+# Regression test for escaped percents
+run_make_test(q(prefix\%.foo: ; @echo '$@'),
+              'prefixbar.foo',
+              "#MAKE#: *** No rule to make target 'prefixbar.foo'.  Stop.",
+              512);
+
 1;
-- 
2.30.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]