[LLVMdev] optimization assumes malloc return is non-null

Chris Lattner sabre at nondot.org
Wed Apr 30 19:21:02 PDT 2008

On Wed, 30 Apr 2008, Ryan M. Lefever wrote:
> Consider the following c code:
> #include <stdlib.h>
> int main(int argc, char** argv){
>   if(malloc(sizeof(int)) == NULL){ return 0; }
>   else{ return 1; }
> }
> When I compile it with -O3, it produces the following bytecode:
> define i32 @main(i32 %argc, i8** %argv) {
> entry:
>         ret i32 1
> }
> Is this an error?  It should be possible for malloc to return NULL, if
> it can not allocate more space.  In fact, some programs should be able
> to gracefully handle such situations.

LLVM should not (and does not, afaik) assume the malloc succeeds in 

If LLVM is able to eliminate all users of the malloc assuming the 
malloc succeeded (as in this case), then it is safe to assume the malloc 
returned success.



More information about the llvm-dev mailing list