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

--- Comment #4 from Jonny Grant <jg at jguk dot org> ---
(In reply to Andrew Pinski from comment #3)
> *** Bug 108893 has been marked as a duplicate of this bug. ***

Hello Andrew
May I check, I thought attribute access read_only was different from attribute
nonnull?

https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

"Note that the access attribute merely specifies how an object referenced by
the pointer argument can be accessed; it does not imply that an access will
happen. Also, the access attribute does not imply the attribute nonnull; it may
be appropriate to add both attributes at the declaration of a function that
unconditionally manipulates a buffer via a pointer argument. See the nonnull
attribute for more information and caveats."

Seems to be stating, nonnull is not the same as access read_only.  Or am I
missing something?



I had understood __attribute__((access(read_only, 1)));  would be enough on a
-O3 -Wall build to generate a warning that a nullptr had been passed (even if
it was not actually dereferenced)



Here is an example that actually does derefernence, and runtime SEGV, but it
the access read_only itself doesn't give any build warning. Just an example,
I'm only hoping to get build warnings when optimizer sees that nullptr got
through

g++ -std=c++23 -O3 -Wall
https://godbolt.org/z/KbzcjEjYj

void f(const char * const str) __attribute__((access(read_only,1)));
void f(const char * const str)
{
    if(*str == 'a')
    {
        __builtin_puts("a1");
    }
    else
    {
        __builtin_puts("a2");
    }
}

int main()
{
    const char * a = nullptr;
    f(a);
}

Reply via email to