Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: base/json/json_parser_unittest.cc

Issue 2712013003: Fix several potential buffer over-read errors in JSONParser::ConsumeNumber. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/json/json_parser.cc ('K') | « base/json/json_parser.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/json/json_parser.h" 5 #include "base/json/json_parser.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/strings/stringprintf.h"
12 #include "base/values.h" 14 #include "base/values.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 namespace base { 17 namespace base {
16 namespace internal { 18 namespace internal {
17 19
18 class JSONParserTest : public testing::Test { 20 class JSONParserTest : public testing::Test {
19 public: 21 public:
20 JSONParser* NewTestParser(const std::string& input, 22 JSONParser* NewTestParser(const std::string& input,
21 int options = JSON_PARSE_RFC) { 23 int options = JSON_PARSE_RFC) {
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 const std::string quoted_bogus_char = "\"" + bogus_char + "\""; 337 const std::string quoted_bogus_char = "\"" + bogus_char + "\"";
336 std::unique_ptr<JSONParser> parser( 338 std::unique_ptr<JSONParser> parser(
337 NewTestParser(quoted_bogus_char, JSON_REPLACE_INVALID_CHARACTERS)); 339 NewTestParser(quoted_bogus_char, JSON_REPLACE_INVALID_CHARACTERS));
338 std::unique_ptr<Value> value(parser->ConsumeString()); 340 std::unique_ptr<Value> value(parser->ConsumeString());
339 ASSERT_TRUE(value.get()); 341 ASSERT_TRUE(value.get());
340 std::string str; 342 std::string str;
341 EXPECT_TRUE(value->GetAsString(&str)); 343 EXPECT_TRUE(value->GetAsString(&str));
342 EXPECT_EQ(kUnicodeReplacementString, str); 344 EXPECT_EQ(kUnicodeReplacementString, str);
343 } 345 }
344 346
347 TEST_F(JSONParserTest, ParseNumberErrors) {
348 const struct {
349 const char* input;
350 bool parse_success;
351 double value;
352 } kCases[] = {
353 // clang-format off
dcheng 2017/02/24 21:15:31 Is this necessary =P
Robert Sesek 2017/02/24 21:25:33 I mean, no, but without this it tries to put three
dcheng 2017/02/24 21:55:45 I see your point. While I think we should be gener
354 {"1", true, 1},
355 {"2.", false, 0},
356 {"42", true, 42},
357 {"6e", false, 0},
358 {"43e2", true, 4300},
359 {"43e-", false, 0},
360 {"9e-3", true, 0.009},
361 {"2e+", false, 0},
362 {"2e+2", true, 200},
363 // clang-format on
364 };
365
366 for (unsigned int i = 0; i < arraysize(kCases); ++i) {
367 auto test_case = kCases[i];
368 SCOPED_TRACE(StringPrintf("case %u: \"%s\"", i, test_case.input));
369
370 // MSan will do a better job detecting over-read errors if the input is
371 // not nul-terminated on the heap.
372 size_t str_len = strlen(test_case.input);
373 auto non_nul_termianted = MakeUnique<char[]>(str_len);
374 memcpy(non_nul_termianted.get(), test_case.input, str_len);
375
376 StringPiece string_piece(non_nul_termianted.get(), str_len);
377 std::unique_ptr<Value> result = JSONReader::Read(string_piece);
378 if (test_case.parse_success) {
379 EXPECT_TRUE(result);
380 } else {
381 EXPECT_FALSE(result);
382 }
383
384 if (!result)
385 continue;
386
387 double double_value = 0;
388 EXPECT_TRUE(result->GetAsDouble(&double_value));
389 EXPECT_EQ(test_case.value, double_value);
390 }
391 }
392
345 } // namespace internal 393 } // namespace internal
346 } // namespace base 394 } // namespace base
OLDNEW
« base/json/json_parser.cc ('K') | « base/json/json_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698