Closed ddavila0 closed 3 years ago
As lame as it is that apparently they have introduced this small bounds check for vector::operator[]
, technically it was still undefined behavior before (even when it still worked) to access buf[n]
with n > buf.size()
, and buf.reserve(n)
left size at 0.
Which is to say, even though it's annoying to have to do the initialization with buf.resize(n)
, this change is technically more correct, even for pre-el8 compilers.
Not going to have time to review - please merge without me.
When using reserve for allocating memory in a vector, the size of the vector (meaning the number of elements in the vector) remains as 0. Then when the we use the "[]" operator (line 154) a new assert, introduced in el8, is done to verify that the vector isn't empty and given that it is empty the execution exits with the following error[*].
This can be fixed by using "resize" instead of "reserve" which apart from allocating memory, it sets a default value, thus affecting the size of the vector.
[*] /usr/include/c++/8/bits/stl_vector.h:932: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = char; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::reference = char&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]: Assertion '__builtin_expect(__n < this->size(), true)' failed.