Skip to content

Commit 937643b

Browse files
authored
[libc++][test] Fixes constexpr char_traits. (#90981)
The issue in nasty_char_traits was discovered by @StephanTLavavej who provided the solution they use in MSVC STL. This solution is based on that example. The same issue affects the constexpr_char_traits which was discovered in #88389. This uses the same fix. Fixes: #74221
1 parent 9e1a49c commit 937643b

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

libcxx/test/support/constexpr_char_traits.h

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@
1616

1717
#include "test_macros.h"
1818

19+
// Tests whether the pointer p is in the range [first, last).
20+
//
21+
// Precondition: The range [first, last) is a valid range.
22+
//
23+
// Typically the pointers are compared with less than. This is not allowed when
24+
// the pointers belong to different ranges, which is UB. Typically, this is
25+
// benign at run-time, however since UB is not allowed during constant
26+
// evaluation this does not compile. This function does the validation without
27+
// UB.
28+
//
29+
// When p is in the range [first, last) the data can be copied from the
30+
// beginning to the end. Otherwise it needs to be copied from the end to the
31+
// beginning.
32+
template <class CharT>
33+
TEST_CONSTEXPR_CXX14 bool is_pointer_in_range(const CharT* first, const CharT* last, const CharT* p) {
34+
if (first == p) // Needed when n == 0
35+
return true;
36+
37+
for (; first != last; ++first)
38+
if (first == p)
39+
return true;
40+
41+
return false;
42+
}
43+
1944
template <class CharT>
2045
struct constexpr_char_traits
2146
{
@@ -98,23 +123,21 @@ constexpr_char_traits<CharT>::find(const char_type* s, std::size_t n, const char
98123
}
99124

100125
template <class CharT>
101-
TEST_CONSTEXPR_CXX14 CharT*
102-
constexpr_char_traits<CharT>::move(char_type* s1, const char_type* s2, std::size_t n)
103-
{
104-
char_type* r = s1;
105-
if (s1 < s2)
106-
{
107-
for (; n; --n, ++s1, ++s2)
108-
assign(*s1, *s2);
109-
}
110-
else if (s2 < s1)
111-
{
112-
s1 += n;
113-
s2 += n;
114-
for (; n; --n)
115-
assign(*--s1, *--s2);
116-
}
117-
return r;
126+
TEST_CONSTEXPR_CXX14 CharT* constexpr_char_traits<CharT>::move(char_type* s1, const char_type* s2, std::size_t n) {
127+
if (s1 == s2)
128+
return s1;
129+
130+
char_type* r = s1;
131+
if (is_pointer_in_range(s1, s1 + n, s2)) {
132+
for (; n; --n)
133+
assign(*s1++, *s2++);
134+
} else {
135+
s1 += n;
136+
s2 += n;
137+
for (; n; --n)
138+
assign(*--s1, *--s2);
139+
}
140+
return r;
118141
}
119142

120143
template <class CharT>

libcxx/test/support/nasty_string.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "make_string.h"
1818
#include "test_macros.h"
19+
#include "constexpr_char_traits.h" // is_pointer_in_range
1920

2021
// This defines a nasty_string similar to nasty_containers. This string's
2122
// value_type does operator hijacking, which allows us to ensure that the
@@ -118,11 +119,14 @@ constexpr const nasty_char* nasty_char_traits::find(const nasty_char* s, std::si
118119
}
119120

120121
constexpr nasty_char* nasty_char_traits::move(nasty_char* s1, const nasty_char* s2, std::size_t n) {
122+
if (s1 == s2)
123+
return s1;
124+
121125
nasty_char* r = s1;
122-
if (s1 < s2) {
123-
for (; n; --n, ++s1, ++s2)
124-
assign(*s1, *s2);
125-
} else if (s2 < s1) {
126+
if (is_pointer_in_range(s1, s1 + n, s2)) {
127+
for (; n; --n)
128+
assign(*s1++, *s2++);
129+
} else {
126130
s1 += n;
127131
s2 += n;
128132
for (; n; --n)

0 commit comments

Comments
 (0)