Commit cf75117d authored by Tricia Crichton's avatar Tricia Crichton Committed by Commit Bot

[ChromeDriver] Confirm brackets found before substr

Some code paths do not set the status message for UnexpectedAlertOpen
errors. When these are encountered, the code crashes when the { or }
are not found. This CL checks for that state and prevents the crash.

Fixed: chromedriver:3073
Change-Id: I9cc1cf4f1988af1300f9363add90a1217f0cb26c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1877136Reviewed-by: default avatarJohn Chen <johnchen@chromium.org>
Commit-Queue: Tricia Crichton <triciac@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709051}
parent b26124d5
......@@ -1249,11 +1249,15 @@ HttpHandler::PrepareStandardResponse(
// error UnexpectedAlertOpen should contain 'data.text' with alert text
if (status.code() == kUnexpectedAlertOpen) {
const std::string& message = status.message();
unsigned first = message.find("{");
unsigned last = message.find_last_of("}");
std::string alertText = message.substr(first, last-first);
alertText = alertText.substr(alertText.find(":") + 2);
inner_params->SetString("data.text", alertText);
auto first = message.find("{");
auto last = message.find_last_of("}");
if (first == std::string::npos || last == std::string::npos) {
inner_params->SetString("data.text", "");
} else {
std::string alertText = message.substr(first, last - first);
alertText = alertText.substr(alertText.find(":") + 2);
inner_params->SetString("data.text", alertText);
}
}
body_params.SetDictionary("value", std::move(inner_params));
} else {
......
......@@ -85,6 +85,7 @@ class HttpHandler {
FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleInvalidPost);
FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleUnimplementedCommand);
FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleCommand);
FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, StandardResponse_ErrorNoMessage);
typedef std::vector<CommandMapping> CommandMap;
Command WrapToCommand(const char* name,
......
......@@ -126,6 +126,13 @@ TEST(HttpHandlerTest, HandleCommand) {
ASSERT_EQ(json, response.body());
}
TEST(HttpHandlerTest, StandardResponse_ErrorNoMessage) {
HttpHandler handler("/");
Status status = Status(kUnexpectedAlertOpen);
ASSERT_NO_FATAL_FAILURE(handler.PrepareStandardResponse(
"not used", status, std::make_unique<base::Value>(), "1234"));
}
TEST(MatchesCommandTest, DiffMethod) {
CommandMapping command(kPost, "path", base::Bind(&DummyCommand, Status(kOk)));
std::string session_id;
......
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