Skip to content

Commit cf4c140

Browse files
committed
Add assert tests for preconditions
1 parent 8ddc5ab commit cf4c140

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

libcxx/include/__text_encoding/te_impl.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ struct __te_impl {
359359
}
360360

361361
_LIBCPP_HIDE_FROM_ABI static constexpr const __te_data* __find_encoding_data(string_view __a) {
362-
_LIBCPP_ASSERT(__a.size() <= __max_name_length_, "Passing encoding name longer than max_name_length!");
362+
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
363+
__a.size() <= __max_name_length_ && !__a.contains('\0'), "invalid string passed to text_encoding(string_view)");
363364
const __te_data* __data_first = __text_encoding_data + 2;
364365
const __te_data* __data_last = std::end(__text_encoding_data);
365366

@@ -377,8 +378,9 @@ struct __te_impl {
377378
}
378379

379380
_LIBCPP_HIDE_FROM_ABI static constexpr const __te_data* __find_encoding_data_by_id(__id __i) {
380-
_LIBCPP_ASSERT(__i >= __id::other && __i <= __id::CP50220 && int_least32_t(__i) != 33 && int_least32_t(__i) != 34,
381-
"Passing invalid id to text_encoding constructor!");
381+
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
382+
((__i >= __id::other && __i <= __id::CP50220) && ((int_least32_t(__i) != 33) && (int_least32_t(__i) != 34))),
383+
"invalid text_encoding::id passed to text_encoding(id)");
382384
auto __found =
383385
std::lower_bound(std::begin(__text_encoding_data), std::end(__text_encoding_data), int_least32_t(__i));
384386

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++26
10+
// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
11+
12+
// <text_encoding>
13+
14+
// text_encoding text_encoding(id)
15+
16+
#include <climits>
17+
#include <text_encoding>
18+
19+
#include "check_assertion.h"
20+
21+
int main(int, char**) {
22+
// Make sure that text_encoding(id) asserts when the input id not in range of allowed values
23+
24+
TEST_LIBCPP_ASSERT_FAILURE(
25+
std::text_encoding(std::text_encoding::id(33)), "invalid text_encoding::id passed to text_encoding(id)");
26+
TEST_LIBCPP_ASSERT_FAILURE(
27+
std::text_encoding(std::text_encoding::id(34)), "invalid text_encoding::id passed to text_encoding(id)");
28+
TEST_LIBCPP_ASSERT_FAILURE(
29+
std::text_encoding(std::text_encoding::id(-1)), "invalid text_encoding::id passed to text_encoding(id)");
30+
TEST_LIBCPP_ASSERT_FAILURE(
31+
std::text_encoding(std::text_encoding::id(INT_MAX)), "invalid text_encoding::id passed to text_encoding(id)");
32+
33+
return 0;
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++26
10+
// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
11+
12+
// <text_encoding>
13+
14+
// text_encoding text_encoding(string_view)
15+
16+
#include <string_view>
17+
#include <text_encoding>
18+
19+
#include "check_assertion.h"
20+
21+
int main(int, char**) {
22+
char str[std::text_encoding::max_name_length + 2] =
23+
"HELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELL";
24+
std::string_view view(str);
25+
26+
// text_encoding(string_view) asserts if its input size() > max_name_length
27+
TEST_LIBCPP_ASSERT_FAILURE(std::text_encoding(view), "invalid string passed to text_encoding(string_view)");
28+
29+
// text_encoding(string_view) asserts if its input contains a null terminator
30+
char str2[std::text_encoding::max_name_length] = "HELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELL";
31+
std::string_view view2(str2);
32+
str2[3] = '\0';
33+
34+
TEST_LIBCPP_ASSERT_FAILURE(std::text_encoding(view2), "invalid string passed to text_encoding(string_view)");
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)