<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/155633>155633</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
New warning -Walloc-size causes injustified warning.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
earnol
</td>
</tr>
</table>
<pre>
The newly added warning via https://github.com/llvm/llvm-project/pull/150028/ is causes warning in unexpected situation.
Please see following [godbolt example](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:2,endLineNumber:15,positionColumn:1,positionLineNumber:10,selectionStartColumn:2,selectionStartLineNumber:15,startColumn:1,startLineNumber:10),source:'%23include+%3Cstdlib.h%3E%0A%23include+%3Cstdio.h%3E%0A%0Atypedef+unsigned+long+size_t%3B%0A%0Avoid+*realloc(void+*ptr,+size_t+size)+__attribute__((alloc_size(2)))%3B%0Avoid+*malloc(size_t+size)+__attribute__((alloc_size(1)))%3B%0A%0A%0Aint+f2_realloc_1()+%7B%0A++int+*p+%3D+(int+*)malloc(12)%3B%0A++int+*q+%3D+(int+*)realloc(p,+0)%3B+//+expected-warning+%7B%7Ballocation+of+insufficient+size+!'0!'+for+type+!'int!'+with+size+!'1!'%7D%7D%0A++printf(%22Pointer+is:+%25p%22,+q)%3B%0A++return+p+!!%3D+q%3B%0A%7D%0A%0Avoid+main()%0A%7B%0A++++f2_realloc_1()%3B%0A%7D%0A%0A//+-Xclang+-analyzer-checker%3Dcore++-Xclang+-analyzer-checker%3Dalpha.deadcode.UnreachableCode++-Xclang++-analyzer-checker%3Dalpha.core.CastSize+++-Xclang+-analyzer-checker%3Dunix++-Xclang+-analyzer-checker%3Ddebug.ExprInspection+++-Xclang+-analyzer-checker%3Doptin.taint.TaintPropagation+++-Xclang+-analyzer-checker%3Doptin.taint.TaintedAlloc%0A'),l:'5',n:'0',o:'C+source+%231',t:'0')),header:(),k:34.71001224185383,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:cclang_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'-Xclang+-analyzer-checker%3Dcore++-Xclang+-analyzer-checker%3Dalpha.deadcode.UnreachableCode++-Xclang++-analyzer-checker%3Dalpha.core.CastSize+++-Xclang+-analyzer-checker%3Dunix++-Xclang+-analyzer-checker%3Ddebug.ExprInspection+++-Xclang+-analyzer-checker%3Doptin.taint.TaintPropagation+++-Xclang+-analyzer-checker%3Doptin.taint.TaintedAlloc',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),header:(),k:35.652365515388325,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+20.1.0',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+(trunk)+(Compiler+%231)',t:'0')),k:29.637622242757846,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4).
In this example in line 12 the warning is generated, however the C99 standard clearly says:
" If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object."
And clang implementation is to return NULL pointer in such case.
Thus the warning should be consistent with clang implementation currently existing and not generate warning when realloc() is used in lieu of free().
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWF2v2j7S_zTmxiJKHELCBRckp0eqVLWV2j7P3iE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