https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80523

            Bug ID: 80523
           Summary: -Wformat-overflow doesn't consider -fexec-charset
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Similarly to -Wformat (bug 20110), the -Wformat-overflow and
-Wformat-truncation warnings don't handle execution character sets that differ
from the host character set.  As a result the warnings don't work correctly
when -fexec-charset specifies such a character set.  Likewise the sprintf
return value optimization doesn't behave correctly.

The test case below shows the problems: the warning that is issued for function
f indicates the wrong number of characters (and the directive is mangled in the
text of the warning) and function g returns the wrong value.

$ cat c.c && gcc -O2 -S -Wall -Wextra -Wpedantic -Wconversion
-fexec-charset=IBM1047 -fdump-tree-optimized=/dev/stdout c.c
char d[1];

void f (void)
{
  __builtin_sprintf (d, "%-s\n", "foobar");
}

int g (void)
{
  return __builtin_snprintf (0, 0, "%-s", "foobar");
}

c.c: In function ‘f’:
c.c:5:25: warning: spurious trailing ‘%’ in format [-Wformat=]
   __builtin_sprintf (d, "%-s\n", "foobar");
                         ^~~~~~~
c.c:5:25: warning: too many arguments for format [-Wformat-extra-args]
c.c: In function ‘g’:
c.c:10:36: warning: too many arguments for format [-Wformat-extra-args]
   return __builtin_snprintf (0, 0, "%-s", "foobar");
                                    ^~~~~
c.c: In function ‘f’:
c.c:5:25: warning: ‘l`�’ directive writing 3 bytes into a region of size 1
[-Wformat-overflow=]
   __builtin_sprintf (d, "%-s\n", "foobar");
                         ^~~~~~~

;; Function f (f, funcdef_no=0, decl_uid=1794, cgraph_uid=0, symbol_order=1)

f ()
{
  <bb 2> [100.00%]:
  __builtin_sprintf (&d, "l`\xa2%", "\x86\x96\x96\x82\x81\x99"); [tail call]
  return;

}



;; Function g (g, funcdef_no=1, decl_uid=1797, cgraph_uid=1, symbol_order=2)

g ()
{
  <bb 2> [100.00%]:
  return 3;

}

Reply via email to