Make operator= and constructor behavior consistent.
hidl_string(nullptr) == "". Now so does operator=(nullptr).
Bug: 35421240
Test: libhidl_test
Change-Id: Ib62fa71ab47a407b7017c809783dd5f421b98e39
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index ec5b18f..6f04838 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -239,6 +239,11 @@
hidl_string &hidl_string::operator=(const char *s) {
clear();
+
+ if (s == nullptr) {
+ return *this;
+ }
+
copyFrom(s, strlen(s));
return *this;
}
diff --git a/test_main.cpp b/test_main.cpp
index 6bd7f83..4f59371 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -61,6 +61,8 @@
EXPECT_STREQ(s2.c_str(), "s2");
hidl_string s2a(nullptr); // copy constructor from null cstr
EXPECT_STREQ("", s2a);
+ s2a = nullptr; // = from nullptr cstr
+ EXPECT_STREQ(s2a.c_str(), "");
hidl_string s3 = hidl_string("s3"); // move =
EXPECT_STREQ(s3.c_str(), "s3");
hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length