@@ -122,16 +122,17 @@ static bool needNTArrayBounds(ConstraintVariable *CV,
122
122
static bool needArrayBounds (Expr *E, ProgramInfo &Info, ASTContext *C) {
123
123
ConstraintResolver CR (Info, C);
124
124
CVarSet ConsVar = CR.getExprConstraintVars (E);
125
+ auto &EnvMap = Info.getConstraints ().getVariables ();
125
126
for (auto CurrCVar : ConsVar) {
126
- if (needArrayBounds (CurrCVar, Info. getConstraints (). getVariables () ))
127
+ if (needArrayBounds (CurrCVar, EnvMap) || needNTArrayBounds (CurrCVar, EnvMap ))
127
128
return true ;
128
129
return false ;
129
130
}
130
131
return false ;
131
132
}
132
133
133
134
static bool needArrayBounds (Decl *D, ProgramInfo &Info, ASTContext *C,
134
- bool IsNtArr = false ) {
135
+ bool IsNtArr) {
135
136
CVarSet ConsVar = Info.getVariable (D, C);
136
137
auto &E = Info.getConstraints ().getVariables ();
137
138
for (auto CurrCVar : ConsVar) {
@@ -143,6 +144,11 @@ static bool needArrayBounds(Decl *D, ProgramInfo &Info, ASTContext *C,
143
144
return false ;
144
145
}
145
146
147
+ static bool needArrayBounds (Decl *D, ProgramInfo &Info, ASTContext *C) {
148
+ return needArrayBounds (D, Info, C, false ) ||
149
+ needArrayBounds (D, Info, C, true );
150
+ }
151
+
146
152
// Map that contains association of allocator functions and indexes of
147
153
// parameters that correspond to the size of the object being assigned.
148
154
static std::map<std::string, std::set<unsigned >> AllocatorSizeAssoc = {
@@ -440,14 +446,14 @@ bool GlobalABVisitor::VisitFunctionDecl(FunctionDecl *FD) {
440
446
// Here, we are using heuristics. So we only use heuristics when
441
447
// there are no bounds already computed.
442
448
if (!ABInfo.getBounds (PK)) {
443
- if (needArrayBounds (PVD, Info, Context)) {
444
- // Is this an array?
445
- ParamArrays[i] = PVal;
446
- }
447
449
if (needArrayBounds (PVD, Info, Context, true )) {
448
450
// Is this an NTArray?
449
451
ParamNtArrays[i] = PVal;
450
452
}
453
+ if (needArrayBounds (PVD, Info, Context, false )) {
454
+ // Is this an array?
455
+ ParamArrays[i] = PVal;
456
+ }
451
457
}
452
458
453
459
// If this is a length field?
@@ -530,6 +536,21 @@ bool GlobalABVisitor::VisitFunctionDecl(FunctionDecl *FD) {
530
536
return true ;
531
537
}
532
538
539
+ void LocalVarABVisitor::handleAssignment (BoundsKey LK, QualType LHSType, Expr *RHS) {
540
+ auto &ABoundsInfo = Info.getABoundsInfo ();
541
+ handleAllocatorCall (LHSType, LK, RHS, Info, Context);
542
+ clang::StringLiteral *SL =
543
+ dyn_cast_or_null<clang::StringLiteral>(RHS->IgnoreParenCasts ());
544
+ if (SL != nullptr ) {
545
+ ABounds *ByBounds =
546
+ new ByteBound (ABoundsInfo.getConstKey (SL->getByteLength ()));
547
+ if (!ABoundsInfo.mergeBounds (LK, Allocator, ByBounds)) {
548
+ delete (ByBounds);
549
+ } else {
550
+ ABoundsInfo.getBStats ().AllocatorMatch .insert (LK);
551
+ }
552
+ }
553
+ }
533
554
534
555
bool LocalVarABVisitor::HandleBinAssign (BinaryOperator *O) {
535
556
Expr *LHS = O->getLHS ()->IgnoreParenCasts ();
@@ -541,7 +562,7 @@ bool LocalVarABVisitor::HandleBinAssign(BinaryOperator *O) {
541
562
// is the RHS expression a call to allocator function?
542
563
if (needArrayBounds (LHS, Info, Context) &&
543
564
tryGetBoundsKeyVar (LHS, LK, Info, Context)) {
544
- handleAllocatorCall ( LHS->getType (), LK, RHS, Info, Context );
565
+ handleAssignment (LK, LHS->getType (), RHS);
545
566
}
546
567
547
568
// Any parameter directly used as a condition in ternary expression
@@ -606,23 +627,9 @@ bool LocalVarABVisitor::VisitDeclStmt(DeclStmt *S) {
606
627
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
607
628
Expr *InitE = VD->getInit ();
608
629
BoundsKey DeclKey;
609
- if (InitE != nullptr && (needArrayBounds (VD, Info, Context) ||
610
- needArrayBounds (VD, Info, Context, true ))) {
611
- clang::StringLiteral *SL =
612
- dyn_cast<clang::StringLiteral>(InitE->IgnoreParenCasts ());
613
- if (tryGetBoundsKeyVar (VD, DeclKey, Info, Context)) {
614
- handleAllocatorCall (VD->getType (), DeclKey, InitE,
615
- Info, Context);
616
- if (SL != nullptr ) {
617
- ABounds *ByBounds =
618
- new ByteBound (ABoundsInfo.getConstKey (SL->getByteLength ()));
619
- if (!ABoundsInfo.mergeBounds (DeclKey, Allocator, ByBounds)) {
620
- delete (ByBounds);
621
- } else {
622
- ABoundsInfo.getBStats ().AllocatorMatch .insert (DeclKey);
623
- }
624
- }
625
- }
630
+ if (InitE != nullptr && needArrayBounds (VD, Info, Context) &&
631
+ tryGetBoundsKeyVar (VD, DeclKey, Info, Context)) {
632
+ handleAssignment (DeclKey, VD->getType (), InitE);
626
633
}
627
634
}
628
635
0 commit comments