-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Wunsafe-buffer-usage] Address some false positives in handling array indices that are decidably correct #117370
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 5 commits
8fed333
35207ea
f534ebe
3cdadc6
2ffd0df
c1cfa18
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 |
|---|---|---|
|
|
@@ -439,8 +439,16 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) { | |
| dyn_cast<StringLiteral>(Node.getBase()->IgnoreParenImpCasts()); | ||
| uint64_t size; | ||
|
|
||
| if (!BaseDRE && !SLiteral) | ||
| return false; | ||
| if (!BaseDRE && !SLiteral) { | ||
| // Try harder to find something that looks like a DeclRefExpr | ||
| const auto *Member = dyn_cast<MemberExpr>(Node.getBase()->IgnoreParenImpCasts()); | ||
| if (!Member) return false; | ||
|
|
||
| const auto *Value = Finder->getASTContext().getAsConstantArrayType(Member->getMemberDecl()->getType()); | ||
| if (!Value) return false; | ||
|
|
||
| size = Value->getLimitedSize(); | ||
| } | ||
|
|
||
| if (BaseDRE) { | ||
| if (!BaseDRE->getDecl()) | ||
|
|
@@ -463,6 +471,13 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) { | |
| return true; | ||
|
||
| } | ||
|
|
||
| // Array index wasn't an integer literal, let's see if it was an enum or | ||
| // something similar | ||
| const auto IntConst = Node.getIdx()->getIntegerConstantExpr(Finder->getASTContext()); | ||
| if (IntConst && 0 <= *IntConst && *IntConst < size) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
||
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.
There's a FIXME above about refactoring Sema::CheckArrayAccess to avoid duplication, and I'm trying to decide if that makes sense or not, or if we should just copy the logic.
It seems like the main behavior difference is that
Sema::CheckArrayAccessdoesn't go looking for aDeclwith aConstantArraytype, it just looks at the type of any old expression, which means it doesn't warn on cases involving casts or other expressions with constant array type, like these: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.
I think ultimately going to the Sema code is probably the better place to be, but I'm not sure who's working on that or if they have any urgency to land it. Ergo I'm in favor of landing this and working with the original owner to find the desired end state, as long as you all think that's an acceptable medium state. :)