On 11/14/19 10:11 AM, Martin Sebor wrote: > Adding tests for unsafe uses of unterminated constant char arrays > in string functions exposed the limitation in strncmp folding > described in PR 92501: GCC only folds strncmp calls involving > nul-terminated constant strings. > > The attached patch improves the folder to also handle unterminated > constant character arrays. This capability is in turn relied on > for the dependable detection of unsafe uses of unterminated arrays > in strncpy, a patch for which I'm about to post separately. > > Tested on x86_64-linux. > > Martin > > gcc-92501.diff > > PR tree-optimization/92501 - strncmp with constant unterminated arrays not > folded > > gcc/testsuite/ChangeLog: > > PR tree-optimization/92501 > * gcc.dg/strcmpopt_7.c: New test. > > gcc/ChangeLog: > > PR tree-optimization/92501 > * gimple-fold.c ((gimple_fold_builtin_string_compare): Let strncmp > handle unterminated arrays. Rename local variables for clarity. > > Index: gcc/gimple-fold.c > =================================================================== > --- gcc/gimple-fold.c (revision 278253) > +++ gcc/gimple-fold.c (working copy) > @@ -2361,9 +2361,32 @@ gimple_fold_builtin_string_compare (gimple_stmt_it > return true; > } > > - const char *p1 = c_getstr (str1); > - const char *p2 = c_getstr (str2); > + /* Initially set to the number of characters, including the terminating > + nul if each array has one. Nx == strnlen(Sx, Nx) implies that > + the array is not terminated by a nul. > + For nul-terminated strings then adjusted to their length. */ > + unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1; > + const char *p1 = c_getstr (str1, &len1); > + const char *p2 = c_getstr (str2, &len2); > > + /* The position of the terminting nul character if one exists, otherwise terminting/terminating/
OK with the nit fixed. jeff