Parameter parsing - memory leak fixes#111
Open
lokeshmuthuraj wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
@lubaihua33 / @simonxiaoss Please review the PR. Thanks. |
There was a problem hiding this comment.
Pull request overview
Establishes consistent heap-based ownership of test->bind_address across the parameter parsing subsystem to eliminate memory leaks, dangling-pointer reads, and free-of-string-literal crashes (notably for IPv6 and -m/-r/-s flags). Also propagates errors from default_ntttcp_test and process_mappings, and finishes cleanup of the threads array and bind_address in test endpoint teardown.
Changes:
process_mappings:strdupthe parsed bind address (instead of pointing into astrsepbuffer), check thestrdup(test->mapping)allocation, and free that buffer on all (most) return paths; propagate its return value fromparse_arguments(-m).- Replace string-literal assignments to
test->bind_addresswithstrdupcopies indefault_ntttcp_test,verify_args(IPv6"::"), and the-r/-shandlers; free the previous value before reassigning;free(test->bind_address)during endpoint cleanup. run_ntttcp_sender: closesynch_socket(guarded by!no_synch) on six previously-missed error paths; bump tool version to1.4.5; fix mixed-tab/space indentation aroundcase 'a'.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/parameter.c | Heap-allocate bind_address in process_mappings, verify_args, and -r/-s; add strdup/allocation checks; free strsep buffer; propagate process_mappings errors. |
| src/ntttcp.h | Change default_ntttcp_test signature to return int to allow error propagation. |
| src/ntttcp.c | default_ntttcp_test returns int and strdup's "0.0.0.0"; free e->results->threads array and e->test->bind_address in endpoint cleanup. |
| src/main.c | Handle new error return from default_ntttcp_test; close synch_socket on six sender error paths under !no_synch. |
| src/const.h | Bump TOOL_VERSION from 1.4.3 to 1.4.5. |
Comments suppressed due to low confidence (1)
src/main.c:363
- After this PR,
test->bind_addressis heap-owned (strdup'd indefault_ntttcp_test,process_mappings,verify_args, and the-r/-shandlers). These error-exit paths onlyfree(test)and leaktest->bind_address. While the process is exiting, this is inconsistent with the new ownership model established in this PR and will be reported by leak sanitizers (the same tooling used to find the leaks this PR fixes). Consider freeingtest->bind_addressbeforefree(test)on all three exit paths (parse_arguments failure on line 361, verify_args failure on line 369, and check_resource_limit failure on line 380).
if (err_code != NO_ERROR) {
PRINT_ERR("main: error when parsing args");
print_flags(test);
free(test);
exit(-1);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
239
to
240
| } | ||
| ++state; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
This PR establishes consistent heap-based memory ownership for bind_address throughout the parameter parsing subsystem. It fixes critical memory leaks where temporary buffer pointers and string literals were assigned to bind_address, which would later be freed, causing segmentation faults.
Depends on: PR #110
Contains changes from PR #110 but PR 110 will be merged before this one.
Problems Solved
1. Dangling Pointer in process_mappings()
How found: ASAN, static analysis
Issue -
parameter.c—strdupresult leaked inprocess_mappings()process_mappings()callsstrdup(test->mapping)and stores the result inelement.strsep()then advances theelementpointer. When an invalid CPU ID is detected, the function returnsERROR_ARGSwithout freeing the original allocation, leaking memory on every call. Even worse, there were 4 early return paths that leaked this allocation.Impact
Memory leak on every -m flag usage (default: every test run since default mapping is "16,,").
2. String Literal Assignment
Issue
process_mappings() assigned test->bind_address = token where token was a pointer into the element buffer. This created a dangling pointer when element was freed.
Impact
Accessing test->bind_address after cleanup would read freed memory. If later freed, would cause a double-free crash.
3. IPv6 String Literal Assignment
Issue
In verify_args(), IPv6 mode unconditionally assigned test->bind_address = "::" (string literal). When cleanup tried to free this read-only memory, it caused segmentation faults.
Impact
All IPv6 tests crashed with exit code 139 (SIGSEGV).
4. Command-Line Argument Pointer Assignment
How found: Static analysis,
valgrind --track-fds=yesIssue -
main.c— sync socket not closed inrun_ntttcp_sender()error pathsIn parse_arguments(), both -r and -s assigned test->bind_address = optarg or argv[optind++]. These point into the argv array, which should not be freed.
Impact
Cleanup attempting to free argv memory would crash the program.
5. Missing Error Propagation
Issue
process_mappings() could fail but its return value was ignored in parse_arguments().
Impact
Parsing errors were silently ignored, leading to undefined behavior.