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

* [Bug c++/95354] GCC misuse "nonnull-attribute" option and can not detect it as UB as well
  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 ` pinskia at gcc dot gnu.org
  2020-05-27  9:16 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-05-27  4:50 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
No,
The place where nonnull attribute is not on the argument itself but rather on
the function.

See the document at:
https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Common-Function-Attributes.html#index-nonnull-function-attribute

THIS IS WHY GCC Is warning on that pointer and all.

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

* [Bug c++/95354] GCC misuse "nonnull-attribute" option and can not detect it as UB as well
  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
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-27  9:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Allowing the attribute on individual parameters might be nice though.

I hate the fact that for C++ member functions the first parameter is the
implicit 'this' pointer which always has to be non-null anyway.

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

* [Bug c++/95354] GCC misuse "nonnull-attribute" option and can not detect it as UB as well
  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
  4 siblings, 0 replies; 6+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-27  9:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Haoxin Tu <haoxintu at gmail dot com> ---
I see. Are there any cases that can trigger the UB of nonnull-attribute? I
doubt the usage of “-fsanitize=nonnull-attribute” in GCC...

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

* [Bug c++/95354] GCC misuse "nonnull-attribute" option and can not detect it as UB as well
  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
                   ` (2 preceding siblings ...)
  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
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-27 10:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Haoxin Tu from comment #3)
> I see. Are there any cases that can trigger the UB of nonnull-attribute? I
> doubt the usage of “-fsanitize=nonnull-attribute” in GCC...

Yes, just use the attribute correctly.

Using:

__attribute__((nonnull)) void has_nonnull_argument(int *p) { 
     ; 
}

will cause UBsan to diagnose it:

nn.cc:7:25: runtime error: null pointer passed as argument 1, which is declared
to never be null

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

* [Bug c++/95354] GCC misuse "nonnull-attribute" option and can not detect it as UB as well
  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
                   ` (3 preceding siblings ...)
  2020-05-27 10:46 ` redi at gcc dot gnu.org
@ 2020-05-27 11:43 ` haoxintu at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-27 11:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Haoxin Tu <haoxintu at gmail dot com> ---
(In reply to Jonathan Wakely from comment #4)
> (In reply to Haoxin Tu from comment #3)
> > I see. Are there any cases that can trigger the UB of nonnull-attribute? I
> > doubt the usage of “-fsanitize=nonnull-attribute” in GCC...
> 
> Yes, just use the attribute correctly.
> 
> Using:
> 
> __attribute__((nonnull)) void has_nonnull_argument(int *p) { 
>      ; 
> }
> 
> will cause UBsan to diagnose it:
> 
> nn.cc:7:25: runtime error: null pointer passed as argument 1, which is
> declared to never be null

Thanks, Jonathan, Got it ~

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