Skip to content

Conversation

@fxysunshine
Copy link
Contributor

@fxysunshine fxysunshine commented Feb 2, 2026

Summary

This PR fixes MISRA C:2012 Rule 10.4 violations in cancellation point handling code by ensuring consistent use of unsigned operands in bitwise operations.

Changes Made

Modified all CANCEL_FLAG_* macro definitions and their usage sites to use unsigned literals (1u instead of 1):

Macro definitions (include/nuttx/cancelpt.h):

  • CANCEL_FLAG_NONCANCELABLE: Changed from (1 << 0) to (1u << 0)
  • CANCEL_FLAG_CANCEL_ASYNC: Changed from (1 << 1) to (1u << 1)
  • CANCEL_FLAG_CANCEL_PENDING: Changed from (1 << 2) to (1u << 2)

Usage sites (18 locations across 4 files):

  • libs/libc/sched/task_cancelpt.c: 7 comparisons updated
  • libs/libc/sched/task_setcancelstate.c: 3 comparisons updated
  • libs/libc/sched/task_setcanceltype.c: 3 comparisons updated
  • sched/task/task_cancelpt.c: 2 comparisons updated

All bitwise AND operations now compare against 0u instead of 0 to maintain unsigned arithmetic consistency.

Why This Change is Needed

MISRA C:2012 Rule 10.4 prohibits mixing signed and unsigned operands in arithmetic operations. The original code violated this rule by:

  1. Using signed integer literals (1) in bit shift operations
  2. Comparing bitwise results against signed zero (0)

This could lead to:

  • Undefined behavior in edge cases
  • Compiler warnings in strict compliance mode
  • Potential portability issues across different platforms

Impact

Stability: No impact - purely type-safety improvements
Compatibility: No breaking changes - all modifications preserve existing behavior
Code Quality: Positive - eliminates 18 MISRA C:2012 Rule 10.4 violations

Testing

Test Environment

  • Host: Ubuntu 22.04 x86_64
  • Toolchain: GCC 13.1.0
  • Target: sim:nsh configuration
  • Build: CMake + Ninja

Test Steps

  1. Build verification:
cd nuttx
cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
cmake --build build -j
  1. Results:
  • MISRA/Coverity: PASS (no new issues introduced; targeted findings addressed)
  • sim:nsh (CMake): PASS (build + NSH smoke)

@fxysunshine fxysunshine marked this pull request as draft February 2, 2026 08:07
@github-actions github-actions bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Feb 2, 2026
Fix violations of MISRA C:2012 Rule 10.4 (operand of unsigned and signed)
in cancellation point handling code.

Changed all CANCEL_FLAG_* macro definitions and their usage to use unsigned
literals (1u instead of 1) to ensure consistent unsigned arithmetic when
performing bitwise operations. This eliminates mixed signed/unsigned operand
violations in:

- CANCEL_FLAG_NONCANCELABLE
- CANCEL_FLAG_CANCEL_ASYNC
- CANCEL_FLAG_CANCEL_PENDING

The changes affect cancellation point entry/exit logic, cancellation state
management, and cancellation type handling across both kernel and libc
implementations.

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
@fxysunshine fxysunshine force-pushed the fix-cancel-flag-misra-c-10-4 branch from c38dcc3 to 433be2f Compare February 2, 2026 08:08
@github-actions github-actions bot added the Size: S The size of the change in this PR is small label Feb 2, 2026
@jerpelea jerpelea changed the title Fix cancel flag misra c 10 4 sched: Fix cancel flag misra c 10 4 Feb 2, 2026
@fxysunshine fxysunshine changed the title sched: Fix cancel flag misra c 10 4 Fix CANCEL_FLAG_* macro definitions MISRA C:2012 Rule 10.4 violations Feb 2, 2026
@fxysunshine fxysunshine marked this pull request as ready for review February 2, 2026 09:32
@jerpelea jerpelea changed the title Fix CANCEL_FLAG_* macro definitions MISRA C:2012 Rule 10.4 violations sched: Fix CANCEL_FLAG_* macro definitions MISRA C:2012 Rule 10.4 violations Feb 2, 2026
Copy link
Contributor

@jerpelea jerpelea left a comment

Choose a reason for hiding this comment

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

for the future please use PR names that include the area

ex:
sched: Fix CANCEL_FLAG_* macro definitions MISRA C:2012 Rule 10.4 violations

NOTE:
This time I updated the title

@xiaoxiang781216 xiaoxiang781216 merged commit a724a5f into apache:master Feb 2, 2026
40 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: M The size of the change in this PR is medium 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.

3 participants