Skip to content

Commit b8bef42

Browse files
kt-peterscarlescufi
authored andcommitted
device_dt_metadata: handle dt_meta being NULL
Using DEVICE_DEFINE, a device without a corresponding DT node can be defined (for example CRYPTO_MTLS), Z_DEVICE_INIT() does not initialize dt_meta for such devices, leaving the field as NULL. device_get_dt_nodelabels() and functions calling it have to handle dev->dt_meta == NULL to prevent fatal errors. Signed-off-by: Jan Peters <[email protected]>
1 parent 086faa5 commit b8bef42

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

drivers/gpio/gpio_shell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ static int cmd_gpio_devices(const struct shell *sh, size_t argc, char **argv)
442442
#ifdef CONFIG_DEVICE_DT_METADATA
443443
const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
444444

445-
if (nl->num_nodelabels > 0) {
445+
if (nl != NULL && nl->num_nodelabels > 0) {
446446
for (size_t j = 0; j < nl->num_nodelabels; j++) {
447447
const char *nodelabel = nl->nodelabels[j];
448448

include/zephyr/device.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,11 +923,14 @@ __syscall const struct device *device_get_by_dt_nodelabel(const char *nodelabel)
923923
/**
924924
* @brief Get the devicetree node labels associated with a device
925925
* @param dev device whose metadata to look up
926-
* @return information about the devicetree node labels
926+
* @return information about the devicetree node labels or NULL if not available
927927
*/
928928
static inline const struct device_dt_nodelabels *
929929
device_get_dt_nodelabels(const struct device *dev)
930930
{
931+
if (dev->dt_meta == NULL) {
932+
return NULL;
933+
}
931934
return dev->dt_meta->nl;
932935
}
933936

kernel/device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const struct device *z_impl_device_get_by_dt_nodelabel(const char *nodelabel)
9898
STRUCT_SECTION_FOREACH(device, dev) {
9999
const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
100100

101-
if (!z_impl_device_is_ready(dev)) {
101+
if (!z_impl_device_is_ready(dev) || nl == NULL) {
102102
continue;
103103
}
104104

subsys/shell/modules/device_service.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static int cmd_device_list(const struct shell *sh,
102102
#ifdef CONFIG_DEVICE_DT_METADATA
103103
const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
104104

105-
if (nl->num_nodelabels > 0) {
105+
if (nl != NULL && nl->num_nodelabels > 0) {
106106
shell_fprintf(sh, SHELL_NORMAL, " DT node labels:");
107107
for (size_t j = 0; j < nl->num_nodelabels; j++) {
108108
const char *nodelabel = nl->nodelabels[j];

0 commit comments

Comments
 (0)