On 07/05/2018 05:54 PM, Martin Sebor wrote:
> GCC folds strlen() calls to empty substrings within arrays
> whose elements are explicitly initialized with NULs but
> fails to do the same for elements that are zeroed out
> implicitly.  For example:
> 
>   const char a[7] = "123\000\000\000";
>   int f (void)
>   {
>     return strlen (a + 5);   // folded
>   }
> 
> but
> 
>   const char b[7] = "123";
>   int g (void)
>   {
>     return strlen (b + 5);   // not folded
>   }
> 
> This is because the c_getstr() function only considers
> the leading TREE_STRING_LENGTH() number of elements of
> an array and not also the remaining elements up the full
> size of the array the string may be stored in.
> 
> The attached patch enhances the function to also consider
> those elements.  If there are more elements in the array
> than TREE_STRING_LENGTH() evaluates to they must be all
> NULs because the array is constant and initialized.
> 
> Tested along with the patch for PR 77357 on x86_64-linux.
> 
> Martin
> 
> gcc-86415.diff
> 
> 
> PR tree-optimization/86415 - strlen() not folded for substrings within 
> constant arrays
> 
> gcc/ChangeLog:
> 
>       PR tree-optimization/86415
>       * fold-const.c (c_getstr): Handle substrings.
> 
> gcc/testsuite/ChangeLog:
> 
>       PR tree-optimization/86415
>       * gcc.dg/strlenopt-48.c: New test.
OK.
jeff

Reply via email to