Alfie Richards <alfie.richa...@arm.com> writes:
> 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 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;

It seems simpler (and IMO slightly clearer) to test for:

  if (lhs.size () == 0)
    return true;

OK that way if you agree.

Thanks,
Richard

>      return memcmp (lhs.begin (), rhs.begin (), lhs.size ()) == 0;
>    }

Reply via email to