Skip to content

Commit e086fc8

Browse files
committed
kernel: fix error propagation for device deferred initialization
This fix propagates the failure code returned from the device initialization function to the application. Previously, do_device_init() mistakely returned errno instead of -errno. We use an intermediate variable to compute the value to store in dev->state->init_res, and return the value from the device's init untouched. Signed-off-by: Loic Domaigne <tech@domaigne.com>
1 parent a10f807 commit e086fc8

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

kernel/init.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,16 @@ static int do_device_init(const struct device *dev)
313313

314314
if (dev->ops.init != NULL) {
315315
rc = dev->ops.init(dev);
316-
/* Mark device initialized. If initialization
317-
* failed, record the error condition.
318-
*/
316+
/* If initialization failed, record the error condition. */
319317
if (rc != 0) {
320-
if (rc < 0) {
321-
rc = -rc;
322-
}
323-
if (rc > UINT8_MAX) {
324-
rc = UINT8_MAX;
325-
}
326-
dev->state->init_res = rc;
318+
__ASSERT(rc != INT_MIN, "init(dev) failed, but init_res will be 0");
319+
int res = (rc < 0) ? -rc : rc;
320+
321+
dev->state->init_res = (res <= UINT8_MAX) ? res : UINT8_MAX;
327322
}
328323
}
329324

325+
/* device initialization has been invoked */
330326
dev->state->initialized = true;
331327

332328
if (rc == 0) {

0 commit comments

Comments
 (0)