Fix memory leaks in DexString wrapper and in unit tests.
This addresses the art-gtest-asan failure from http://r.android.com/867370:
http://ab/5224785
Test: env SANITIZE_HOST=address m test-art-host-gtest-dex_file_supp_test
Bug: 119632407
Change-Id: I84f761561223c7da56a335b6691189020027a4a0
diff --git a/libdexfile/external/dex_file_supp_test.cc b/libdexfile/external/dex_file_supp_test.cc
index 3dd08c0..2f7ad50 100644
--- a/libdexfile/external/dex_file_supp_test.cc
+++ b/libdexfile/external/dex_file_supp_test.cc
@@ -75,6 +75,12 @@
EXPECT_EQ(std::string_view(s2), "foo");
}
+TEST(DexStringTest, reassign) {
+ auto s = DexString("foo");
+ s = DexString("bar");
+ EXPECT_EQ(std::string_view(s), "bar");
+}
+
TEST(DexStringTest, data_access) {
auto s = DexString("foo");
EXPECT_STREQ(s.data(), "foo");
@@ -272,7 +278,7 @@
std::unique_ptr<DexFile> dex_file = GetTestDexData();
ASSERT_NE(dex_file, nullptr);
- auto df1 = DexFile(std::move(*dex_file.release()));
+ auto df1 = DexFile(std::move(*dex_file));
auto df2 = DexFile(std::move(df1));
MethodInfo info = df2.GetMethodInfoForOffset(0x100, false);
diff --git a/libdexfile/external/include/art_api/dex_file_support.h b/libdexfile/external/include/art_api/dex_file_support.h
index ddd9143..24222af 100644
--- a/libdexfile/external/include/art_api/dex_file_support.h
+++ b/libdexfile/external/include/art_api/dex_file_support.h
@@ -23,6 +23,7 @@
#include <memory>
#include <string>
#include <string_view>
+#include <utility>
#include <vector>
#include <android-base/macros.h>
@@ -35,7 +36,9 @@
// Minimal std::string look-alike for a string returned from libdexfile.
class DexString final {
public:
- DexString(DexString&& dex_str) noexcept { ReplaceExtString(std::move(dex_str)); }
+ DexString(DexString&& dex_str) noexcept : ext_string_(dex_str.ext_string_) {
+ dex_str.ext_string_ = ExtDexFileMakeString("", 0);
+ }
explicit DexString(const char* str = "")
: ext_string_(ExtDexFileMakeString(str, std::strlen(str))) {}
explicit DexString(std::string_view str)
@@ -43,7 +46,7 @@
~DexString() { ExtDexFileFreeString(ext_string_); }
DexString& operator=(DexString&& dex_str) noexcept {
- ReplaceExtString(std::move(dex_str));
+ std::swap(ext_string_, dex_str.ext_string_);
return *this;
}
@@ -72,11 +75,6 @@
explicit DexString(const ExtDexFileString* ext_string) : ext_string_(ext_string) {}
const ExtDexFileString* ext_string_; // Owned instance. Never nullptr.
- void ReplaceExtString(DexString&& dex_str) {
- ext_string_ = dex_str.ext_string_;
- dex_str.ext_string_ = ExtDexFileMakeString("", 0);
- }
-
DISALLOW_COPY_AND_ASSIGN(DexString);
};