hujun260 opened a new pull request, #17941:
URL: https://github.com/apache/nuttx/pull/17941
## Summary
This PR fixes an issue where the argv pointer in the task info structure
is not properly initialized, leading to invalid memory access when
`nxsched_get_stackargs()` is called during task enumeration (e.g., by
the `ps` command).
The problem occurs because the TCB is initialized with a valid PID early
in the task creation process, but the argvstack is not initialized at
that time. This can result in `nxsched_get_stackargs()` returning invalid
addresses.
## Changes
The fix initializes the argv pointer to NULL across the task creation and
management lifecycle:
1. **tls_initinfo.c**: Initialize argv to NULL for all new tasks
2. **tls_dupinfo.c**: Preserve argv initialization when duplicating task info
3. **task_setup.c**: Ensure argv is properly initialized during task setup
4. **task_argvstr.c**: Add proper validity checks before using argv
5. **sched_get_tls.c**: Update pointer formatting for consistency
6. **include/nuttx/tls.h**: Add argv field to task_info_s structure
This approach ensures that argv is always valid and can be safely checked
with NULL comparisons before being dereferenced.
## Testing
Tested on:
- **Platform**: NuttX simulator environment
- **Target**: Task enumeration and scheduling subsystem
- **Method**: Verified argv pointer initialization throughout task lifecycle
- Task creation and initialization
- Task duplication and TLS copying
- Task enumeration via ps command
- Argument string retrieval via nxsched_get_stackargs
- **Result**: argv pointer is safely initialized to NULL and validated
before use, preventing invalid memory access
esp32s3-devkit:nsh
nsh>
nsh> uname -a
NuttX 12.12.0 27e915b0ee6-dirty Jan 15 2026 19:26:47 xtensa esp32s3-devkit
nsh> ostest
stdio_test: write fd=1
stdio_test: Standard I/O Check: printf
stdio_test: write fd=2
stdio_test: Standard I/O Check: fprintf to stderr
ostest_main: putenv(Variable1=BadValue3)
ostest_main: setenv(Variable1, GoodValue1, TRUE)
ostest_main: setenv(Variable2, BadValue1, FALSE)
ostest_main: setenv(Variable2, GoodValue2, TRUE)
ostest_main: setenv(Variable3, GoodValue3, FALSE)
## Impact
- **Stability**: Prevents potential kernel crashes from invalid memory access
- **Compatibility**: No breaking changes; all existing APIs remain unchanged
- **Performance**: Negligible impact; only adds a NULL pointer initialization
- **Code Quality**: Improves robustness of task management subsystem
--
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]