On 1/7/21 5:53 PM, Martin Sebor via Gcc-patches wrote:
> In PR 98097 Richard expects -Wstring-compare for a call to strcmp()
> with a member array and a string literal of larger size, used in
> an equality test.
>
> In virtually all cases the test will indicate the two are unequal
> because the string stored in the member must be shorter (to fit
> the terminating nul), but GCC doesn't fold the result because
> there's wicked code out there that treats whole aggregates as if
> they were strings, up their full size.  Because the warning is
> based on the same conservative assumptions as the optimization,
> it doesn't trigger, letting the almost certain bug go unnoticed.
>
> The attached patch allows -Wstring-compare to trigger for these
> bugs by partly decoupling the warning from the underlying strcmp
> optimization.  Making this possible requires adding a new member
> to the c_strlen_data struct, which in turn called for changing
> the meaning of the existing decl member to nonstr.  That led to
> changes elsewhere, simply to adjust to the name change.  For
> the purposes of review, the meat of the warning changes is in
> tree-ssa-strlen.c.  All the rest of changes simply adjust code
> to the new name.
>
> Tested on x86_64-linux (None of Binutils, GDB, Glibc, or Valgrind
> triggers any instances of the warning with this change.)
>
> Martin
>
> gcc-98097.diff
>
> PR middle-end/98097 - missing -Wstring-compare with a member array
>
> gcc/ChangeLog:
>
>       PR middle-end/98097
>       * builtins.c (unterminated_array): Adjust to a name change.  Adjust
>       indentation.
>       (c_strlen): Use a member instead of a local variable.
>       (expand_builtin_stpcpy_1): Adjust to a name change.
>       (fold_builtin_strlen): Same.
>       * builtins.h (struct c_strlen_data::nonstr): New data member to use
>       instead of decl.
>        (struct c_strlen_data::decl): Adjust comment.
>       * gimple-fold.c (get_range_strlen_tree): Set c_strlen_data::nonstr
>       in addition to c_strlen_data::decl.
>       (get_maxval_strlen): Adjust to a name change.
>       (gimple_fold_builtin_stpcpy): Same.
>       (gimple_fold_builtin_strlen): Same.
>       * gimple-ssa-sprintf.c (get_string_length): Same.
>       * tree-ssa-strlen.c (get_range_strlen_dynamic): Same.  Also set
>       struct c_strlen_data::decl.
>       (get_len_or_size): Use c_strlen_data::decl.  Succeed even for
>       nonconstant member arrays.
>       (strxcmp_eqz_result): Handle member arrays.
>       (handle_builtin_string_cmp): Issue warnings for member arrays.
>
> gcc/testsuite/ChangeLog:
>
>       PR middle-end/98097
>       * gcc.dg/Wstring-compare.c:
>       * gcc.dg/strcmpopt_10.c:
>       * gcc.dg/Wstring-compare-4.c: New test.
>       * gcc.dg/Wstring-compare-5.c: New test.
I think you need to update the function comment for gen_len_or_size to
describe the special case where we make the range invalidate and inverted.

OK with that change.

jeff

Reply via email to