Commit 8a515308 authored by Gustavo Stor's avatar Gustavo Stor Committed by Facebook Github Bot

Fix cUnescape for non-strict cases

Summary: Strings terminating in a backslash shouldn't throw if strict is set to false. However, the default implementation results in an undefined behavior, since the iterator goes beyond the end position of the string.

Reviewed By: simpkins

Differential Revision: D7123155

fbshipit-source-id: 4311037ccdbfd95f402109dcc1b5862e52fb97a1
parent 32f91898
......@@ -96,6 +96,7 @@ void cUnescape(StringPiece str, String& out, bool strict) {
continue;
}
out.append(&*last, p - last);
++p;
if (p == str.end()) { // backslash at end of string
if (strict) {
throw std::invalid_argument("incomplete escape sequence");
......@@ -104,7 +105,6 @@ void cUnescape(StringPiece str, String& out, bool strict) {
last = p;
continue;
}
++p;
char e = detail::cUnescapeTable[static_cast<unsigned char>(*p)];
if (e == 'O') { // octal
unsigned char val = 0;
......
......@@ -27,6 +27,7 @@
#include <folly/container/Array.h>
#include <folly/portability/GTest.h>
#include <folly/test/TestUtils.h>
using namespace folly;
using namespace std;
......@@ -184,13 +185,20 @@ TEST(Escape, cUnescape) {
EXPECT_EQ("hello\nworld", cUnescape<std::string>("hello\\x0aworld"));
EXPECT_EQ("hello\xff\xfe", cUnescape<std::string>("hello\\377\\376"));
EXPECT_EQ("hello\xff\xfe", cUnescape<std::string>("hello\\xff\\xfe"));
EXPECT_THROW({cUnescape<std::string>("hello\\");},
std::invalid_argument);
EXPECT_THROW({cUnescape<std::string>("hello\\x");},
std::invalid_argument);
EXPECT_THROW({cUnescape<std::string>("hello\\q");},
std::invalid_argument);
EXPECT_EQ("hello\\", cUnescape<std::string>("hello\\", false));
EXPECT_THROW_RE(
cUnescape<std::string>("hello\\"),
std::invalid_argument,
"incomplete escape sequence");
EXPECT_THROW_RE(
cUnescape<std::string>("hello\\x"),
std::invalid_argument,
"incomplete hex escape sequence");
EXPECT_THROW_RE(
cUnescape<std::string>("hello\\q"),
std::invalid_argument,
"invalid escape sequence");
}
TEST(Escape, uriEscape) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment