fix: verification of const/var decls
missing cases of AidlConstantValue::Type caused crashes when verifying
const decls of AidlDefinedTypes.
Now we verify all types of const/var decls.
Bug: 175478675
Test: aidl_unittests
Change-Id: I6524032c227f6c40b895d80c0ab172a978081390
diff --git a/aidl.cpp b/aidl.cpp
index 344649d..5b1295d 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -664,30 +664,18 @@
return AidlError::BAD_METHOD_ID;
}
}
- // Verify and resolve the constant declarations
- for (const auto& constant : defined_type->GetConstantDeclarations()) {
- switch (constant->GetValue().GetType()) {
- case AidlConstantValue::Type::STRING: // fall-through
- case AidlConstantValue::Type::REF: // fall-through
- case AidlConstantValue::Type::INT8: // fall-through
- case AidlConstantValue::Type::INT32: // fall-through
- case AidlConstantValue::Type::INT64: // fall-through
- case AidlConstantValue::Type::FLOATING: // fall-through
- case AidlConstantValue::Type::UNARY: // fall-through
- case AidlConstantValue::Type::BINARY: {
- bool success = constant->CheckValid(*typenames);
- if (!success) {
- return AidlError::BAD_TYPE;
- }
- if (constant->ValueString(cpp::ConstantValueDecorator).empty()) {
- return AidlError::BAD_TYPE;
- }
- break;
+ // Verify the var/const declarations.
+ // const expressions should be non-empty when evaluated with the var/const type.
+ if (!is_check_api) {
+ for (const auto& constant : defined_type->GetConstantDeclarations()) {
+ if (constant->ValueString(AidlConstantValueDecorator).empty()) {
+ return AidlError::BAD_TYPE;
}
- default:
- AIDL_FATAL(constant) << "Unrecognized constant type: "
- << static_cast<int>(constant->GetValue().GetType());
- break;
+ }
+ for (const auto& var : defined_type->GetFields()) {
+ if (var->GetDefaultValue() && var->ValueString(AidlConstantValueDecorator).empty()) {
+ return AidlError::BAD_TYPE;
+ }
}
}
}
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 03b7d66..5079296 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -1038,6 +1038,17 @@
EXPECT_EQ(AidlError::BAD_TYPE, error);
}
+TEST_F(AidlTest, BoolConstantsEvaluatesToIntegers) {
+ io_delegate_.SetFileContents("a/Foo.aidl", "package a; parcelable Foo { const int y = true; }");
+ CaptureStderr();
+ auto options = Options::From("aidl --lang java -o out a/Foo.aidl");
+ EXPECT_EQ(0, aidl::compile_aidl(options, io_delegate_));
+ EXPECT_EQ("", GetCapturedStderr());
+ string code;
+ EXPECT_TRUE(io_delegate_.GetWrittenContents("out/a/Foo.java", &code));
+ EXPECT_THAT(code, testing::HasSubstr("public static final int y = 1;"));
+}
+
TEST_P(AidlTest, FailOnManyDefinedTypes) {
AidlError error;
const string expected_stderr =