baloghadamsoftware created this revision. baloghadamsoftware added reviewers: gribozavr2, aaron.ballman. baloghadamsoftware added a project: clang-tools-extra. Herald added subscribers: martong, steakhal, gamesh411, Szelethus, dkrupp, rnkovacs, xazax.hun, whisperity. Herald added a project: clang. baloghadamsoftware requested review of this revision.
Placement new operators on non-object types cause crash in `bugprone-misplaced-pointer-arithmetic-in-alloc`. This patch fixes this issue. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D87683 Files: clang-tools-extra/clang-tidy/bugprone/MisplacedPointerArithmeticInAllocCheck.cpp clang-tools-extra/test/clang-tidy/checkers/bugprone-misplaced-pointer-arithmetic-in-alloc.cpp Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-misplaced-pointer-arithmetic-in-alloc.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/bugprone-misplaced-pointer-arithmetic-in-alloc.cpp +++ clang-tools-extra/test/clang-tidy/checkers/bugprone-misplaced-pointer-arithmetic-in-alloc.cpp @@ -51,3 +51,10 @@ // CHECK-FIXES: p = new char[n - m] + 10; // FIXME: should be p = new char[n - m + 10]; } + +void* operator new(unsigned long, void*); + +void placement_new_ptr(void *buf, C *old) { + C **p = new (buf) C*(old) + 1; + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:11: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument +} Index: clang-tools-extra/clang-tidy/bugprone/MisplacedPointerArithmeticInAllocCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/MisplacedPointerArithmeticInAllocCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/MisplacedPointerArithmeticInAllocCheck.cpp @@ -77,9 +77,9 @@ CallName = "operator new[]"; } else { const auto *CtrE = New->getConstructExpr(); - if (!CtrE->getArg(CtrE->getNumArgs() - 1) - ->getType() - ->isIntegralOrEnumerationType()) + if (!CtrE || !CtrE->getArg(CtrE->getNumArgs() - 1) + ->getType() + ->isIntegralOrEnumerationType()) return; CallName = "operator new"; }
Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-misplaced-pointer-arithmetic-in-alloc.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/bugprone-misplaced-pointer-arithmetic-in-alloc.cpp +++ clang-tools-extra/test/clang-tidy/checkers/bugprone-misplaced-pointer-arithmetic-in-alloc.cpp @@ -51,3 +51,10 @@ // CHECK-FIXES: p = new char[n - m] + 10; // FIXME: should be p = new char[n - m + 10]; } + +void* operator new(unsigned long, void*); + +void placement_new_ptr(void *buf, C *old) { + C **p = new (buf) C*(old) + 1; + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:11: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument +} Index: clang-tools-extra/clang-tidy/bugprone/MisplacedPointerArithmeticInAllocCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/MisplacedPointerArithmeticInAllocCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/MisplacedPointerArithmeticInAllocCheck.cpp @@ -77,9 +77,9 @@ CallName = "operator new[]"; } else { const auto *CtrE = New->getConstructExpr(); - if (!CtrE->getArg(CtrE->getNumArgs() - 1) - ->getType() - ->isIntegralOrEnumerationType()) + if (!CtrE || !CtrE->getArg(CtrE->getNumArgs() - 1) + ->getType() + ->isIntegralOrEnumerationType()) return; CallName = "operator new"; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits