Skip to content

Commit 5f63382

Browse files
vector_algorithms.cpp: fix incorrect activation of bitmap find-meow-of algorithms (#5758)
Co-authored-by: Stephan T. Lavavej <stl@microsoft.com>
1 parent 0803ae6 commit 5f63382

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

stl/src/vector_algorithms.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,16 +3991,20 @@ namespace {
39913991

39923992
const size_t _Byte_size = _Needle_length * sizeof(_Ty);
39933993

3994+
constexpr size_t _Vec_size = sizeof(_Mask);
3995+
constexpr size_t _Vec_mask = _Vec_size - 1;
3996+
static_assert((_Vec_size & _Vec_mask) == 0);
3997+
39943998
const void* _Stop = _Needle_ptr;
3995-
_Advance_bytes(_Stop, _Byte_size & ~size_t{0x1F});
3996-
for (; _Needle_ptr != _Stop; _Needle_ptr += 32 / sizeof(_Ty)) {
3999+
_Advance_bytes(_Stop, _Byte_size & ~_Vec_mask);
4000+
for (; _Needle_ptr != _Stop; _Needle_ptr += _Vec_size / sizeof(_Ty)) {
39974001
const __m128i _Data = _mm_loadu_si128(reinterpret_cast<const __m128i*>(_Needle_ptr));
39984002
if (!_mm_testz_si128(_Mask, _Data)) {
39994003
return false;
40004004
}
40014005
}
40024006

4003-
_Advance_bytes(_Stop, _Byte_size & 0x1E);
4007+
_Advance_bytes(_Stop, _Byte_size & _Vec_mask);
40044008
for (; _Needle_ptr != _Stop; ++_Needle_ptr) {
40054009
if ((*_Needle_ptr & ~_Ty{0xFF}) != 0) {
40064010
return false;

tests/std/tests/VSO_0000000_vector_algorithms/test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,26 @@ void test_basic_string(mt19937_64& gen) {
17101710
}
17111711
}
17121712

1713+
// GH-5757 <string>: wstring::find_first_of crashes on some inputs
1714+
void test_gh_5757_find_first_of() {
1715+
const wstring hay(40, L'e');
1716+
wstring needle;
1717+
needle.resize(32);
1718+
1719+
for (unsigned int k = 0; k != 32; k += 16) {
1720+
for (unsigned int i = 0; i != 8; ++i) {
1721+
needle[i + k] = static_cast<wchar_t>(L'a' + i);
1722+
}
1723+
1724+
for (unsigned int i = 8; i != 16; ++i) {
1725+
needle[i + k] = static_cast<wchar_t>(0xFF00 + i);
1726+
}
1727+
}
1728+
1729+
const auto pos = hay.find_first_of(needle);
1730+
assert(pos == 0);
1731+
}
1732+
17131733
void test_string(mt19937_64& gen) {
17141734
test_basic_string<char>(gen);
17151735
test_basic_string<wchar_t>(gen);
@@ -1719,6 +1739,8 @@ void test_string(mt19937_64& gen) {
17191739
test_basic_string<char16_t>(gen);
17201740
test_basic_string<char32_t>(gen);
17211741
test_basic_string<unsigned long long>(gen);
1742+
1743+
test_gh_5757_find_first_of();
17221744
}
17231745

17241746
void test_various_containers() {

0 commit comments

Comments
 (0)