Skip to content

Commit

Permalink
Recognize "\r\n" (Windows) line endings in base::JSONParser so that l…
Browse files Browse the repository at this point in the history
…ine numbers in error reports won't confuse users.

BUG=167424
TEST=Unit tests

Review URL: https://chromiumcodereview.appspot.com/12026035

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@180625 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
fangjue23303@gmail.com committed Feb 5, 2013
1 parent b4b26c6 commit c472a6c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,4 @@ Trevor Perrin <unsafe@trevp.net>
Ion Rosca <rosca@adobe.com>
Sylvain Zimmer <sylvinus@gmail.com>
Sungmann Cho <sungmann.cho@gmail.com>
方觉 (Fang Jue) <fangjue23303@gmail.com>
4 changes: 3 additions & 1 deletion base/json/json_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ void JSONParser::EatWhitespaceAndComments() {
case '\r':
case '\n':
index_last_line_ = index_;
++line_number_;
// Don't increment line_number_ twice for "\r\n".
if (!(*pos_ == '\n' && pos_ > start_pos_ && *(pos_ - 1) == '\r'))
++line_number_;
// Fall through.
case ' ':
case '\t':
Expand Down
17 changes: 15 additions & 2 deletions base/json/json_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,28 @@ TEST_F(JSONParserTest, ErrorMessages) {
EXPECT_EQ(0, error_code);

// Test line and column counting
const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
// error here ---------------------------------^
const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
// error here ----------------------------------^
root.reset(JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC,
&error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
error_message);
EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);

error_code = 0;
error_message = "";
// Test line and column counting with "\r\n" line ending
const char big_json_crlf[] =
"[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]";
// error here ----------------------^
root.reset(JSONReader::ReadAndReturnError(big_json_crlf, JSON_PARSE_RFC,
&error_code, &error_message));
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
error_message);
EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);

// Test each of the error conditions
root.reset(JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC,
&error_code, &error_message));
Expand Down

0 comments on commit c472a6c

Please sign in to comment.