hidl_string: allow null cstr
Automatically converting nullptr to empty string rather than erroring.
This gets rid of null values at the hal layer (rather than passing them
to the server) and simplifies code that would have to check for nulls.
Test: libhidl_test
Fixes: 35421240
Change-Id: Iada25da5407c3eccdd28704d993ffdee8b15c5fe
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);