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

            Bug ID: 85725
           Summary: strchr and strstr of a one-element array with a
                    non-empty string can be assumed to return null
           Product: gcc
           Version: 8.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 pr85724, calls to strchr, strrchr, and strstr with a one-element
array as the first argument can be folded to null when the second argument is a
non-empty string (or a non-zero character) because the only string that can be
stored in a one-element array is the empty string.

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

int f0 (void)
{
  return __builtin_strlen (a);   // folded to 0
}

char* f1 (void)
{
  return __builtin_strchr (a, '1');   // can be folded to null
}

char* f2 (const char *s)
{
  return __builtin_strstr (a, "123");   // can be folded to null
}

;; Function f0 (f0, funcdef_no=0, decl_uid=1957, cgraph_uid=0, symbol_order=0)

f0 ()
{
  <bb 2> [local count: 1073741825]:
  return 0;

}



;; Function f1 (f1, funcdef_no=1, decl_uid=1960, cgraph_uid=1, symbol_order=1)

f1 ()
{
  char * _2;

  <bb 2> [local count: 1073741825]:
  _2 = __builtin_strchr (&a, 49); [tail call]
  return _2;

}



;; Function f2 (f2, funcdef_no=2, decl_uid=1963, cgraph_uid=2, symbol_order=2)

f2 (const char * s)
{
  char * _2;

  <bb 2> [local count: 1073741825]:
  _2 = __builtin_strstr (&a, "123"); [tail call]
  return _2;

}

Reply via email to