Fix leak in aidl_unittests
Turning on "sanitizer: address" revealed the leak in the test code.
Bug: n/a
Test: aidl_unittests
Change-Id: Id13820cff676d998934d6b1485d453f37397d696
diff --git a/Android.bp b/Android.bp
index 59c4a62..d4ec732 100644
--- a/Android.bp
+++ b/Android.bp
@@ -173,6 +173,10 @@
"liblog",
],
+ sanitize: {
+ address: true,
+ },
+
required: ["aidl-golden-test-build-hook"],
}
diff --git a/aidl_language.cpp b/aidl_language.cpp
index 0cbef0b..5941f35 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -167,9 +167,9 @@
__builtin_unreachable();
}
-AidlAnnotation* AidlAnnotation::Parse(
+std::unique_ptr<AidlAnnotation> AidlAnnotation::Parse(
const AidlLocation& location, const string& name,
- std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list,
+ std::map<std::string, std::shared_ptr<AidlConstantValue>> parameter_list,
const Comments& comments) {
const Schema* schema = nullptr;
for (const Schema& a_schema : AllSchemas()) {
@@ -187,19 +187,16 @@
}
stream << ".";
AIDL_ERROR(location) << stream.str();
- return nullptr;
- }
- if (parameter_list == nullptr) {
- return new AidlAnnotation(location, *schema, {}, comments);
+ return {};
}
- return new AidlAnnotation(location, *schema, std::move(*parameter_list), comments);
+ return std::unique_ptr<AidlAnnotation>(
+ new AidlAnnotation(location, *schema, std::move(parameter_list), comments));
}
-AidlAnnotation::AidlAnnotation(
- const AidlLocation& location, const Schema& schema,
- std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters,
- const Comments& comments)
+AidlAnnotation::AidlAnnotation(const AidlLocation& location, const Schema& schema,
+ std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters,
+ const Comments& comments)
: AidlNode(location, comments), schema_(schema), parameters_(std::move(parameters)) {}
struct ConstReferenceFinder : AidlVisitor {
diff --git a/aidl_language.h b/aidl_language.h
index d32ee24..973f20c 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -252,9 +252,9 @@
static std::string TypeToString(Type type);
- static AidlAnnotation* Parse(
+ static std::unique_ptr<AidlAnnotation> Parse(
const AidlLocation& location, const string& name,
- std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list,
+ std::map<std::string, std::shared_ptr<AidlConstantValue>> parameter_list,
const Comments& comments);
AidlAnnotation(const AidlAnnotation&) = default;
@@ -306,7 +306,7 @@
static const std::vector<Schema>& AllSchemas();
AidlAnnotation(const AidlLocation& location, const Schema& schema,
- std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters,
+ std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters,
const Comments& comments);
const Schema& schema_;
diff --git a/aidl_language_y.yy b/aidl_language_y.yy
index aa82c4d..9518579 100644
--- a/aidl_language_y.yy
+++ b/aidl_language_y.yy
@@ -772,17 +772,22 @@
annotation
: ANNOTATION {
- $$ = AidlAnnotation::Parse(loc(@1), $1->GetText(), nullptr, $1->GetComments());
- if (!$$) {
+ auto annot = AidlAnnotation::Parse(loc(@1), $1->GetText(), {}, $1->GetComments());
+ if (annot) {
+ $$ = annot.release();
+ } else {
ps->AddError();
}
delete $1;
}
| ANNOTATION '(' parameter_list ')' {
- $$ = AidlAnnotation::Parse(loc(@1, @4), $1->GetText(), $3, $1->GetComments());
- if (!$$) {
+ auto annot = AidlAnnotation::Parse(loc(@1, @4), $1->GetText(), std::move(*$3), $1->GetComments());
+ if (annot) {
+ $$ = annot.release();
+ } else {
ps->AddError();
}
+
delete $1;
delete $3;
}
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index efad030..782e150 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -1451,7 +1451,7 @@
auto set_nullable = [](std::unique_ptr<AidlTypeSpecifier>&& type) {
std::vector<AidlAnnotation> annotations;
- annotations.emplace_back(*AidlAnnotation::Parse(AIDL_LOCATION_HERE, "nullable", nullptr, {}));
+ annotations.emplace_back(*AidlAnnotation::Parse(AIDL_LOCATION_HERE, "nullable", {}, {}));
type->Annotate(std::move(annotations));
return std::move(type);
};