public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/96925] New: missing warning on sprintf into destination at negative offset
@ 2020-09-03 16:03 msebor at gcc dot gnu.org
  2020-09-03 16:06 ` [Bug middle-end/96925] " msebor at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-09-03 16:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96925
           Summary: missing warning on sprintf into destination at
                    negative offset
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Only the first of the two invalid calls below is diagnosed.  Both should be.

$ cat z.c && gcc -O2 -S -Wall -xc -fdump-tree-optimized=/dev/stdout z.c
extern char a[];

void f (int i)
{
  if (i >= 0) i = -1;
  __builtin_strcpy (a + i, "3");   // warning (good)
}

void g (int i)
{
  if (i >= 0) i = -1;
  __builtin_sprintf (a + i, "%i", 3);   // missing warning
}
z.c: In function ‘f’:
z.c:6:3: warning: ‘__builtin_memcpy’ offset [-2147483648, -1] is out of the
bounds of object ‘a’ with type ‘char[]’ [-Warray-bounds]
    6 |   __builtin_strcpy (a + i, "3");   // warning (good)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
z.c:1:13: note: ‘a’ declared here
    1 | extern char a[];
      |             ^

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

f (int i)
{
  sizetype _1;
  char * _2;

  <bb 2> [local count: 1073741824]:
  i_4 = MIN_EXPR <i_3(D), -1>;
  _1 = (sizetype) i_4;
  _2 = &a + _1;
  __builtin_memcpy (_2, "3", 2); [tail call]
  return;

}



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

g (int i)
{
  sizetype _1;
  char * _2;

  <bb 2> [local count: 1073741824]:
  i_4 = MIN_EXPR <i_3(D), -1>;
  _1 = (sizetype) i_4;
  _2 = &a + _1;
  __builtin_sprintf (_2, "%i", 3); [tail call]
  return;

}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug middle-end/96925] missing warning on sprintf into destination at negative offset
  2020-09-03 16:03 [Bug middle-end/96925] New: missing warning on sprintf into destination at negative offset msebor at gcc dot gnu.org
@ 2020-09-03 16:06 ` msebor at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-09-03 16:06 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |56456
           Keywords|                            |diagnostic

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning for the strcpy is issued by the wrestrict pass which doesn't
consider sprintf.  -Wrestrict is implemented separately for sprintf but without
the same bounds checking.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
[Bug 56456] [meta-bug] bogus/missing -Warray-bounds

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-03 16:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-03 16:03 [Bug middle-end/96925] New: missing warning on sprintf into destination at negative offset msebor at gcc dot gnu.org
2020-09-03 16:06 ` [Bug middle-end/96925] " msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).