Skip to content

Commit 37bd788

Browse files
authored
[Clang][Sema] Fix templated array size calculation. (#96464)
The [last attempt](#89036) to fix #41441 has been reverted immediately. Here I'm trying the simplest idea I've been able to come with: skip handling dependent case in `BuildCXXNew`. The original test (borrowed form #89036) passes. Also I've created and added to the tests a minimal repro of the code #89036 fails on. This (obviously) also passes.
1 parent 51d0e40 commit 37bd788

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ Bug Fixes to C++ Support
515515
- Fixed overload handling for object parameters with top-level cv-qualifiers in explicit member functions (#GH100394)
516516
- Fixed a bug in lambda captures where ``constexpr`` class-type objects were not properly considered ODR-used in
517517
certain situations. (#GH47400), (#GH90896)
518+
- Fix erroneous templated array size calculation leading to crashes in generated code. (#GH41441)
518519

519520
Bug Fixes to AST Handling
520521
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,8 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
21572157

21582158
// Per C++0x [expr.new]p5, the type being constructed may be a
21592159
// typedef of an array type.
2160-
if (!ArraySize) {
2160+
// Dependent case will be handled separately.
2161+
if (!ArraySize && !AllocType->isDependentType()) {
21612162
if (const ConstantArrayType *Array
21622163
= Context.getAsConstantArrayType(AllocType)) {
21632164
ArraySize = IntegerLiteral::Create(Context, Array->getSize(),

clang/test/SemaCXX/GH41441.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %clang --target=x86_64-pc-linux -S -fno-discard-value-names -emit-llvm -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 %s -fsyntax-only -verify
3+
4+
namespace std {
5+
using size_t = decltype(sizeof(int));
6+
};
7+
void* operator new[](std::size_t, void*) noexcept;
8+
9+
// CHECK: call void @llvm.memset.p0.i64(ptr align 1 %x, i8 0, i64 8, i1 false)
10+
// CHECK: call void @llvm.memset.p0.i64(ptr align 16 %x, i8 0, i64 32, i1 false)
11+
template <typename TYPE>
12+
void f()
13+
{
14+
typedef TYPE TArray[8];
15+
16+
TArray x;
17+
new(&x) TArray();
18+
}
19+
20+
template <typename T>
21+
void f1() {
22+
int (*x)[1] = new int[1][1];
23+
}
24+
template void f1<char>();
25+
void f2() {
26+
int (*x)[1] = new int[1][1];
27+
}
28+
29+
int main()
30+
{
31+
f<char>();
32+
f<int>();
33+
}
34+
35+
// expected-no-diagnostics
36+
template <typename T> struct unique_ptr {unique_ptr(T* p){}};
37+
38+
template <typename T>
39+
unique_ptr<T> make_unique(unsigned long long n) {
40+
return unique_ptr<T>(new T[n]());
41+
}
42+
43+
auto boro(int n){
44+
typedef double HistoryBuffer[4];
45+
return make_unique<HistoryBuffer>(n);
46+
}

0 commit comments

Comments
 (0)