[PATCH v2] Suppress -Wcast-qual warnings in bsearch

Florian Weimer fweimer@redhat.com
Thu Sep 30 16:41:42 GMT 2021


* Adhemerval Zanella via Libc-alpha:

> On 30/09/2021 12:07, Joseph Myers wrote:
>> On Thu, 30 Sep 2021, Adhemerval Zanella via Libc-alpha wrote:
>> 
>>>>>        else
>>>>> +#if __GNUC_PREREQ(4, 6)
>>>>> +# pragma GCC diagnostic push
>>>>> +# pragma GCC diagnostic ignored "-Wcast-qual"
>>>>> +#endif
>>>>>  	return (void *) __p;
>>>>> +#if __GNUC_PREREQ(4, 6)
>>>>> +# pragma GCC diagnostic pop
>>>>> +#endif
>>>>>      }
>> 
>> I think braces may need adding around those pragmas to avoid a pragma 
>> being considered the body of the else in some cases.
>
> But how exactly the pragma is changing the code semantic in this case?
> Would it be safe for all supported gcc (since it is an installed header)?

It's parsed as:

  while (__l < __u)
    {
      __idx = (__l + __u) / 2;
      __p = (const void *) (((const char *) __base) + (__idx * __size));
      __comparison = (*__compar) (__key, __p);
      if (__comparison < 0)
        __u = __idx;
      else if (__comparison > 0)
        __l = __idx + 1;
      else
#if __GNUC_PREREQ(4, 6)
          # pragma GCC diagnostic push
      # pragma GCC diagnostic ignored "-Wcast-qual"
#endif
      return (void *) __p;
#if __GNUC_PREREQ(4, 6)
      # pragma GCC diagnostic pop
#endif
    }

See

  (GCC-pragma) - whether a #pragma is a statement depends on the type of pra
  <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63326>

and:

  -Wmisleading-indentation should also detect missing indentation 
  <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66298#c3>

Sorry, I should have caught this during review (and actually tested the
patch).

Joseph is right, we need braces here.

Thanks,
Florian



More information about the Libc-alpha mailing list