Skip to content

boot: zephyr: Avoid relying on assert statement and add explicit checks#2666

Open
namjoshiniks wants to merge 1 commit intomcu-tools:mainfrom
namjoshiniks:mcuboot-fixes/return_code_checks
Open

boot: zephyr: Avoid relying on assert statement and add explicit checks#2666
namjoshiniks wants to merge 1 commit intomcu-tools:mainfrom
namjoshiniks:mcuboot-fixes/return_code_checks

Conversation

@namjoshiniks
Copy link
Copy Markdown
Contributor

@namjoshiniks namjoshiniks commented Mar 18, 2026

Avoid relying on assert statement and add explicit checks

Fixes /issues/2661

@namjoshiniks namjoshiniks force-pushed the mcuboot-fixes/return_code_checks branch 4 times, most recently from b3df3eb to b573772 Compare March 24, 2026 20:19
@namjoshiniks namjoshiniks requested a review from d3zd3z as a code owner March 24, 2026 20:19
@namjoshiniks namjoshiniks force-pushed the mcuboot-fixes/return_code_checks branch from b573772 to 6fa896e Compare March 24, 2026 21:49
@namjoshiniks namjoshiniks force-pushed the mcuboot-fixes/return_code_checks branch 2 times, most recently from 1679a96 to ad54782 Compare March 25, 2026 18:25
Copy link
Copy Markdown
Collaborator

@de-nordic de-nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a best idea.
When we fail in these places it is not like you can fix a device nor react to the problem, yet these errors will be carried verbatim wasting space on device boot partition.
There is a difference between this issues and when, for example, signature check fails; the later has a chance to happen due to actually bad signature or image and tells you that something may be done to address the problem
If any of fixed errors here happen we have either configuration issue or device is failing anyway and nothing can be done; these are either catch in testing or device can play dead anyway.

Fixes mcu-tools#2661

Signed-off-by: Nikhil Namjoshi <nikhilnamjoshi@google.com>
@namjoshiniks
Copy link
Copy Markdown
Contributor Author

This is not a best idea. When we fail in these places it is not like you can fix a device nor react to the problem, yet these errors will be carried verbatim wasting space on device boot partition. There is a difference between this issues and when, for example, signature check fails; the later has a chance to happen due to actually bad signature or image and tells you that something may be done to address the problem If any of fixed errors here happen we have either configuration issue or device is failing anyway and nothing can be done; these are either catch in testing or device can play dead anyway.

@de-nordic

The problem here is the ASSERT statements get stripped during compile time if DEBUG flag is disabled and CONFIG_ASSERT=n, which is the case in all production projects. If asserts are stripped off, the behavior is undefined and error can go undetected (and possibly a security vulnerability too).

As for space, Zephyr based MCUboot is already bulky so a few bytes may not matter. But I do agree, that having string statements for unrecoverable errors that shouldn't really happen in production code is unnecessary. So made the logs to be DBG type logs

@namjoshiniks namjoshiniks force-pushed the mcuboot-fixes/return_code_checks branch from ad54782 to e3d3919 Compare March 25, 2026 19:49
@namjoshiniks namjoshiniks requested a review from de-nordic March 25, 2026 19:49
@de-nordic
Copy link
Copy Markdown
Collaborator

The problem here is the ASSERT statements get stripped during compile time if DEBUG flag is disabled and CONFIG_ASSERT=n, which is the case in all production projects. If asserts are stripped off, the behavior is undefined and error can go undetected (and possibly a security vulnerability too).

Maybe change this to LOG_DBG and panic when tests fail, even when non in debug mode.

As for space, Zephyr based MCUboot is already bulky so a few bytes may not matter.

They do when they are generally dead code unless unrecoverable problem occurs. And it does matter, because that is space taken out of apps, that is space I am asked to find for customers, that is space that is taking extra time to verify on signature, when mcuboot is protected in other means, this also takes out extra protection bits in soc.

But I do agree, that having string statements for unrecoverable errors that shouldn't really happen in production code is unnecessary. So made the logs to be DBG type logs

Fine with this.

@namjoshiniks
Copy link
Copy Markdown
Contributor Author

namjoshiniks commented Mar 26, 2026

Maybe change this to LOG_DBG and panic when tests fail, even when non in debug mode.

Not sure if you got chance to look at the code, but I changed the logging statements to Debug. Additionally, wherever possible I am returning errors (from non void functions or functions that can handle errors gracefully). For void functions, I added FIH_PANIC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zephyr MCUboot: Avoid relying on assert statements and add explicit return code checks

3 participants