diff --git a/include/device.h b/include/device.h index 3029caeeaeb..45c42f69e1f 100644 --- a/include/device.h +++ b/include/device.h @@ -313,7 +313,8 @@ struct device { * it can use this function to retrieve the device structure of the lower level * driver by the name the driver exposes to the system. * - * @param name device name to search for. + * @param name device name to search for. A null pointer, or a pointer to an + * empty string, will cause NULL to be returned. * * @return pointer to device structure; NULL if not found or cannot be used. */ diff --git a/kernel/device.c b/kernel/device.c index 013669df7a1..a30439ae599 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -79,6 +79,13 @@ const struct device *z_impl_device_get_binding(const char *name) { const struct device *dev; + /* A null string identifies no device. So does an empty + * string. + */ + if ((name == NULL) || (*name == 0)) { + return NULL; + } + /* Split the search into two loops: in the common scenario, where * device names are stored in ROM (and are referenced by the user * with CONFIG_* macros), only cheap pointer comparisons will be