<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/113399>113399</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
new/delete leaks are caught on return, but can they be caught on throw?
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AE1020
</td>
</tr>
</table>
<pre>
*(Note: I'm using Ubuntu clang version 18.1.3, but I also pulled and built 20.0.0 to see if things were different, or if there were any new settings.)*
Leaks of unpaired `new` seem to be caught well so long as you are using `return`:
const char* Leak_IS_Detected_Test(int x) {
bool* guard = new bool;
if (x > 0)
return "This path leaks, and is reported";
delete guard;
return "This path has no leak";
}
> $ clang --analyze caught.cpp
> caught.cpp:4:16: warning: Potential leak of memory pointed to by 'guard' [cplusplus.NewDeleteLeaks]
> 4 | return "This path leaks, and is reported";
But I would also like to catch `new leaks (and/or `malloc()` leaks) in the cases of throws:
const char* Leak_NOT_Detected_Test(int x) {
bool* guard = new bool;
if (x > 0)
throw "This path leaks, but doesn't report";
delete guard;
return "This path has no leak";
}
Is there some fiddling of settings such that this could be considered a leak? I got an AI suggestion to try an alternate path diagnostics mode, but if this was the correct incantation it didn't have any effect:
clang --analyze -Xanalyzer -analyzer-config -Xanalyzer path-diagnostics-alternate=true not-caught.cpp
For what I am doing, I'm not concerned whether it's a literal throw or a function that calls the throw with an attribute. But I've gathered that `noreturn` functions are conservatively assumed to possibly be `exit()`s so they do not generate reports.
If throws cannot be checked, are there other attributes besides `noreturn` which would get the same checks as a return statement in terms of control flow?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VluL4zgT_TXKS5Fgy7k-5CHdmUDgY76FnYV9G2SpYmtHloJUjjv765eSnb7MXlhYGEO7jUqqy6lTR1Ep2cYj7sXqSayOM9VTG-L-8KksZDGrg7nvhTwIuf0cCEV1gLOQmw76ZH0Dv9S9px60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