mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:57:46 +00:00
Everywhere: Explicitly specify the size in StringView constructors
This commit moves the length calculations out to be directly on the StringView users. This is an important step towards the goal of removing StringView(char const*), as it moves the responsibility of calculating the size of the string to the user of the StringView (which will prevent naive uses causing OOB access).
This commit is contained in:
parent
e3da0adfe6
commit
c70f45ff44
75 changed files with 264 additions and 203 deletions
|
@ -31,7 +31,7 @@ static StringView test_default_arg(SourceLocation const& loc = SourceLocation::c
|
|||
TEST_CASE(default_arg_scenario)
|
||||
{
|
||||
auto actual_calling_function = test_default_arg();
|
||||
auto expected_calling_function = StringView(__FUNCTION__);
|
||||
auto expected_calling_function = StringView { __FUNCTION__, strlen(__FUNCTION__) };
|
||||
|
||||
EXPECT_EQ(expected_calling_function, actual_calling_function);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ TEST_CASE(construct_empty)
|
|||
TEST_CASE(view_literal)
|
||||
{
|
||||
char const* truth = "cats rule dogs drool";
|
||||
StringView view(truth);
|
||||
StringView view { truth, strlen(truth) };
|
||||
EXPECT_EQ(view.is_null(), false);
|
||||
EXPECT_EQ(view.characters_without_null_termination(), truth);
|
||||
EXPECT_EQ(view, view);
|
||||
|
|
|
@ -51,33 +51,33 @@ TEST_CASE(decode_utf8)
|
|||
TEST_CASE(validate_invalid_ut8)
|
||||
{
|
||||
size_t valid_bytes;
|
||||
char invalid_utf8_1[] = { 42, 35, (char)182, 9, 0 };
|
||||
Utf8View utf8_1 { StringView { invalid_utf8_1 } };
|
||||
char invalid_utf8_1[] = { 42, 35, (char)182, 9 };
|
||||
Utf8View utf8_1 { StringView { invalid_utf8_1, 4 } };
|
||||
EXPECT(!utf8_1.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 2);
|
||||
|
||||
char invalid_utf8_2[] = { 42, 35, (char)208, (char)208, 0 };
|
||||
Utf8View utf8_2 { StringView { invalid_utf8_2 } };
|
||||
char invalid_utf8_2[] = { 42, 35, (char)208, (char)208 };
|
||||
Utf8View utf8_2 { StringView { invalid_utf8_2, 4 } };
|
||||
EXPECT(!utf8_2.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 2);
|
||||
|
||||
char invalid_utf8_3[] = { (char)208, 0 };
|
||||
Utf8View utf8_3 { StringView { invalid_utf8_3 } };
|
||||
char invalid_utf8_3[] = { (char)208 };
|
||||
Utf8View utf8_3 { StringView { invalid_utf8_3, 1 } };
|
||||
EXPECT(!utf8_3.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 0);
|
||||
|
||||
char invalid_utf8_4[] = { (char)208, 35, 0 };
|
||||
Utf8View utf8_4 { StringView { invalid_utf8_4 } };
|
||||
char invalid_utf8_4[] = { (char)208, 35 };
|
||||
Utf8View utf8_4 { StringView { invalid_utf8_4, 2 } };
|
||||
EXPECT(!utf8_4.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 0);
|
||||
|
||||
char invalid_utf8_5[] = { (char)0xf4, (char)0x8f, (char)0xbf, (char)0xc0, 0 }; // U+110000
|
||||
Utf8View utf8_5 { StringView { invalid_utf8_5 } };
|
||||
char invalid_utf8_5[] = { (char)0xf4, (char)0x8f, (char)0xbf, (char)0xc0 }; // U+110000
|
||||
Utf8View utf8_5 { StringView { invalid_utf8_5, 4 } };
|
||||
EXPECT(!utf8_5.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 0);
|
||||
|
||||
char invalid_utf8_6[] = { (char)0xf4, (char)0xa1, (char)0xb0, (char)0xbd, 0 }; // U+121c3d
|
||||
Utf8View utf8_6 { StringView { invalid_utf8_6 } };
|
||||
char invalid_utf8_6[] = { (char)0xf4, (char)0xa1, (char)0xb0, (char)0xbd }; // U+121c3d
|
||||
Utf8View utf8_6 { StringView { invalid_utf8_6, 4 } };
|
||||
EXPECT(!utf8_6.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 0);
|
||||
}
|
||||
|
@ -122,8 +122,8 @@ TEST_CASE(decode_invalid_ut8)
|
|||
{
|
||||
// Test case 1 : Getting an extension byte as first byte of the code point
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', (char)0xA0, 'd', 0 };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
char raw_data[] = { 'a', 'b', (char)0xA0, 'd' };
|
||||
Utf8View view { StringView { raw_data, 4 } };
|
||||
u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd' };
|
||||
String expected_underlying_bytes[] = { "a", "b", "\xA0", "d" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
@ -140,8 +140,8 @@ TEST_CASE(decode_invalid_ut8)
|
|||
|
||||
// Test case 2 : Getting a non-extension byte when an extension byte is expected
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', (char)0xC0, 'd', 'e', 0 };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
char raw_data[] = { 'a', 'b', (char)0xC0, 'd', 'e' };
|
||||
Utf8View view { StringView { raw_data, 5 } };
|
||||
u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd', 'e' };
|
||||
String expected_underlying_bytes[] = { "a", "b", "\xC0", "d", "e" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
@ -158,8 +158,8 @@ TEST_CASE(decode_invalid_ut8)
|
|||
|
||||
// Test case 3 : Not enough bytes before the end of the string
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', (char)0x90, 'd', 0 };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
char raw_data[] = { 'a', 'b', (char)0x90, 'd' };
|
||||
Utf8View view { StringView { raw_data, 4 } };
|
||||
u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd' };
|
||||
String expected_underlying_bytes[] = { "a", "b", "\x90", "d" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
@ -176,8 +176,8 @@ TEST_CASE(decode_invalid_ut8)
|
|||
|
||||
// Test case 4 : Not enough bytes at the end of the string
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', 'c', (char)0x90, 0 };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
char raw_data[] = { 'a', 'b', 'c', (char)0x90 };
|
||||
Utf8View view { StringView { raw_data, 4 } };
|
||||
u32 expected_characters[] = { 'a', 'b', 'c', 0xFFFD };
|
||||
String expected_underlying_bytes[] = { "a", "b", "c", "\x90" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
|
|
@ -41,7 +41,7 @@ TEST_CASE(asctime)
|
|||
|
||||
time_t epoch = 0;
|
||||
auto result = asctime(localtime(&epoch));
|
||||
EXPECT_EQ(expected_epoch, StringView(result));
|
||||
EXPECT_EQ(expected_epoch, StringView(result, strlen(result)));
|
||||
}
|
||||
|
||||
TEST_CASE(asctime_r)
|
||||
|
@ -51,7 +51,7 @@ TEST_CASE(asctime_r)
|
|||
char buffer[26] {};
|
||||
time_t epoch = 0;
|
||||
auto result = asctime_r(localtime(&epoch), buffer);
|
||||
EXPECT_EQ(expected_epoch, StringView(result));
|
||||
EXPECT_EQ(expected_epoch, StringView(result, strlen(result)));
|
||||
}
|
||||
|
||||
TEST_CASE(ctime)
|
||||
|
@ -61,7 +61,7 @@ TEST_CASE(ctime)
|
|||
time_t epoch = 0;
|
||||
auto result = ctime(&epoch);
|
||||
|
||||
EXPECT_EQ(expected_epoch, StringView(result));
|
||||
EXPECT_EQ(expected_epoch, StringView(result, strlen(result)));
|
||||
}
|
||||
|
||||
TEST_CASE(ctime_r)
|
||||
|
@ -72,7 +72,7 @@ TEST_CASE(ctime_r)
|
|||
time_t epoch = 0;
|
||||
auto result = ctime_r(&epoch, buffer);
|
||||
|
||||
EXPECT_EQ(expected_epoch, StringView(result));
|
||||
EXPECT_EQ(expected_epoch, StringView(result, strlen(result)));
|
||||
}
|
||||
|
||||
TEST_CASE(tzset)
|
||||
|
|
|
@ -47,7 +47,7 @@ TEST_CASE(overlong_realpath)
|
|||
|
||||
// Then, create a long path.
|
||||
StringBuilder expected;
|
||||
expected.append(tmp_dir);
|
||||
expected.append({ tmp_dir, strlen(tmp_dir) });
|
||||
|
||||
// But first, demonstrate the functionality at a reasonable depth:
|
||||
auto expected_str = expected.build();
|
||||
|
@ -63,7 +63,7 @@ TEST_CASE(overlong_realpath)
|
|||
return;
|
||||
}
|
||||
expected.append('/');
|
||||
expected.append(PATH_LOREM_250);
|
||||
expected.append({ PATH_LOREM_250, strlen(PATH_LOREM_250) });
|
||||
ret = chdir(PATH_LOREM_250);
|
||||
if (ret < 0) {
|
||||
perror("chdir iter");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue