[PATCH] Warn when NULL is returned from 'operator new' without 'throw()'

Artyom Skrobov Artyom.Skrobov at arm.com
Fri Jan 10 05:07:28 PST 2014

Thank you for your suggestions Richard!
One point though:

> Please use RetValExp->isNullPointerConstant instead.
> Please also add testcases for operator new returning nullptr, and
returning expressions such as 1 - 1, and for operator new marked as

I want to note that expressions such as 1-1 are invalid as return values
from operator new, and produce "error: cannot initialize return object of
type 'void *' with an rvalue of type 'int'"

At the same time, expressions such as (void*)(1-1) are not recognized either
by isNullPointerConstant or by EvaluateAsInt as integer zeroes, namely
because they are not integers.

For a reference, GCC doesn't warn on

   void *operator new(size_t n) {
     return (void*)(1-1);

which isn't too bad; but neither does it warn on

   void *operator new(size_t n) {
     void* blah = 0;
     return blah;

nor even on

   void *operator new(size_t n) {
     return (void*)0;

which seems quite valuable to be able to detect.

isNullPointerConstant doesn't recognize the two latter cases as null pointer
constants, either.

Do you think Clang should be able to detect such null-pointer-expressions?
If so, could you advise how it could be implemented, seeing that
isNullPointerConstant and EvaluateAsInt prove ineffective?

More information about the cfe-commits mailing list