Skip to content

Commit 9271183

Browse files
committed
Fix the basic_static_cstring array ctor to respect the no-embedded-NULs invariant
The constructor taking a const CharT (&)[M] copied M - 1 characters verbatim via assign(arr, M - 1), preserving any embedded NULs in the input. This would violate the type's documented "no embedded NULs" invariant. const char arr[] = { '1', '2', '\0', '4', '5', '\0' }; static_cstring<5> s(arr); // data_ holds {'1','2','\0','4','5','\0'} --- embedded NUL stored Delegate to assign(arr) instead, which uses traits::length() to determine the size. This matches the behavior of the other CharT-accepting entry points (assign(const CharT*), the const CharT* constructor) and makes the invariant self-enforcing at construction. Note: In practice, this is a bug that would never surface, because the array constructor is never chosen for static_cstring<N> s(arr)---the pointer constructor always wins---but the array constructor is needed for CTAD (deduction guide), so it better be correct. This also means the test we added is, at the moment, superfluous.
1 parent 1c3143c commit 9271183

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

example/static_cstring/static_cstring.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class basic_static_cstring
123123
constexpr basic_static_cstring(const CharT (&arr)[M])
124124
{
125125
static_assert(M <= N + 1, "Array too big for static_cstring");
126-
assign(arr, M - 1);
126+
assign(arr);
127127
}
128128

129129
constexpr size_type size() const noexcept

example/static_cstring/static_cstring_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ testCStringConstruct()
154154
BOOST_TEST(*s1.end() == 0);
155155
}
156156

157+
// Construct from a C string with embedded NULs.
158+
{
159+
const char arr[] = { '1', '2', '\0', '4', '5', '\0' };
160+
static_cstring<5> s1(arr);
161+
BOOST_TEST(s1 == "12" );
162+
}
163+
157164
// Copy construction.
158165
{
159166
static_cstring<5> s1("12345");

0 commit comments

Comments
 (0)