This is an archive of the discontinued LLVM Phabricator instance.

StaticAnalyzer should inline overridden delete operator the same way as overridden new operator
ClosedPublic

Authored by frederic-tingaud-sonarsource on May 3 2022, 5:20 AM.

Details

Summary

Currently, when encountering an overridden new operator, the StaticAnalyzer will inline it when possible, while an overridden delete operator will never be, which leads us to false positives like the following:

struct CustomOperators {
  void *operator new(size_t count) {
    return malloc(count);
  }

  void operator delete(void *addr) {
    free(addr);
  }
};

void compliant() {
  auto *a = new CustomOperators();
  delete a; // warning{{Potential leak of memory pointed to by 'a'}}
}

This patch restores the symmetry between how operator new and operator delete are handled by also inlining the content of operator delete when possible.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2022, 5:20 AM
frederic-tingaud-sonarsource requested review of this revision.May 3 2022, 5:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2022, 5:20 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Looks promising, I am close to accept this, thanks!

clang/include/clang/Analysis/ConstructionContext.h
122–125

I think we could use the variadic isa template .

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
950–958

Could you please also update the config option's documentation at AnalyzerOptions.def?

clang/test/Analysis/cxxnewexpr-callback-inline.cpp
0

It is hard to see how the two test files are different.
Ideally, the two test files cxxnewexpr-callback-inline.cpp and cxxnewexpr-callback-noinline.cpp could be merged into the very same test file with two RUN lines and with different --check-prefix.

However, I see that this is kind of unrelated to this particular change, so, that could be done in a separate, follow-up patch, if you have the time and mood for that.

Applying recommendations

frederic-tingaud-sonarsource marked an inline comment as done.May 5 2022, 8:29 AM
frederic-tingaud-sonarsource added inline comments.
clang/include/clang/Analysis/ConstructionContext.h
122–125

Because assert is a macro, the commas of variadic templates break it.

martong accepted this revision.May 5 2022, 8:41 AM

Thanks for the update, LGTM!

This revision is now accepted and ready to land.May 5 2022, 8:41 AM
frederic-tingaud-sonarsource marked an inline comment as not done.May 5 2022, 8:47 AM

As I don't have merge rights, would it be possible for you to merge it?

This revision was landed with ongoing or failed builds.May 9 2022, 6:48 AM
This revision was automatically updated to reflect the committed changes.
clang/test/Analysis/cxxnewexpr-callback-inline.cpp