hujun260 opened a new pull request, #18227:
URL: https://github.com/apache/nuttx/pull/18227
## Summary
Refactor the scheduler getter function to improve code quality by
consolidating multiple return statements into a single exit point. The
implementation inverts the null pointer check and restructures the control
flow, enhancing maintainability and MISRA HIS standards compliance.
## Motivation and Problem
The original nxsched_get_scheduler() function had multiple return statements
scattered throughout the code, with an early return for error conditions. This
violates MISRA HIS coding standards which require single exit point patterns
for improved code predictability and verifiability.
### Safety Standard Compliance
- **MISRA HIS Standard**: Enforces single-exit-point pattern for functions
- **Coverity Defect**: HIS_metric_violation (RETURN) - Multiple return
points detected
- **Pattern**: Invert condition and use result variable for single exit
- **Benefit**: Enhanced code structure, improved static analysis
compatibility
## Changes Made
1. **Result Variable Initialization**: Added `int ret = -ESRCH;` for default
error value
- Represents task not found error condition
- Provides consistent return value on early error path
2. **Condition Inversion**: Changed `if (tcb == NULL)` to `if (tcb != NULL)`
- Inverts the null check logic
- Consolidates success path handling
3. **Single Exit Point**: Moved all logic into inverted condition block
- Policy extraction now within success branch
- Result assignment to ret variable
- Single return statement at end
4. **Code Structure**: Improves readability and maintainability
- Clear separation of error vs success paths
- Unified return mechanism
- Standard control flow pattern
### File Statistics
- **File Modified**: `sched/sched/sched_getscheduler.c`
- **Lines Changed**: 18 (10 insertions, 8 deletions)
- **Type**: Code restructuring for compliance improvement
## Impact Analysis
✅ **Backward Compatibility**: MAINTAINED - Function behavior unchanged
✅ **Code Quality**: IMPROVED - Single exit point pattern implemented
✅ **Return Values**: IDENTICAL - Same error codes and policy values
✅ **Performance**: NO IMPACT - Same logic, improved structure
✅ **Safety**: ENHANCED - MISRA HIS compliance verified
## Verification Checklist
- [x] Compilation successful with all warning flags enabled
- [x] Function behavior unchanged for valid task IDs
- [x] Error return (-ESRCH) unchanged for invalid task IDs
- [x] Scheduling policy extraction preserved and correct
- [x] Policy offset (+1) correctly applied to TCB value
- [x] Condition inversion logic validated
- [x] Single exit point pattern fully implemented
- [x] Coverity HIS_metric_violation (RETURN) defect resolved
- [x] Code review completed - all paths tested
## Testing Scenarios
1. **Valid Task ID (Self)**: Test with pid=0 returns correct scheduling
policy
2. **Valid Task ID (Other)**: Test with valid PID returns correct policy
3. **Invalid Task ID**: Verify invalid PID returns -ESRCH error
4. **Policy Values**: Confirm policy + 1 offset applied correctly
5. **All Policies**: Test with different scheduling policies (FIFO, RR,
SPORADIC)
6. **Null Pointer Path**: Verify null TCB correctly handled with error return
7. **Return Path**: Confirm single return statement execution
## Technical Notes
- Default error value (-ESRCH) set at function start
- Null check inverted to consolidate success path
- Policy extraction and offset only applied when TCB is valid
- Return variable updated only in success branch
- Comment preserved explaining 1-based vs 0-based values
- No functional change to task lookup or policy handling
## Related Issues
- **Category**: Code Quality & Safety Standard Compliance
- **Standard**: MISRA HIS - Single Exit Point Pattern
- **Defect**: Coverity HIS_metric_violation (RETURN)
- **Subsystem**: Kernel scheduler - policy query (sched/sched/)
## Build Information
- **Compiler**: ARM GCC 10.x (primary test environment)
- **Architectures**: ARMv7-A, ARMv7-R, x86_64
- **Target**: NuttX kernel - scheduler subsystem
- **Build Flags**: `-Wall -Wextra -Werror`
## Diff Summary
- Initialize return variable with error value at function start
- Invert null pointer condition for single exit point
- Move policy extraction into success branch
- Apply policy offset and assign to return variable
- Single return statement at function end
- Improved code structure and MISRA compliance
--
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]