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

            Bug ID: 95794
           Summary: strnlen of a constant string plus variable offset not
                    folded when bound exceeds size
           Product: gcc
           Version: 10.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: ---

GCC folds comparisons of the results of calls to strlen with constant strings
and variable offsets but it doesn't do the same for the equivalent strnlen with
a bound in excess of the string size (strnlen with a bound greater than the
string length are equivalent to strlen with the same argument).

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

void f (int i)
{
  if (__builtin_strlen (&a[i]) > 5)       // folded to false
    __builtin_abort ();
}

void g (int i)
{
  if (__builtin_strnlen (&a[i], 7) > 5)   // not folded
    __builtin_abort ();
}


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

f (int i)
{
  <bb 2> [local count: 1073741824]:
  return;

}



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

g (int i)
{
  const char * _1;
  long unsigned int _2;
  sizetype _6;

  <bb 2> [local count: 1073741824]:
  _6 = (sizetype) i_3(D);
  _1 = &a + _6;
  _2 = __builtin_strnlen (_1, 7);
  if (_2 > 5)
    goto <bb 3>; [0.00%]
  else
    goto <bb 4>; [100.00%]

  <bb 3> [count: 0]:
  __builtin_abort ();

  <bb 4> [local count: 1073741824]:
  return;

}

Reply via email to