1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

LibC: Set saved_str to null in strtok_r if no tokens were found

If we do not do this, the next call to strtok_r will start tokenizing
(and possibly modifying!) the memory pointed to by `saved_ptr`.
This commit is contained in:
Daniel Bertalan 2022-02-26 12:53:02 +01:00 committed by Linus Groh
parent 4b4177f39c
commit ea52ba9fdc
2 changed files with 17 additions and 2 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
* Copyright (c) 2022, Daniel Bertalan <dani@danielbertalan.dev>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,3 +17,15 @@ TEST_CASE(strerror_r_basic)
EXPECT_EQ(strerror_r(EFAULT, buf, sizeof(buf)), 0);
EXPECT_EQ(strcmp(buf, "Bad address"), 0);
}
TEST_CASE(strtok_r_delimiters_only)
{
char dummy[] = "a;";
char input[] = ";;;;;;";
char* saved_str = dummy;
EXPECT_EQ(strtok_r(input, ";", &saved_str), nullptr);
EXPECT_EQ(strtok_r(nullptr, ";", &saved_str), nullptr);
// The string to which `saved_str` initially points to shouldn't be modified.
EXPECT_EQ(strcmp(dummy, "a;"), 0);
}