Skip to content

Commit 0da7669

Browse files
committed
Fix URL::Encode crash on Windows
On Windows, the higher characters of a UTF-8 sequence would get converted to a large negative int before being passed to isalnum(). This would fail a sanity check ("undefined behaviour"), causing an assert violation. First casting to unsigned char fixes this.
1 parent 5d003b4 commit 0da7669

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

url.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ string URL::Encode(const string& s, bool full) {
8989
string::value_type c = (*i);
9090

9191
// Keep alphanumeric and other accepted characters intact
92-
if (!full && (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')) {
92+
if (!full && (isalnum((unsigned char)c) || c == '-' || c == '_' || c == '.' || c == '~')) {
9393
escaped << c;
9494
continue;
9595
}

url_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ TEST(TestURL, EncodeNotFull)
106106

107107
enc = URL::Encode("{\"k1\": \"v\", \"k2\": 123}", false);
108108
ASSERT_EQ(enc, "%7B%22k1%22%3A%20%22v%22%2C%20%22k2%22%3A%20123%7D");
109+
110+
enc = URL::Encode("", false);
111+
ASSERT_EQ(enc, "%E2%8C%98");
109112
}
110113

111114
TEST(TestURL, EncodeFull)
@@ -124,4 +127,7 @@ TEST(TestURL, EncodeFull)
124127

125128
enc = URL::Encode("{\"k1\": \"v\", \"k2\": 123}", true);
126129
ASSERT_EQ(enc, "%7B%22%6B%31%22%3A%20%22%76%22%2C%20%22%6B%32%22%3A%20%31%32%33%7D");
130+
131+
enc = URL::Encode("", false);
132+
ASSERT_EQ(enc, "%E2%8C%98");
127133
}

0 commit comments

Comments
 (0)