no warning with returns_nonnull

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

no warning with returns_nonnull

Renato Golin via cfe-dev
clang 3.6 brought some very useful warnings when a function argument
with __attribute__((nonnull)) had a NULL check:
warning: nonnull parameter 'p' will evaluate to 'true' on first
encounter [-Wpointer-bool-conversion]

With gcc and earlier clang versions, the NULL check was silently
optimized away.

We just realized that the same problem remains with
__attribute__((returns_nonnull)): NULL checks are silently optimized
away, as in the following test case.

int *nul(void) {
     return NULL;
}

__attribute__((returns_nonnull)) int *foo(void)
{
     return nul();
}

int main(void)
{
     fprintf(stderr, "%p\n", foo());
     if (foo() == NULL) {
         fprintf(stderr, "foo is null\n");
     }
     return 0;
}

Is it planned to have a warning in that case ?
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: no warning with returns_nonnull

Renato Golin via cfe-dev
Patch to fix this is in review at http://reviews.llvm.org/D15324 . It should be able to catch simple cases like the one you posted.

Thanks for catching this! :)

On Mon, Dec 7, 2015 at 6:47 AM, Xavier Chantry via cfe-dev <[hidden email]> wrote:
clang 3.6 brought some very useful warnings when a function argument with __attribute__((nonnull)) had a NULL check:
warning: nonnull parameter 'p' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]

With gcc and earlier clang versions, the NULL check was silently optimized away.

We just realized that the same problem remains with __attribute__((returns_nonnull)): NULL checks are silently optimized away, as in the following test case.

int *nul(void) {
    return NULL;
}

__attribute__((returns_nonnull)) int *foo(void)
{
    return nul();
}

int main(void)
{
    fprintf(stderr, "%p\n", foo());
    if (foo() == NULL) {
        fprintf(stderr, "foo is null\n");
    }
    return 0;
}

Is it planned to have a warning in that case ?
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev