hujun260 opened a new pull request, #18219:
URL: https://github.com/apache/nuttx/pull/18219
## Summary
This series contains three related improvements to the reader-writer
semaphore
implementation for MISRA HIS compliance:
1. Consolidate return statements in `down_read_trylock()` with single exit
point
2. Eliminate goto statements in `down_read()` with structured control flow
3. Consolidate return statements in `init_rwsem()` with inverted conditions
## Motivation and Problem
### Part 1: down_read_trylock() RETURN Consolidation
Multiple return statements violate MISRA HIS rules. This change consolidates
returns and replaces goto with if-else blocks for better code clarity.
### Part 2: down_read() HIS_GOTO Elimination
Goto statements violate MISRA HIS safety standards. Structured if-else blocks
improve verifiability and maintain better code flow.
### Part 3: init_rwsem() RETURN Consolidation
Multiple error handling paths with early returns violate MISRA HIS standards.
Inverting conditions improves code structure and compliance.
## Changes
### Commit 1: down_read_trylock()
- Introduce result variable `ret` initialized to 1 (success)
- Replace goto with if-else-else structure
- Replace early return with `ret = 0` assignment
- Single exit point with centralized return
### Commit 2: down_read()
- Replace goto label with if-else structure
- Move writer wait loop into else block
- Move reader increment into nested structure
- Eliminate out: label and goto statements
### Commit 3: init_rwsem()
- Invert error conditions (ret < 0 becomes ret >= 0)
- Consolidate nested initialization into nested if blocks
- Replace multiple early returns with single exit
- Introduce result variable for proper error propagation
## Impact
- **MISRA Compliance**: Achieves HIS_RETURN and HIS_GOTO compliance metrics
- **Code Quality**: Reduced cyclomatic complexity and improved readability
- **Verifiability**: Single exit points improve static analysis capabilities
- **Maintainability**: Structured control flow easier to understand and
modify
- **Backward Compatibility**: No functional changes; identical runtime
behavior
- **Performance**: No performance impact; compiler optimizations identical
## Verification
- [x] Code compiles without warnings on ARM GCC 10.x
- [x] Verified on QEMU ARMv7-A simulator
- [x] Reader-writer semaphore operations verified:
- Read lock acquisition (try and blocking)
- Write lock acquisition and holder detection
- Semaphore initialization with error handling
- [x] All control flow paths verified
- [x] Static analysis shows improved compliance metrics
## Testing
Tested with:
- ARM GCC 10.x compiler
- QEMU ARMv7-A simulation
- Reader-writer semaphore scenarios:
- down_read_trylock() with holder self-detection
- down_read_trylock() with existing writer
- down_read() blocking on writer
- init_rwsem() successful initialization
- init_rwsem() error handling paths
## Related Issues
Addresses Coverity static analysis HIS_metric_violation findings:
- HIS_metric_violation(RETURN) in multiple functions
- HIS_metric_violation(HIS_GOTO) in control flow
## Files Changed
- `sched/semaphore/sem_rw.c` (85 lines total across 3 commits)
- Commit 1: 25 lines (12 insertions, 13 deletions)
- Commit 2: 28 lines (14 insertions, 14 deletions)
- Commit 3: 32 lines (16 insertions, 16 deletions)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]