boostorg / locale

Boost.Locale
Boost Software License 1.0
32 stars 70 forks source link

Do not try to access element when vector is empty #60

Closed OznOg closed 2 years ago

OznOg commented 3 years ago

Trying to access tmp[0] causes a crash on Fedora when assertion on STL are enabled.

/usr/include/c++/10/bits/stl_vector.h:1045: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = unsigned char; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::reference = unsigned char&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]: Assertion '__builtin_expect(__n < this->size(), true)' failed.

This patch just passes nullptr as pointer to getSortKey() when tmp size is 0, preventing dereferencing elements in empty vector.

I guess that &tmp[0] should be optimized as 'no real access' when disabling assertion, but actually leads to crash when assert are enabled.

OznOg commented 3 years ago

updated te commit with the NULL instead of nullptr

OznOg commented 3 years ago

Any update on the subject? do you want me to test any other solution?

OznOg commented 3 years ago

ping

hdu-sdlzx commented 2 years ago

ping @artyom-beilis @jwakely @Flamefire anyone review and merge this PR?

Flamefire commented 2 years ago

At this point I'd just say: If the string is empty return the empty tmp at https://github.com/boostorg/locale/pull/60/files#diff-6b6b0b450f26c89b0a6d407203b2b8cea9c6e661417786980726feed67fb3fa5R93. Avoids this whole issue.

Edit: Scratch that:

Sort key byte arrays are zero-terminated and can be compared using strcmp().

I.e. the result is always at least 1 byte. Opened https://github.com/boostorg/locale/pull/72 for a better fix.

Flamefire commented 2 years ago

Superseded by #72 which also fixes the bug. Thanks for the report and PR!