public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/94746] New: -Wsizeof-pointer-div not triggered by system header macros
@ 2020-04-24 13:39 colomar.6.4.3 at gmail dot com
  2020-04-25 19:10 ` [Bug c/94746] " asolokha at gmx dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: colomar.6.4.3 at gmail dot com @ 2020-04-24 13:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94746

            Bug ID: 94746
           Summary: -Wsizeof-pointer-div not triggered by system header
                    macros
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: colomar.6.4.3 at gmail dot com
  Target Milestone: ---

I found out that the macro `#define __arraycount(__x) (sizeof(__x) /
sizeof(__x[0]))` which is in libbsd's <sys/cdefs.h> doesn't trigger any warning
when applied to a pointer.

I investigated the reason, and I found out that my own `#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))` doesn't trigger any warnings either (it is
installed in `/usr/local/include/`, but it does trigger warnings when compiling
before installing it.

So I came to the conclusion that the bug is the following:

GCC suppresses warnings in system headers, which of course is a generally good
thing, but in this case it is not a good thing.

Maybe a solution would be to not suppress warnings about macros defined in
system headers (this might suddenly trigger lots of warnings, some of them
maybe unwanted, I don't know).  There might be more bugs like this one being
ignored.

Code to reproduce the bug:

#include <sys/cdefs.h>

int main(void)
{
        int *p;

        return  __arraycount(p);
}

Minimum compilation settings to reproduce the bug:

$ gcc -Wsizeof-pointer-div bug.c `pkg-config --cflags libbsd-overlay`

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/94746] -Wsizeof-pointer-div not triggered by system header macros
  2020-04-24 13:39 [Bug c/94746] New: -Wsizeof-pointer-div not triggered by system header macros colomar.6.4.3 at gmail dot com
@ 2020-04-25 19:10 ` asolokha at gmx dot com
  2020-04-25 22:15 ` colomar.6.4.3 at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: asolokha at gmx dot com @ 2020-04-25 19:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94746

Arseny Solokha <asolokha at gmx dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |asolokha at gmx dot com

--- Comment #1 from Arseny Solokha <asolokha at gmx dot com> ---
I'm not a GCC developer, but this is by design.

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wsystem-headers

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/94746] -Wsizeof-pointer-div not triggered by system header macros
  2020-04-24 13:39 [Bug c/94746] New: -Wsizeof-pointer-div not triggered by system header macros colomar.6.4.3 at gmail dot com
  2020-04-25 19:10 ` [Bug c/94746] " asolokha at gmx dot com
@ 2020-04-25 22:15 ` colomar.6.4.3 at gmail dot com
  2020-04-26  0:08 ` colomar.6.4.3 at gmail dot com
  2024-05-15  7:00 ` alx at kernel dot org
  3 siblings, 0 replies; 5+ messages in thread
From: colomar.6.4.3 at gmail dot com @ 2020-04-25 22:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94746

--- Comment #2 from Alejandro Colomar <colomar.6.4.3 at gmail dot com> ---
Maybe the design is not perfect.

Maybe some special warnings should still be warned about when they are used in
user's code.  I don't think there are any possible false positives with this
warning.  But still I don't know.

Maybe I should add a pragma in the system header to enable `-Wsystem-headers`
for that macro.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/94746] -Wsizeof-pointer-div not triggered by system header macros
  2020-04-24 13:39 [Bug c/94746] New: -Wsizeof-pointer-div not triggered by system header macros colomar.6.4.3 at gmail dot com
  2020-04-25 19:10 ` [Bug c/94746] " asolokha at gmx dot com
  2020-04-25 22:15 ` colomar.6.4.3 at gmail dot com
@ 2020-04-26  0:08 ` colomar.6.4.3 at gmail dot com
  2024-05-15  7:00 ` alx at kernel dot org
  3 siblings, 0 replies; 5+ messages in thread
From: colomar.6.4.3 at gmail dot com @ 2020-04-26  0:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94746

--- Comment #3 from Alejandro Colomar <colomar.6.4.3 at gmail dot com> ---
I tried to use ``#pragma GCC diagnostic`` to enable ``-Wsystem-headers`` only
for that macro.  It bloated me with completely unrelated errors from libraries.
 So it is not an option.

The only workaround right now is to use a ``_Static_assert``:

.. code-block:: c

        #include <sys/cdefs.h>


        #define is_same_type(a, b)                                      \
                __builtin_types_compatible_p(__typeof__(a), __typeof__(b))

        #define is_array(a)             (!is_same_type((a), &(a)[0]))

        #define Static_assert_array(a)                                  \
                _Static_assert(is_array(a), "Not a `[]` !")

        #define ARRAY_SIZE(arr)         __extension__(                  \
        {                                                               \
                Static_assert_array(arr);                               \
                __arraycount((arr));                                    \
        }                                                               \
        )

This macro is safe no matter which warnings are enabled.  There is no other way
to write a safe macro in a system library for calculating the size of an array.
 I would call that a bug.  The warning is completely useless, unless you keep
copy&pasting that macro for each and every project, which is of course *wrong*.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/94746] -Wsizeof-pointer-div not triggered by system header macros
  2020-04-24 13:39 [Bug c/94746] New: -Wsizeof-pointer-div not triggered by system header macros colomar.6.4.3 at gmail dot com
                   ` (2 preceding siblings ...)
  2020-04-26  0:08 ` colomar.6.4.3 at gmail dot com
@ 2024-05-15  7:00 ` alx at kernel dot org
  3 siblings, 0 replies; 5+ messages in thread
From: alx at kernel dot org @ 2024-05-15  7:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94746

Alejandro Colomar <alx at kernel dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |WONTFIX
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #4 from Alejandro Colomar <alx at kernel dot org> ---
A static_assert(3) is indeed the right solution.  As Arseny said, this is by
design, so I'll close it as WONTFIX.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-05-15  7:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-24 13:39 [Bug c/94746] New: -Wsizeof-pointer-div not triggered by system header macros colomar.6.4.3 at gmail dot com
2020-04-25 19:10 ` [Bug c/94746] " asolokha at gmx dot com
2020-04-25 22:15 ` colomar.6.4.3 at gmail dot com
2020-04-26  0:08 ` colomar.6.4.3 at gmail dot com
2024-05-15  7:00 ` alx at kernel dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).