Skip to content

[-Wunsafe-buffer-usage] Fix a small bug recently found #102953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions clang/lib/Analysis/UnsafeBufferUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2659,7 +2659,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
Expand Down Expand Up @@ -2792,9 +2792,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 {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Loading