Skip to content

include/errno.h: skip set_errno in interrupt context#18839

Merged
acassis merged 1 commit into
apache:masterfrom
masc2008:set_errno
May 3, 2026
Merged

include/errno.h: skip set_errno in interrupt context#18839
acassis merged 1 commit into
apache:masterfrom
masc2008:set_errno

Conversation

@masc2008
Copy link
Copy Markdown
Contributor

@masc2008 masc2008 commented May 3, 2026

Summary

  • Why: errno is defined as a per-thread value. When set_errno() is called from an interrupt handler, it currently modifies the interrupted task's errno, which violates POSIX semantics because the interrupted task did not request the failing operation.
  • What: Add an up_interrupt_context() guard inside the set_errno() macro so that the assignment is skipped when running in an interrupt handler.
  • How: Include <nuttx/irq.h> and wrap the errno = (int)(e) assignment with if (!up_interrupt_context()).

Impact

  • Is new feature added? Is existing feature changed? NO
  • Impact on user (will user need to adapt to change)? NO
  • Impact on build (will build process change)? NO
  • Impact on hardware (will arch(s) / board(s) / driver(s) change)? NO
  • Impact on documentation (is update required / provided)? NO
  • Impact on security (any sort of implications)? NO
  • Impact on compatibility (backward/forward/interoperability)? NO
  • Anything else to consider or add? This is a defensive fix that prevents silent corruption of a task's errno by ISR code.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): Linux x86_64, GCC 13.4.0 (arm-none-eabi)
  • Target(s): arm/qemu-armv7a:full

Testing logs after change:

there is no log when do it, I tested this under qemu-armv7a and BES chip.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: XS The size of the change in this PR is very small labels May 3, 2026
@ankohuu
Copy link
Copy Markdown
Contributor

ankohuu commented May 3, 2026

Do we need to add an ASSERT and fix already?

set_errno() should not modify the interrupted task's errno.
Add a check using up_interrupt_context() and skip the assignment
when called from an interrupt handler.

Signed-off-by: Jerry Ma <shichunma@bestechnic.com>
@github-actions github-actions Bot added Size: S The size of the change in this PR is small and removed Size: XS The size of the change in this PR is very small labels May 3, 2026
Copy link
Copy Markdown
Contributor Author

@masc2008 masc2008 left a comment

Choose a reason for hiding this comment

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

fixed compile error.

@masc2008
Copy link
Copy Markdown
Contributor Author

masc2008 commented May 3, 2026

Do we need to add an ASSERT and fix already?

some person may have already called some libc function like in interrupt context, it's not a good practice. But I don't think it's good to do such break change. let's start from a small step.

@linguini1
Copy link
Copy Markdown
Contributor

Just an FYI, since we are moving to 13.0.0 next, this would be a good time for the breaking change of adding an assertion.

@acassis acassis merged commit 3c9a9d6 into apache:master May 3, 2026
37 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants