Indexing into a vector past its end is UB.#813
Merged
axic merged 1 commit intowasmx:masterfrom May 5, 2022
Merged
Conversation
Codecov Report
@@ Coverage Diff @@
## master #813 +/- ##
==========================================
- Coverage 99.00% 98.92% -0.08%
==========================================
Files 81 80 -1
Lines 12845 11834 -1011
==========================================
- Hits 12717 11707 -1010
+ Misses 128 127 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
63c9a28 to
e98428c
Compare
chfast
approved these changes
Apr 24, 2022
axic
approved these changes
Apr 27, 2022
Member
|
Thank you @graydon, we'll try to get this merged soon. |
gumb0
reviewed
Apr 28, 2022
Collaborator
gumb0
left a comment
There was a problem hiding this comment.
Nice investigation, thank you.
I have only one small cosemtic change request.
lib/fizzy/execute.cpp
Outdated
Comment on lines
+1566
to
+1567
| assert(pc == code.instructions.data() + code.instructions.size()); // End of code must be | ||
| // reached. |
Collaborator
There was a problem hiding this comment.
Nit:
Suggested change
| assert(pc == code.instructions.data() + code.instructions.size()); // End of code must be | |
| // reached. | |
| // End of code must be reached. | |
| assert(pc == code.instructions.data() + code.instructions.size()); |
gumb0
approved these changes
May 5, 2022
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.
Even if all you're doing with the resulting reference is deriving a pointer to one-past-the-end.
Or it might be UB. Unclear.
At least: according to cplusplus.com calling operator[] on a vector with an out-of-range argument is UB.
UBSan does not report anything, however. Or rather: it reports UB if you form a reference to
v[v.size()]for an empty vectorv, but not for a nonempty vectorv. Go figure.Building with
-stdlib=libc++and-D_LIBCPP_DEBUG=1produces a runtime error_LIBCPP_ASSERT '__n < size()' failed. vector[] index out of boundsfor empty or nonempty vectors.In the C++ spec section 1.3.2 [dcl.ref] it says that "A reference shall be initialized to refer to a valid object". Furthermore table 88 defines
a[n]as meaning*(a.begin() + n)on vector, and 26.3.11.4 [vector.data] says that only the range[data(), data() + size())is a valid range. But none of this explicitly says it's UB to initialize a reference to the one-past-the-end position. Just that it's not included in the ways that a reference "shall" be initialized.So .. it is not clear to me by reading the spec whether it is UB merely to have called the operator (and thereby formed a reference to an element one-past-the-end) in order to then derive a pointer to that referenced element, but not dereferenced it. However, one can form the pointer in question without having to answer this question using the
data() + size()idiom used elsewhere in this code, so that's what this PR does.