On Mon, Jul 28, 2025 at 7:53 AM Alfie Richards <alfie.richa...@arm.com> wrote:
>
> Hi,
>
> Small fix to resolve an UBSAN diagnostic.
>
> Reg tested for Aarch64
>
> Thanks,
> Alfie
>
> -- >8 --
>
> This adds a nullptr check to fix a regression where it is possible to call
> `memcmp (NULL, NULL, 0)` which is UB.

This is obvious at least to me. Also `memcmp (NULL, NULL, 0)` is not
UB always; C26 makes it well defined. nonnull_if_nonzero is on the
builtins these days but many libc (older glibc included) still uses
nonnull which has priority over it.

Thanks,
Andrew Pinski

>
> This should fix the bootstrap-ubsan build.
>
> gcc/ChangeLog:
>         PR middle-end/121261
>         * vec.h: Add null ptr check.
> ---
>  gcc/vec.h | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/gcc/vec.h b/gcc/vec.h
> index 9604edb1c3c..1277a881f1c 100644
> --- a/gcc/vec.h
> +++ b/gcc/vec.h
> @@ -2514,6 +2514,10 @@ public:
>        return false;
>      if (lhs.size () != rhs.size ())
>        return false;
> +    /* Case where either is a NULL pointer and therefore, as both are valid,
> +       both are empty slices with length 0.  */
> +    if (lhs.begin () == NULL || rhs.begin () == NULL)
> +      return true;
>      return memcmp (lhs.begin (), rhs.begin (), lhs.size ()) == 0;
>    }
>
> --
> 2.34.1
>

Reply via email to