Improve the robustness of env & binfmt code #17449
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses two issues in NuttX resource and error handling:
Environment Resource Cleanup:
In group_leave.c, the unnecessary #ifndef CONFIG_DISABLE_ENVIRON conditional has been removed. Now, env_release(group) is always called when a group is released, regardless of configuration. This ensures environment variables are reliably cleaned up, reducing the risk of resource leaks especially under corner cases or configuration changes.
Improved Error Handling in Binfmt:
In binfmt_execmodule.c, redundant or misplaced return statements in the error handling code have been fixed, ensuring proper module cleanup and return value handling during exec failures.
These changes improve NuttX's robustness by enforcing more consistent and defensive resource management and error handling.
Impact
Reliability: Strengthens system stability by ensuring task groups always release their environment resources when the last member exits.
Maintainability: Code is cleaner and less dependent on conditional compilation flags, aiding future maintenance.
No API Change: This PR does not introduce any API change and is fully backward compatible.
Compatibility: The resource management logic is now configuration-independent, making it safer for future config changes and custom builds.
Testing
All changes were built and tested under the NuttX simulator (sim:nsh configuration).
Conducted the following tests:
Created and exited multiple tasks and processes to validate group and environment resource release.
Checked for regressions, crashes, and leaks with OSTest.
Verified resource cleanup using allocator logs and valgrind reports.
Host environment: x86_64 Linux
Board/target: Simulator (sim)
No resource leaks or abnormal behaviors were detected.