@@ -436,21 +436,31 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
436
436
437
437
const auto *BaseDRE =
438
438
dyn_cast<DeclRefExpr>(Node.getBase ()->IgnoreParenImpCasts ());
439
- if (!BaseDRE)
440
- return false ;
441
- if (!BaseDRE->getDecl ())
442
- return false ;
443
- const auto *CATy = Finder->getASTContext ().getAsConstantArrayType (
444
- BaseDRE->getDecl ()->getType ());
445
- if (!CATy)
439
+ const auto *SLiteral = dyn_cast<StringLiteral>(Node.getBase ()->IgnoreParenImpCasts ());
440
+ uint64_t size;
441
+
442
+ if (!BaseDRE && !SLiteral)
446
443
return false ;
447
444
445
+ if (BaseDRE) {
446
+ if (!BaseDRE->getDecl ())
447
+ return false ;
448
+ const auto *CATy = Finder->getASTContext ().getAsConstantArrayType (
449
+ BaseDRE->getDecl ()->getType ());
450
+ if (!CATy) {
451
+ return false ;
452
+ }
453
+ size = CATy->getLimitedSize ();
454
+ } else if (SLiteral) {
455
+ size = SLiteral->getLength ();
456
+ }
457
+
448
458
if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx ())) {
449
459
const APInt ArrIdx = IdxLit->getValue ();
450
460
// FIXME: ArrIdx.isNegative() we could immediately emit an error as that's a
451
461
// bug
452
462
if (ArrIdx.isNonNegative () &&
453
- ArrIdx.getLimitedValue () < CATy-> getLimitedSize () )
463
+ ArrIdx.getLimitedValue () < size )
454
464
return true ;
455
465
}
456
466
@@ -1142,7 +1152,7 @@ class ArraySubscriptGadget : public WarningGadget {
1142
1152
// clang-format off
1143
1153
return stmt (arraySubscriptExpr (
1144
1154
hasBase (ignoringParenImpCasts (
1145
- anyOf (hasPointerType (), hasArrayType ()))),
1155
+ anyOf (hasPointerType (), hasArrayType (), stringLiteral () ))),
1146
1156
unless (anyOf (
1147
1157
isSafeArraySubscript (),
1148
1158
hasIndex (
0 commit comments