Skip to content

Commit

Permalink
chore(developer): merge #11910 into #11894
Browse files Browse the repository at this point in the history
Merge branch 'fix/developer/11814-kmx_u16-buffer-overrun' into fix/developer/correct-use-of-u16chr-when-second-parameter-could-be-null
  • Loading branch information
markcsinclair committed Jul 4, 2024
2 parents 34859c3 + 69d8b30 commit 7efd46f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
6 changes: 3 additions & 3 deletions developer/src/kmcmplib/src/kmx_u16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ KMX_WCHAR * u16tok(KMX_WCHAR *p, const KMX_WCHAR ch, KMX_WCHAR **ctx) {
else {
*ctx = NULL;
}
return p;
return *p ? p : NULL;
}

KMX_WCHAR * u16tok(KMX_WCHAR* p, const KMX_WCHAR* delim, KMX_WCHAR** ctx) {
Expand All @@ -259,13 +259,13 @@ KMX_WCHAR * u16tok(KMX_WCHAR* p, const KMX_WCHAR* delim, KMX_WCHAR** ctx) {
if (*q) {
*q = 0;
q++;
while (u16chr(delim, *q)) q++;
while (*q && u16chr(delim, *q)) q++;
*ctx = q;
}
else {
*ctx = NULL;
}
return p;
return *p ? p : NULL;
}

double u16tof( KMX_WCHAR* str)
Expand Down
52 changes: 51 additions & 1 deletion developer/src/kmcmplib/tests/gtest-kmx_u16-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,54 @@ TEST_F(kmx_u16_Test, u16tok_delim_test) {
EXPECT_EQ(str, u16tok(str, u"|", &ctx));
EXPECT_EQ(0, u16cmp(u"abc", str));
EXPECT_EQ(0, u16cmp(u"def", ctx));
}
}

TEST_F(kmx_u16_Test, u16tok_char_delim) {
// For char delimiter: KMX_WCHAR * u16tok(KMX_WCHAR *p, const KMX_WCHAR ch, KMX_WCHAR **ctx) ;

KMX_WCHAR *ctx = nullptr;
EXPECT_EQ(nullptr, u16tok(nullptr, ' ', &ctx));

KMX_WCHAR buffer[128] = u"test a space and two";
EXPECT_TRUE(!u16cmp(u"test", u16tok(buffer, ' ', &ctx)));
EXPECT_TRUE(!u16cmp(u"a", u16tok(nullptr, ' ', &ctx)));
EXPECT_TRUE(!u16cmp(u"space", u16tok(nullptr, ' ', &ctx)));
EXPECT_TRUE(!u16cmp(u"and", u16tok(nullptr, ' ', &ctx)));
EXPECT_TRUE(!u16cmp(u"two", u16tok(nullptr, ' ', &ctx)));

KMX_WCHAR buffer_space[128] = u" ";
EXPECT_EQ(nullptr, u16tok(buffer_space, ' ', &ctx));
EXPECT_EQ(nullptr, u16tok(nullptr, ' ', &ctx));
}

TEST_F(kmx_u16_Test, u16tok_str_delim) {
// For string delimiter: KMX_WCHAR * u16tok(KMX_WCHAR* p, const KMX_WCHAR* ch, KMX_WCHAR** ctx) ;

KMX_WCHAR *ctx = nullptr;
EXPECT_EQ(nullptr, u16tok(nullptr, u" ", &ctx));

KMX_WCHAR buffer[128] = u"test a space and two";
EXPECT_TRUE(!u16cmp(u"test", u16tok(buffer, u" ", &ctx)));
EXPECT_TRUE(!u16cmp(u"a", u16tok(nullptr, u" ", &ctx)));
EXPECT_TRUE(!u16cmp(u"space", u16tok(nullptr, u" ", &ctx)));
EXPECT_TRUE(!u16cmp(u"and", u16tok(nullptr, u" ", &ctx)));
EXPECT_TRUE(!u16cmp(u"two", u16tok(nullptr, u" ", &ctx)));

KMX_WCHAR buffer_space[128] = u" ";
EXPECT_EQ(nullptr, u16tok(buffer_space, u" ", &ctx));
EXPECT_EQ(nullptr, u16tok(nullptr, u" ", &ctx));
}

TEST_F(kmx_u16_Test, u16tok_str_compare_to_strtok) {
// Compare behaviour of strtok:
char sbuffer[128] = "test a space and two";
EXPECT_TRUE(!strcmp("test", strtok(sbuffer, " ")));
EXPECT_TRUE(!strcmp("a", strtok(nullptr, " ")));
EXPECT_TRUE(!strcmp("space", strtok(nullptr, " ")));
EXPECT_TRUE(!strcmp("and", strtok(nullptr, " ")));
EXPECT_TRUE(!strcmp("two", strtok(nullptr, " ")));

char sbuffer_space[128] = " ";
EXPECT_EQ(nullptr, strtok(sbuffer_space, " "));
EXPECT_EQ(nullptr, strtok(nullptr, " "));
}

0 comments on commit 7efd46f

Please sign in to comment.