Fix a potential memory leak bug.
BUG=
TEST=
Review URL: http://codereview.chromium.org/9113075
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119269 0039d316-1c4b-4281-b951-d872f2087c98
CrOS-Libchrome-Original-Commit: 50fa6f92efd0aceae9dbe689b3b216d003b095a7
diff --git a/base/json/json_value_converter.h b/base/json/json_value_converter.h
index 0bb131c..6fbe246 100644
--- a/base/json/json_value_converter.h
+++ b/base/json/json_value_converter.h
@@ -259,9 +259,9 @@
if (!list->Get(i, &element))
continue;
- Element *e = new Element;
- if (basic_converter_.Convert(*element, e)) {
- field->push_back(e);
+ scoped_ptr<Element> e(new Element);
+ if (basic_converter_.Convert(*element, e.get())) {
+ field->push_back(e.release());
} else {
DVLOG(1) << "failure at " << i << "-th element";
return false;
@@ -293,9 +293,9 @@
if (!list->Get(i, &element))
continue;
- NestedType* nested = new NestedType();
- if (converter_.Convert(*element, nested)) {
- field->push_back(nested);
+ scoped_ptr<NestedType> nested(new NestedType);
+ if (converter_.Convert(*element, nested.get())) {
+ field->push_back(nested.release());
} else {
DVLOG(1) << "failure at " << i << "-th element";
return false;
diff --git a/base/json/json_value_converter_unittest.cc b/base/json/json_value_converter_unittest.cc
index d23a1e9..b81a52c 100644
--- a/base/json/json_value_converter_unittest.cc
+++ b/base/json/json_value_converter_unittest.cc
@@ -193,4 +193,21 @@
// No check the values as mentioned above.
}
+TEST(JSONValueConverterTest, RepeatedValueErrorInTheMiddle) {
+ const char normal_data[] =
+ "{\n"
+ " \"foo\": 1,\n"
+ " \"bar\": \"bar\",\n"
+ " \"baz\": true,\n"
+ " \"simple_enum\": \"baz\","
+ " \"ints\": [1, false]"
+ "}\n";
+
+ scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false));
+ SimpleMessage message;
+ base::JSONValueConverter<SimpleMessage> converter;
+ EXPECT_FALSE(converter.Convert(*value.get(), &message));
+ // No check the values as mentioned above.
+}
+
} // namespace base