Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion folly/String-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ void cUnescape(StringPiece str, String& out, bool strict) {
last = p;
} else if (e == 'X') { // hex
++p;
if (p == str.end()) { // \x at end of string
if (p == str.end() ||
detail::hexTable[static_cast<unsigned char>(*p)] >= 16) {
// \x not followed by a hex digit
if (strict) {
throw_exception<std::invalid_argument>(
"incomplete hex escape sequence");
Expand Down
12 changes: 12 additions & 0 deletions folly/test/StringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,18 @@ TEST(Escape, cUnescape) {
cUnescape<std::string>("hello\\q"),
std::invalid_argument,
"invalid escape sequence");

// \x must be followed by at least one hex digit, also before other text.
EXPECT_THROW_RE(
cUnescape<std::string>("hello\\xg"),
std::invalid_argument,
"incomplete hex escape sequence");
EXPECT_THROW_RE(
cUnescape<std::string>("\\x\\n"),
std::invalid_argument,
"incomplete hex escape sequence");
EXPECT_EQ("hello\\xg", cUnescape<std::string>("hello\\xg", false));
EXPECT_EQ("\\x\n", cUnescape<std::string>("\\x\\n", false));
}

TEST(Escape, uriEscape) {
Expand Down