-
Notifications
You must be signed in to change notification settings - Fork 38
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
Indexing into a vector past its end is UB. #813
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
Thank you @graydon, we'll try to get this merged soon. |
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.
Nice investigation, thank you.
I have only one small cosemtic change request.
lib/fizzy/execute.cpp
Outdated
assert(pc == code.instructions.data() + code.instructions.size()); // End of code must be | ||
// reached. |
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.
Nit:
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()); |
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=1
produces a runtime error_LIBCPP_ASSERT '__n < size()' failed. vector[] index out of bounds
for 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.