Suppress -Wcast-qual warnings in bsearch

Message ID YKUdWuzFbiAnWx+Y@redhat.com
State Superseded
Headers show
Series
  • Suppress -Wcast-qual warnings in bsearch
Related show

Commit Message

Wilco Dijkstra via Libc-alpha May 19, 2021, 2:14 p.m.
This fixes these GCC warnings when -Wsystem-headers is used:

/usr/include/bits/stdlib-bsearch.h: In function ‘bsearch’:
/usr/include/bits/stdlib-bsearch.h:32:13: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
    32 |       __p = (void *) (((const char *) __base) + (__idx * __size));
       |             ^
/usr/include/bits/stdlib-bsearch.h:39:16: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
    39 |         return (void *) __p;
       |                ^

OK to push?
commit 2e988862a07b75b433468cfd591a8acb1ab4f0af
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed May 19 14:58:50 2021 +0100

    Suppress -Wcast-qual warnings in bsearch
    
    The first cast to (void *) is redundant but should be (const void *)
    anyway, because that's the type of the lvalue being assigned to.
    
    The second cast is necessary and intentionally not const-correct, so
    tell the compiler not to warn about it.

Comments

Joseph Myers May 19, 2021, 3:37 p.m. | #1
On Wed, 19 May 2021, Jonathan Wakely via Libc-alpha wrote:

> +#pragma GCC diagnostic push

> +#pragma GCC diagnostic ignored "-Wcast-qual"

>  	return (void *) __p;

> +#pragma GCC diagnostic pop


I think such pragma uses in installed headers should be conditional on 
__GNUC_PREREQ (4, 6) (either directly or via conditionally defining a 
macro in sys/cdefs.h).

-- 
Joseph S. Myers
joseph@codesourcery.com
Wilco Dijkstra via Libc-alpha May 19, 2021, 3:50 p.m. | #2
On 19/05/21 15:37 +0000, Joseph Myers wrote:
>On Wed, 19 May 2021, Jonathan Wakely via Libc-alpha wrote:

>

>> +#pragma GCC diagnostic push

>> +#pragma GCC diagnostic ignored "-Wcast-qual"

>>  	return (void *) __p;

>> +#pragma GCC diagnostic pop

>

>I think such pragma uses in installed headers should be conditional on

>__GNUC_PREREQ (4, 6) (either directly or via conditionally defining a

>macro in sys/cdefs.h).


Good point.

I spent about two minutes trying to do something with _Pragma in
sys/cdefs.h to allow:

__GLIBC_IGNORE_WARNING("-Wcast-qual")
   return (void *) __p;
__GLIBC_UNIGNORE_WARNING

but didn't get it working, so here's a patch that just tests
__GNUC_PREREQ directly.
commit ee7c2451a102b4cd3c87a31b3156ad4458729840
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed May 19 16:48:19 2021 +0100

    Suppress -Wcast-qual warnings in bsearch
    
    The first cast to (void *) is redundant but should be (const void *)
    anyway, because that's the type of the lvalue being assigned to.
    
    The second cast is necessary and intentionally not const-correct, so
    tell the compiler not to warn about it.

diff --git a/bits/stdlib-bsearch.h b/bits/stdlib-bsearch.h
index 4132dc6af0..d688ed2e15 100644
--- a/bits/stdlib-bsearch.h
+++ b/bits/stdlib-bsearch.h
@@ -29,14 +29,21 @@ bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
   while (__l < __u)
     {
       __idx = (__l + __u) / 2;
-      __p = (void *) (((const char *) __base) + (__idx * __size));
+      __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
     }
 
   return NULL;

Patch

diff --git a/bits/stdlib-bsearch.h b/bits/stdlib-bsearch.h
index 4132dc6af0..a08ad34ed6 100644
--- a/bits/stdlib-bsearch.h
+++ b/bits/stdlib-bsearch.h
@@ -29,14 +29,17 @@  bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
   while (__l < __u)
     {
       __idx = (__l + __u) / 2;
-      __p = (void *) (((const char *) __base) + (__idx * __size));
+      __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
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-qual"
 	return (void *) __p;
+#pragma GCC diagnostic pop
     }
 
   return NULL;