On Tue 2026-11-10 16:10:43, Filip Kastl wrote:
> On Tue 2026-11-10 15:45:37, Richard Biener wrote:
> > On Tue, 10 Nov 2026, Filip Kastl wrote:
> > 
> > > On Tue 2026-11-10 15:21:25, Giuliano Belinassi wrote:
> > > > Hello.
> > > > 
> > > > On Tue, 2026-11-10 at 14:32 +0100, Richard Biener wrote:
> > > > > On Mon, 9 Nov 2026, Filip Kastl wrote:
> > > > > 
> > > > > > 1. The function gimple_fold_builtin_sprintf is only used in
> > > > > > gimple-ssa-sprintf.cc.  Move it there and make it private.
> > > > > > 
> > > > > > 2. Previously, gimple_fold_builtin_sprintf only handled cases where
> > > > > > the
> > > > > > format string were a directiveless plain string or a single %s. 
> > > > > > Here
> > > > > > we expand this to be able to handle multiple directives within a
> > > > > > single
> > > > > > format string, transforming each directive or plain string into
> > > > > > calls
> > > > > > to memcpy and strlen/strnlen if its size is unknown in compilation
> > > > > > time.
> > > > > > 
> > > > > > New tests were generated by Claude.
> > > > > > 
> > > > > > gcc/ChangeLog:
> > > > > > 
> > > > > >     * gimple-fold.cc (gimple_fold_builtin_sprintf): Move to
> > > > > >     gimple_fold_builtin_sprintf and make static.
> > > > > 
> > > > > Is the fact that the function isn't called from fold_stmt an
> > > > > oversight and would it not enable the followup by means of
> > > > > folding earlier?  I'd say it should be called (and that done
> > > > > as separate upfront patch).
> > > > 
> > > > Should it? If the folding of the sprintf happens before the strlen
> > > > pass, then sprintf would be optimized away and the pass has nothing to
> > > > work with to emit a warning/error to the user.  The original form is
> > > > gone and what is left is a sequence of calls to strlen and memcpy.
> > > > 
> > > > Furthermore, maybe no optimization pass would ever emit an sprintf
> > > > call?
> > > 
> > > Yeah, why would it actually be useful to have sprintf optimization be a 
> > > part of
> > > the gimple fold machinery?  Optimizing sprintf sounds to me more like a 
> > > thing
> > > you want to do with a specific pass once or twice in the optimization 
> > > pipeline.
> > > 
> > > Also, this comes to mind: The version of gimple_fold_builtin_sprintf from 
> > > the
> > > patch series requires the format string to be parsed.  If we ran it on 
> > > each
> > > call to fold_stmt, I think we could end up parsing a single format string 
> > > many
> > > times.
> > 
> > I'm not saying your enhancement should be done from fold_stmt, I'm
> > saying the existing optimization not triggering there is probably
> > an oversight.  Other functions like printf are folded.
> > 
> > Richard.
> 
> Ah, ok.  The old gimple_fold_builtin_sprintf is a lightweight function, so 
> that
> makes sense for fold_stmt.
> 
> So let's keep the old gimple_fold_builtin_sprintf, wire it into fold_stmt, and
> rename the gimple_fold_builtin_sprintf from the patch series to something 
> else.
> 
> Filip

So I looked into this.  GCC not folding sprintf via fold_stmt isn't an
oversight.  It is an explicit decision in r8-673-ga104bd88592d85 motivated by
pr77671.  The problem is that if you fold an sprintf call too early, the strlen
pass isn't able to produce warnings for it.  Indeed, if I try this patch

diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index 71dd8f1da74..56ab677269c 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -5579,6 +5579,10 @@ gimple_fold_builtin (gimple_stmt_iterator *gsi)
     case BUILT_IN_MEMMOVE:
       return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
                                            gimple_call_arg (stmt, 1), fcode);
+    case BUILT_IN_SPRINTF:
+      return gimple_fold_builtin_sprintf (gsi);
     case BUILT_IN_SPRINTF_CHK:
     case BUILT_IN_VSPRINTF_CHK:
       return gimple_fold_builtin_sprintf_chk (gsi, fcode);

I get failing builtin-sprintf-warn-2.c (and maybe other files from the
testsuite -- I only tried this one).  Here is a smaller testcase:

--- testcase ---
#include <stdio.h>

int main()
{
    char a[3];
    sprintf("%s", "abc");
    return 0;
}
--- ---

--- without patch ---
[fkastl@localhost gcc]$ ./xgcc -B . -O2 foo.c -Wall -Wextra -Wpedantic
foo.c: In function ‘main’:
foo.c:5:10: warning: unused variable ‘a’ [-Wunused-variable]
    5 |     char a[3];
      |          ^
foo.c:6:23: warning: ‘sprintf’ writing a terminating nul past the end of the 
destination [-Wformat-overflow=]
    6 |     sprintf("%s", "abc");
      |                       ^
foo.c:6:5: note: ‘sprintf’ output 4 bytes into a destination of size 3
    6 |     sprintf("%s", "abc");
      |     ^~~~~~~~~~~~~~~~~~~~
--- ---

vs

--- with patch ---
[fkastl@localhost gcc]$ ./xgcc -B . -O2 foo.c -Wall -Wextra -Wpedantic
foo.c: In function ‘main’:
foo.c:5:10: warning: unused variable ‘a’ [-Wunused-variable]
    5 |     char a[3];
      |          ^
foo.c:6:5: warning: ‘__builtin_memcpy’ forming offset 3 is out of the bounds 
[0, 3] [-Warray-bounds=]
    6 |     sprintf("%s", "abc");
      |     ^~~~~~~~~~~~~~~~~~~~
--- ---

Thus, I think we should just remove gimple_fold_builtin_sprintf (but let's add
a comment into gimple_fold_builtin that makes it clear this fold is omitted due
to pr77671).

But I'm of course open to other suggestions.

Thanks,
Filip

Reply via email to