[clang] 9aa3b53 - [-Wunsafe-buffer-usage] Fix a small bug recently found (#102953)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 15 12:34:18 PDT 2024
Author: Ziqing Luo
Date: 2024-08-15T12:34:15-07:00
New Revision: 9aa3b53ac52167aba6253a52805874948ce40c8f
URL: https://github.com/llvm/llvm-project/commit/9aa3b53ac52167aba6253a52805874948ce40c8f
DIFF: https://github.com/llvm/llvm-project/commit/9aa3b53ac52167aba6253a52805874948ce40c8f.diff
LOG: [-Wunsafe-buffer-usage] Fix a small bug recently found (#102953)
`QualType::isConstantArrayType()` checks canonical type. So a following
cast should be applied to canonical type as well:
```
if (Ty->isConstantArrayType())
cast<ConstantArrayType>(Ty.getCanonicalType()); // cast<ConstantArrayType>(Ty) is incorrect
```
Added:
Modified:
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage-in-container-span-construct.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 051381edabf0b2..95c288bfe3fd2f 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -402,9 +402,9 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
QualType Arg0Ty = Arg0->IgnoreImplicit()->getType();
- if (Arg0Ty->isConstantArrayType()) {
- const APSInt ConstArrSize =
- APSInt(cast<ConstantArrayType>(Arg0Ty)->getSize());
+ if (auto *ConstArrTy =
+ Finder->getASTContext().getAsConstantArrayType(Arg0Ty)) {
+ const APSInt ConstArrSize = APSInt(ConstArrTy->getSize());
// Check form 4:
return Arg1CV && APSInt::compareValues(ConstArrSize, *Arg1CV) == 0;
@@ -2664,7 +2664,7 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
// Note: the code below expects the declaration to not use any type sugar like
// typedef.
- if (auto CAT = dyn_cast<clang::ConstantArrayType>(D->getType())) {
+ if (auto CAT = Ctx.getAsConstantArrayType(D->getType())) {
const QualType &ArrayEltT = CAT->getElementType();
assert(!ArrayEltT.isNull() && "Trying to fix a non-array type variable!");
// FIXME: support multi-dimensional arrays
@@ -2797,9 +2797,9 @@ fixVariable(const VarDecl *VD, FixitStrategy::Kind K,
return {};
}
case FixitStrategy::Kind::Array: {
- if (VD->isLocalVarDecl() &&
- isa<clang::ConstantArrayType>(VD->getType().getCanonicalType()))
- return fixVariableWithArray(VD, Tracker, Ctx, Handler);
+ if (VD->isLocalVarDecl())
+ if (auto CAT = Ctx.getAsConstantArrayType(VD->getType()))
+ return fixVariableWithArray(VD, Tracker, Ctx, Handler);
DEBUG_NOTE_DECL_FAIL(VD, " : not a local const-size array");
return {};
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-in-container-span-construct.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-in-container-span-construct.cpp
index a1ddc384e0d9b8..e97511593bbd81 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-in-container-span-construct.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-in-container-span-construct.cpp
@@ -79,6 +79,8 @@ namespace construct_wt_ptr_size {
unsigned Y = 10;
std::span<int> S = std::span{&X, 1}; // no-warning
int Arr[10];
+ typedef int TenInts_t[10];
+ TenInts_t Arr2;
S = std::span{&X, 2}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}}
S = std::span{new int[10], 10}; // no-warning
@@ -90,6 +92,7 @@ namespace construct_wt_ptr_size {
S = std::span{new int[10], 9}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} // not smart enough to tell its safe
S = std::span{new int[10], Y}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} // not smart enough to tell its safe
S = std::span{Arr, 10}; // no-warning
+ S = std::span{Arr2, 10}; // no-warning
S = std::span{Arr, Y}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} // not smart enough to tell its safe
S = std::span{p, 0}; // no-warning
}
More information about the cfe-commits
mailing list