-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Description
(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?