Skip to content

Commit fd6db23

Browse files
committed
Updated comments in test, added MSVC related links, and dropped the dangling comment
1 parent b93bd2c commit fd6db23

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ bool PlacementNewChecker::checkPlaceCapacityIsSufficient(
117117
llvm::formatv("Storage provided to placement new is only {0} bytes, "
118118
"whereas the allocated type requires {1} bytes",
119119
SizeOfPlaceCI->getValue(), SizeOfTargetCI->getValue());
120-
// TODO: use clang constants
121120

122121
auto R = std::make_unique<PathSensitiveBugReport>(SBT, Msg, N);
123122
bugreporter::trackExpressionValue(N, NE->getPlacementArg(0), *R);

clang/test/Analysis/placement-new.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,41 @@ void f1() {
166166
short a;
167167
};
168168

169-
// on some systems, placement array new may allocate more memory than the nominal size of the array
170-
// in such cases, test code could be problematic, but the checker doesn't warn here because this behavior is expected to be rare
169+
// On some systems, (notably before MSVC 16.7), a non-allocating placement
170+
// array new could allocate more memory than the nominal size of the array.
171+
172+
// Since CWG 2382 (implemented in MSVC 16.7), overhead was disallowed for non-allocating placement new.
173+
// See:
174+
// https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-170
175+
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1969r0.html#2382
176+
177+
// However, as of 17.1, there is a regression when the type comes from a template
178+
// parameter where MSVC reintroduces overhead.
179+
// See:
180+
// https://developercommunity.visualstudio.com/t/10777485
181+
// https://godbolt.org/z/E1z1Tsfvj
182+
183+
// The checker doesn't warn here because this behavior only affects older
184+
// MSVC versions (<16.7) or certain specific versions (17.1).
185+
// Suppressing warnings avoids false positives on standard-compliant compilers
186+
// and modern MSVC versions, but users of affected MSVC versions should be
187+
// aware of potential buffer size issues.
188+
171189
const unsigned N = 32;
172190
alignas(S) unsigned char buffer1[sizeof(S) * N];
173-
::new (buffer1) S[N];
191+
::new (buffer1) S[N]; // no-warning: See comments above
174192
}
175193

176194
void f2() {
177195
struct S {
178196
short a;
179197
};
180198

181-
// on some systems, placement array new may allocate more memory than the nominal size of the array
182-
// in such cases, test code could be problematic, but the checker doesn't warn here because this behavior is expected to be rare
199+
// On some systems, placement array new could allocate more memory than the nominal size of the array.
200+
// See the comment at f1() above for more details.
183201
const unsigned N = 32;
184202
alignas(S) unsigned char buffer2[sizeof(S) * N + sizeof(int)];
185-
::new (buffer2) S[N];
203+
::new (buffer2) S[N]; // no-warning: See comments above
186204
}
187205
} // namespace testArrayTypesAllocation
188206

0 commit comments

Comments
 (0)