llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[analyzer] Placement new checker for arrays result in false positive

Open isuckatcs opened this issue 4 years ago • 5 comments

When placement new is used to create and array of elements at a specific location, a warning is falsely reported.

#include <new>

struct s {
  int x;
};

int main() {
  s arr[4];
  new (arr + 1) s[1];
//  ^~~~~~~~~~~
//  warning: 12 bytes is possibly not enough for array allocation which requires 4 bytes. Current overhead requires the // size of 8 bytes [cplusplus.PlacementNew]

  return 0;
}

The warning is only reported in case of arrays with both trunk and 14.0.0 clang. Writing new (arr + 1) s doesn't end up with a warning, but new (arr + 1) s[1], new (arr + 1) s[2], etc. all do. Feel free to check the snippet on godbolt: https://godbolt.org/z/dvz9nqPne. Compile only using --analyze.

isuckatcs avatar Jun 28 '22 10:06 isuckatcs

@llvm/issue-subscribers-clang-static-analyzer

llvmbot avatar Jun 28 '22 10:06 llvmbot

@martong this is your checker!

haoNoQ avatar Jun 29 '22 21:06 haoNoQ

https://reviews.llvm.org/D129280

martong avatar Jul 07 '22 12:07 martong

does not show this since 635e6d765303(https://github.com/llvm/llvm-project/pull/150161) due to bisection

hongtaihu avatar Oct 16 '26 04:10 hongtaihu

I have looked through the issue and the commit in https://github.com/llvm/llvm-project/issues/56264#issuecomment-6208236382. The PR https://github.com/llvm/llvm-project/pull/150161 resolves the underlying issue which led to this ticket as well. https://github.com/llvm/llvm-project/pull/150161 does not add the test case listed in this issue to the test suite, but resolves the bug. I close this as completed.

benedekaibas avatar Oct 28 '26 20:10 benedekaibas