diff options
author | Jeongik Cha <jeongik@google.com> | 2021-09-25 01:19:56 +0900 |
---|---|---|
committer | Jeongik Cha <jeongik@google.com> | 2021-09-27 01:48:47 +0000 |
commit | 8042991b1668bbde2c8d752c92e9f53f50352c1a (patch) | |
tree | b5ae9acae07c5aaf5cc07daf67f49a457484a493 | |
parent | 4d08ea788767db87b3d69cd68bfd34a47ca97270 (diff) | |
download | aidl-8042991b1668bbde2c8d752c92e9f53f50352c1a.tar.gz |
An enum is initialized as zero if it doesn't have default value
In C++, NDK backend, an enum value hasn't been initialized if it doesn't
have default value. In that case, initialize an enum with zero for the
backends.
Bug: 198346478
Test: check golden output and m
Ignore-AOSP-First: security patch
Change-Id: I6cb67258cc4aacc41ae80c46ccee10a3cfe723f6
Merged-In: I6cb67258cc4aacc41ae80c46ccee10a3cfe723f6
(cherry picked from commit f39b16442ad832f2921ad0eec298b7c333936dc1)
-rw-r--r-- | generate_cpp.cpp | 7 | ||||
-rw-r--r-- | generate_ndk.cpp | 7 |
2 files changed, 14 insertions, 0 deletions
diff --git a/generate_cpp.cpp b/generate_cpp.cpp index cc256573..bbee9b3a 100644 --- a/generate_cpp.cpp +++ b/generate_cpp.cpp @@ -1111,6 +1111,13 @@ std::unique_ptr<Document> BuildParcelHeader(const AidlTypenames& typenames, if (variable->GetDefaultValue()) { out << " = " << cppType.c_str() << "(" << variable->ValueString(ConstantValueDecorator) << ")"; + } else if (auto type = typenames.TryGetDefinedType(variable->GetType().GetName()); type) { + if (auto enum_type = type->AsEnumDeclaration(); enum_type) { + if (!variable->GetType().IsArray()) { + // if an enum doesn't have explicit default value, do zero-initialization + out << " = " << cppType << "(0)"; + } + } } out << ";\n"; diff --git a/generate_ndk.cpp b/generate_ndk.cpp index 274d283f..0efd7eb7 100644 --- a/generate_ndk.cpp +++ b/generate_ndk.cpp @@ -879,6 +879,13 @@ void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types, out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName(); if (variable->GetDefaultValue()) { out << " = " << variable->ValueString(ConstantValueDecorator); + } else if (auto type = types.TryGetDefinedType(variable->GetType().GetName()); type) { + if (auto enum_type = type->AsEnumDeclaration(); enum_type) { + if (!variable->GetType().IsArray()) { + // if an enum doesn't have explicit default value, do zero-initialization + out << " = " << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << "(0)"; + } + } } out << ";\n"; } |