diff options
author | Steven Moreland <smoreland@google.com> | 2021-03-29 22:04:51 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-03-29 22:04:51 +0000 |
commit | 2f352931749658e5fa7cc619a75814a7af829c62 (patch) | |
tree | b2ee6796c081cb83f9fd185f82b9386a3dd3f9d1 | |
parent | ec1e23222141e8fc738e7edbc6290f5d4bd42997 (diff) | |
parent | 34fb6e12224989f8442699ec22d2fcbd5070a346 (diff) | |
download | libhidl-2f352931749658e5fa7cc619a75814a7af829c62.tar.gz |
Merge "hidl_string: empty string opt always applies" am: 34fb6e1222
Original change: https://android-review.googlesource.com/c/platform/system/libhidl/+/1656230
Change-Id: I9f6aea65b86a849ce8e69e7dec2c436d28a96b74
-rw-r--r-- | base/HidlSupport.cpp | 8 | ||||
-rw-r--r-- | test_main.cpp | 14 |
2 files changed, 22 insertions, 0 deletions
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp index af805b9..78faa2f 100644 --- a/base/HidlSupport.cpp +++ b/base/HidlSupport.cpp @@ -217,6 +217,14 @@ void hidl_string::copyFrom(const char *data, size_t size) { if (size >= UINT32_MAX) { LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size; } + + if (size == 0) { + mBuffer = kEmptyString; + mSize = 0; + mOwnsBuffer = false; + return; + } + char *buf = (char *)malloc(size + 1); memcpy(buf, data, size); buf[size] = '\0'; diff --git a/test_main.cpp b/test_main.cpp index ea4dfd1..5c6c78e 100644 --- a/test_main.cpp +++ b/test_main.cpp @@ -175,6 +175,20 @@ TEST_F(LibHidlTest, StringTest) { EXPECT_FALSE(hs2 <= hs1); } +// empty string optimization should apply for any constructor +TEST_F(LibHidlTest, HidlStringEmptyLiteralAllocation) { + using android::hardware::hidl_string; + + hidl_string empty1; + hidl_string empty2(""); + hidl_string empty3("foo", 0); + hidl_string empty4((std::string())); + + EXPECT_EQ(empty1.c_str(), empty2.c_str()); + EXPECT_EQ(empty1.c_str(), empty3.c_str()); + EXPECT_EQ(empty1.c_str(), empty4.c_str()); +} + TEST_F(LibHidlTest, MemoryTest) { using android::hardware::hidl_memory; |