-
Notifications
You must be signed in to change notification settings - Fork 351
[-Wunsafe-buffer-usage] Check safe assignment patterns #11680
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6020,6 +6020,60 @@ static bool checkAssignedAndUsed(const BoundsAttributedAssignmentGroup &Group, | |
| return IsGroupSafe; | ||
| } | ||
|
|
||
| // Checks if each assignment to count-attributed pointer in the group is safe. | ||
| static bool | ||
| checkAssignmentPatterns(const BoundsAttributedAssignmentGroup &Group, | ||
| UnsafeBufferUsageHandler &Handler, ASTContext &Ctx) { | ||
| // Collect dependent values. | ||
| DependentValuesTy DependentValues; | ||
| for (size_t I = 0, N = Group.AssignedObjects.size(); I < N; ++I) { | ||
| const ValueDecl *VD = Group.AssignedObjects[I].Decl; | ||
| const auto *Attr = VD->getAttr<DependerDeclsAttr>(); | ||
| if (!Attr) | ||
| continue; | ||
|
|
||
| const BinaryOperator *Assign = Group.Assignments[I]; | ||
| const Expr *Value = Assign->getRHS(); | ||
|
|
||
| [[maybe_unused]] bool Inserted = | ||
| DependentValues.insert({{VD, Attr->getIsDeref()}, Value}).second; | ||
| // Previous checks in `checkBoundsAttributedGroup` should have validated | ||
| // that we have only a single assignment. | ||
| assert(Inserted); | ||
| } | ||
|
|
||
| bool IsGroupSafe = true; | ||
|
|
||
| // Check every pointer in the group. | ||
| for (size_t I = 0, N = Group.AssignedObjects.size(); I < N; ++I) { | ||
| const ValueDecl *VD = Group.AssignedObjects[I].Decl; | ||
|
|
||
| QualType Ty = VD->getType(); | ||
| const auto *CAT = Ty->getAs<CountAttributedType>(); | ||
| if (!CAT && Ty->isPointerType()) | ||
| CAT = Ty->getPointeeType()->getAs<CountAttributedType>(); | ||
| if (!CAT) | ||
| continue; | ||
|
|
||
| const BinaryOperator *Assign = Group.Assignments[I]; | ||
|
|
||
| // TODO: Move this logic to isCountAttributedPointerArgumentSafeImpl. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, the parameters of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will take care of this TODO in my refactoring patch. |
||
| const Expr *CountArg = | ||
| DependentValues.size() == 1 ? DependentValues.begin()->second : nullptr; | ||
|
|
||
| bool IsSafe = isCountAttributedPointerArgumentSafeImpl( | ||
| Ctx, Assign->getRHS(), CountArg, CAT, CAT->getCountExpr(), | ||
| CAT->isCountInBytes(), CAT->isOrNull(), &DependentValues); | ||
| if (!IsSafe) { | ||
| Handler.handleUnsafeCountAttributedPointerAssignment( | ||
| Assign, /*IsRelatedToDecl=*/false, Ctx); | ||
| IsGroupSafe = false; | ||
| } | ||
| } | ||
|
|
||
| return IsGroupSafe; | ||
| } | ||
|
|
||
| // Checks if the bounds-attributed group is safe. This function returns false | ||
| // iff the assignment group is unsafe and diagnostics have been emitted. | ||
| static bool | ||
|
|
@@ -6031,8 +6085,7 @@ checkBoundsAttributedGroup(const BoundsAttributedAssignmentGroup &Group, | |
| return false; | ||
| if (!checkAssignedAndUsed(Group, Handler, Ctx)) | ||
| return false; | ||
| // TODO: Add more checks. | ||
| return true; | ||
| return checkAssignmentPatterns(Group, Handler, Ctx); | ||
| } | ||
|
|
||
| static void | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have tests covering this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, those cases happen when we have a dereference on LHS (
*ptr =). We have tests like this: