[DoNotMerge][CodeGen] Fix missing bounds check on ++/-- of array subscripts - #13563
Draft
rapidsna wants to merge 1 commit into
Draft
[DoNotMerge][CodeGen] Fix missing bounds check on ++/-- of array subscripts#13563rapidsna wants to merge 1 commit into
rapidsna wants to merge 1 commit into
Conversation
The four inc/dec visitors in ScalarExprEmitter emitted the subexpression's
lvalue via EmitLValue, bypassing EmitCheckedLValue. For an ArraySubscriptExpr,
that emits the ASE with Accessed=false, which produces two different bugs:
* -fsanitize=array-bounds: the compare degrades from index < N to
index <= N (EmitBoundsCheckImpl picks ICmpULT/ICmpULE on Accessed),
so the write at index == N slips through.
* -fbounds-safety: the firebloom bounds-check emission in
EmitWidePtrArraySubscriptExpr is fully gated on Accessed, so no
check is emitted at all for ++/--.
Route inc/dec through EmitCheckedLValue(..., TCK_Store), matching what
plain and compound assignment already do. TCK_Store is correct because
inc/dec is a read-modify-write whose store side is the one that needs
the bounds guarantee.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
rdar://174356716
rapidsna
commented
Jul 29, 2026
| @@ -705,19 +705,19 @@ class ScalarExprEmitter | |||
|
|
|||
| // Unary Operators. | |||
| Value *VisitUnaryPostDec(const UnaryOperator *E) { | |||
| LValue LV = EmitLValue(E->getSubExpr()); | |||
| LValue LV = EmitCheckedLValue(E->getSubExpr(), CodeGenFunction::TCK_Store); | |||
Author
There was a problem hiding this comment.
Discussion is needed on how risky it is and whether it needs an extra qualification or gating (like what we did to roll out new bounds checks for -fbounds-safety). My sense is that if we land this early in the milestone, we can piggy back on the regular compiler qualification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The four inc/dec visitors in ScalarExprEmitter emitted the subexpression's lvalue via EmitLValue, bypassing EmitCheckedLValue. For an ArraySubscriptExpr, that emits the ASE with Accessed=false, which produces two different bugs:
-fsanitize=array-bounds: the compare degrades from index < N to index <= N (EmitBoundsCheckImpl picks ICmpULT/ICmpULE on Accessed), so the write at index == N slips through.
-fbounds-safety: the firebloom bounds-check emission in EmitWidePtrArraySubscriptExpr is fully gated on Accessed, so no check is emitted at all for ++/--.
Route inc/dec through EmitCheckedLValue(..., TCK_Store), matching what plain and compound assignment already do. TCK_Store is correct because inc/dec is a read-modify-write whose store side is the one that needs the bounds guarantee.
Co-Authored-By: Claude Opus 4.7 noreply@anthropic.com
rdar://174356716