hidl_string: allow null cstr am: a21d84f11f
am: 55107b3754

Change-Id: Ic42549d8aaf290cbf4a1cd7b71be6ffd1d85813b
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 1544969..68155cd 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -195,6 +195,10 @@
 }
 
 hidl_string::hidl_string(const char *s) : hidl_string() {
+    if (s == nullptr) {
+        return;
+    }
+
     copyFrom(s, strlen(s));
 }
 
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 80f95b6..d22c257 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -126,7 +126,7 @@
 
     // copy constructor.
     hidl_string(const hidl_string &);
-    // copy from a C-style string.
+    // copy from a C-style string. nullptr will create an empty string
     hidl_string(const char *);
     // copy the first length characters from a C-style string.
     hidl_string(const char *, size_t length);
diff --git a/test_main.cpp b/test_main.cpp
index d663391..6bd7f83 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -59,6 +59,8 @@
     EXPECT_STREQ(s1.c_str(), "s1");
     hidl_string s2("s2"); // copy constructor from cstr
     EXPECT_STREQ(s2.c_str(), "s2");
+    hidl_string s2a(nullptr); // copy constructor from null cstr
+    EXPECT_STREQ("", s2a);
     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