diff --git a/drivers/gpio/gpio_shell.c b/drivers/gpio/gpio_shell.c index 04f0c05b91a..d05683327c9 100644 --- a/drivers/gpio/gpio_shell.c +++ b/drivers/gpio/gpio_shell.c @@ -442,7 +442,7 @@ static int cmd_gpio_devices(const struct shell *sh, size_t argc, char **argv) #ifdef CONFIG_DEVICE_DT_METADATA const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev); - if (nl->num_nodelabels > 0) { + if (nl != NULL && nl->num_nodelabels > 0) { for (size_t j = 0; j < nl->num_nodelabels; j++) { const char *nodelabel = nl->nodelabels[j]; diff --git a/include/zephyr/device.h b/include/zephyr/device.h index e8bbc09a299..cc72cb177f3 100644 --- a/include/zephyr/device.h +++ b/include/zephyr/device.h @@ -923,11 +923,14 @@ __syscall const struct device *device_get_by_dt_nodelabel(const char *nodelabel) /** * @brief Get the devicetree node labels associated with a device * @param dev device whose metadata to look up - * @return information about the devicetree node labels + * @return information about the devicetree node labels or NULL if not available */ static inline const struct device_dt_nodelabels * device_get_dt_nodelabels(const struct device *dev) { + if (dev->dt_meta == NULL) { + return NULL; + } return dev->dt_meta->nl; } diff --git a/kernel/device.c b/kernel/device.c index de38406aac5..6edf63bd4d1 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -98,7 +98,7 @@ const struct device *z_impl_device_get_by_dt_nodelabel(const char *nodelabel) STRUCT_SECTION_FOREACH(device, dev) { const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev); - if (!z_impl_device_is_ready(dev)) { + if (!z_impl_device_is_ready(dev) || nl == NULL) { continue; } diff --git a/subsys/shell/modules/device_service.c b/subsys/shell/modules/device_service.c index acdf70bd8ce..98f9fd1d420 100644 --- a/subsys/shell/modules/device_service.c +++ b/subsys/shell/modules/device_service.c @@ -102,7 +102,7 @@ static int cmd_device_list(const struct shell *sh, #ifdef CONFIG_DEVICE_DT_METADATA const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev); - if (nl->num_nodelabels > 0) { + if (nl != NULL && nl->num_nodelabels > 0) { shell_fprintf(sh, SHELL_NORMAL, " DT node labels:"); for (size_t j = 0; j < nl->num_nodelabels; j++) { const char *nodelabel = nl->nodelabels[j];