[PATCH] issue -Wstring-compare in more case (PR 95673)

Jeff Law law@redhat.com
Fri Nov 6 16:39:09 GMT 2020


On 9/30/20 6:14 PM, Martin Sebor via Gcc-patches wrote:
> -Wstring-compare triggers under the same strict conditions as
> the strcmp/strncmp call is folded into a constant: only when
> all the uses of the result are [in]equality expressions with
> zero.  However, even when the call cannot be folded into
> a constant because the result is in addition used in other
> expressions besides equality to zero, GCC still sets the range
> of the result to nonzero.  So in more complex functions where
> some of the uses of the same result are in tests for equality
> to zero and others in other expressions, the warning fails to
> point out the very mistake it's designed to detect.
>
> The attached change enhances the function that determines how
> the strcmp/strncmp is used to also make it possible to detect
> the mistakes in the multi-use situations.
>
> Tested on x86_64-linux & by building Glibc and Binutils/GDB
> and confirming it triggers no new warnings.
>
> Martin
>
> gcc-95673.diff
>
> PR middle-end/95673 - missing -Wstring-compare for an impossible strncmp test
>
> gcc/ChangeLog:
>
> 	PR middle-end/95673
> 	* tree-ssa-strlen.c (used_only_for_zero_equality): Rename...
> 	(use_in_zero_equality): ...to this.  Add a default argument.
> 	(handle_builtin_memcmp): Adjust to the name change above.
> 	(handle_builtin_string_cmp): Same.
> 	(maybe_warn_pointless_strcmp): Same.  Pass in an explicit argument.
>
> gcc/testsuite/ChangeLog:
>
> 	PR middle-end/95673
> 	* gcc.dg/Wstring-compare-3.c: New test.

Please retest on the trunk and if testing is OK this is fine for the trunk.

jeff




More information about the Gcc-patches mailing list