public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95354] New: GCC misuse "nonnull-attribute" option and can not detect it as UB as well
@ 2020-05-27  4:25 haoxintu at gmail dot com
  2020-05-27  4:50 ` [Bug c++/95354] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-27  4:25 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95354
           Summary: GCC misuse "nonnull-attribute" option and can not
                    detect it as UB as well
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

This case test.cc

#include<iostream>
#include<cstddef>
void has_nonnull_argument(__attribute__((nonnull)) int *p) { 
     ; 
}
int main () {
    has_nonnull_argument(NULL);
    std::cout << "ok" << std::endl;
    return 0;
}

in GCC-trunk

$./g++ -fsanitize=undefined test.cc ; ./a.out 
test.cc:3:57: warning: ‘nonnull’ attribute only applies to function types
[-Wattributes]
    3 | void has_nonnull_argument(__attribute__((nonnull)) int *p) {
      |                                                         ^
ok

$./g++ -fsanitize=nonull-attribute test.cc ; ./a.out 
test.cc:3:57: warning: ‘nonnull’ attribute only applies to function types
[-Wattributes]
    3 | void has_nonnull_argument(__attribute__((nonnull)) int *p) {
      |                                                         ^
ok

in Clang-trunk

$clang++ -fsanitize=nonnull-attribute test.cc ; ./a.out 
est.cc:7:30: warning: null passed to a callee that requires a non-null argument
[-Wnonnull]
    has_nonnull_argument(NULL);
                         ~~~~^
1 warning generated.
test.cc:7:26: runtime error: null pointer passed as argument 1, which is
declared to never be null
test.cc:3:42: note: nonnull attribute specified here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.cc:7:26 in 
ok

According to the description in
https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#Instrumentation-Options
for "-fsanitize=nonull-attribute", it says "This option enables instrumentation
of calls, checking whether null values are not passed to arguments marked as
requiring a non-null value by the nonnull function attribute." 

I guess the warning message by GCC may also incorrect, the correct one should
look like in Clang produced.

I have tested them in recent GCC versions including GCC-8, GCC-9, and GCC-10,
they have the same symptom as well.

My GCC version is
$g++ --version
g++ (GCC) 11.0.0 20200526 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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

end of thread, other threads:[~2020-05-27 11:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-27  4:25 [Bug c++/95354] New: GCC misuse "nonnull-attribute" option and can not detect it as UB as well haoxintu at gmail dot com
2020-05-27  4:50 ` [Bug c++/95354] " pinskia at gcc dot gnu.org
2020-05-27  9:16 ` redi at gcc dot gnu.org
2020-05-27  9:32 ` haoxintu at gmail dot com
2020-05-27 10:46 ` redi at gcc dot gnu.org
2020-05-27 11:43 ` haoxintu at gmail dot com

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).