union: __assert2() when a tag mismatches
Rather than abort()ing without any message, __assert2() will leave a bit
of hints.
- bad access: get<tag>() with a wrong tag.
- can't reach here: writeToParcel() with an unknown tag. (in practice,
this won't happen though)
Bug: 170681273
Test: aidl_integration_test
Change-Id: I85a872d602720542657ca1cbb1c931a3e6d15749
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index a4209c0..d76d449 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -2990,6 +2990,7 @@
#include <binder/Parcel.h>
#include <binder/ParcelFileDescriptor.h>
#include <binder/Status.h>
+#include <cassert>
#include <codecvt>
#include <cstdint>
#include <locale>
@@ -2999,6 +3000,10 @@
#include <variant>
#include <vector>
+#ifndef __BIONIC__
+#define __assert2(a,b,c,d) ((void)0)
+#endif
+
namespace a {
class Foo : public ::android::Parcelable {
@@ -3062,13 +3067,13 @@
template <Tag _tag>
const auto& get() const {
- if (getTag() != _tag) { abort(); }
+ if (getTag() != _tag) { __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); }
return std::get<_tag>(_value);
}
template <Tag _tag>
auto& get() {
- if (getTag() != _tag) { abort(); }
+ if (getTag() != _tag) { __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); }
return std::get<_tag>(_value);
}
@@ -3161,7 +3166,7 @@
case e: return _aidl_parcel->writeByte(static_cast<int8_t>(get<e>()));
case pfd: return _aidl_parcel->writeParcelable(get<pfd>());
}
- abort();
+ __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "can't reach here");
}
} // namespace a
@@ -3174,6 +3179,7 @@
#include <locale>
#include <sstream>
+#include <cassert>
#include <type_traits>
#include <utility>
#include <variant>
@@ -3186,6 +3192,11 @@
#include <android/binder_stability.h>
#endif // BINDER_STABILITY_SUPPORT
#include <aidl/a/ByteEnum.h>
+
+#ifndef __BIONIC__
+#define __assert2(a,b,c,d) ((void)0)
+#endif
+
namespace aidl {
namespace a {
class Foo {
@@ -3233,13 +3244,13 @@
template <Tag _tag>
const auto& get() const {
- if (getTag() != _tag) { abort(); }
+ if (getTag() != _tag) { __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); }
return std::get<_tag>(_value);
}
template <Tag _tag>
auto& get() {
- if (getTag() != _tag) { abort(); }
+ if (getTag() != _tag) { __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); }
return std::get<_tag>(_value);
}
@@ -3332,7 +3343,7 @@
case e: return AParcel_writeByte(_parcel, static_cast<int8_t>(get<e>()));
case pfd: return ::ndk::AParcel_writeRequiredParcelFileDescriptor(_parcel, get<pfd>());
}
- abort();
+ __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "can't reach here");
}
} // namespace a