On Tue, Nov 26, 2024 at 06:51:40AM +0100, Heiko Eißfeldt wrote:
> Would it make sense to have a nullptr check for asmspec at the
> beginning, too?

No.
There is already
  if (asmspec != 0)
around it which guarantees it and strip_reg_name will never return NULL.

> diff --git a/gcc/varasm.cc b/gcc/varasm.cc
> index dd67dd441c0..3e3e4bc5b42 100644
> --- a/gcc/varasm.cc
> +++ b/gcc/varasm.cc
> @@ -988,16 +988,21 @@ decode_reg_name_and_count (const char *asmspec, int 
> *pnregs)
>        asmspec = strip_reg_name (asmspec);
>  
>        /* Allow a decimal number as a "register name".  */
> -      for (i = strlen (asmspec) - 1; i >= 0; i--)
> -     if (! ISDIGIT (asmspec[i]))
> -       break;
> -      if (asmspec[0] != 0 && i < 0)
> +      if (ISDIGIT (asmspec[0]))
>       {
> -       i = atoi (asmspec);
> -       if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
> -         return i;
> -       else
> -         return -2;
> +       char *pend;
> +       errno = 0;
> +       unsigned long j = strtoul (asmspec, &pend, 10);
> +       if (*pend == '\0')
> +         {
> +           static_assert( FIRST_PSEUDO_REGISTER <= INT_MAX );

This is wrongly formatted and valid only in C++17 and later, while
GCC is currently written in C++14.
So it needs to be
              static_assert (FIRST_PSEUDO_REGISTER <= INT_MAX, "");

> +           if (errno != ERANGE
> +               && j < FIRST_PSEUDO_REGISTER
> +               && reg_names[j][0])
> +             return j;
> +           else
> +             return -2;
> +         }
>       }
>  
>        for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)

Ok for trunk with that change.

        Jakub

Reply via email to