Fix for memory safety and error handling#110
Open
lokeshmuthuraj wants to merge 2 commits into
Open
Conversation
Contributor
Author
|
@lubaihua33 / @simonxiaoss Please review the PR. Thanks. |
This was referenced Jun 1, 2026
There was a problem hiding this comment.
Pull request overview
This PR aims to harden ntttcp's initialization and cleanup by (1) returning an error from default_ntttcp_test instead of silently leaving bind_address as a string literal, (2) freeing the e->results->threads array that was previously leaked, and (3) explicitly closing tep->synch_socket on six error/return paths in run_ntttcp_sender. The tool version is also bumped to 1.4.4.
Changes:
default_ntttcp_testnowstrdupsbind_addressand returns anint(NO_ERROR/ERROR_MEMORY_ALLOC);mainhandles the failure path.free_ntttcp_test_endpoint_and_testfrees thee->results->threadspointer array in addition to its entries.run_ntttcp_senderclosessynch_socket(guarded byno_synch) before eachreturn ERROR_GENERALand before returning from the continuous-mode sleep path.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/ntttcp.h | Updates default_ntttcp_test declaration to return int. |
| src/ntttcp.c | Drops literal bind_address assignment, strdups "0.0.0.0", returns errors, and frees e->results->threads. |
| src/main.c | Handles default_ntttcp_test error return and closes synch_socket on sender error/return paths. |
| src/const.h | Bumps TOOL_VERSION from 1.4.3 to 1.4.4. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+62
to
+67
| /* Allocate bind_address last to ensure all other fields are initialized if allocation fails */ | ||
| test->bind_address = strdup("0.0.0.0"); | ||
| if (!test->bind_address) { | ||
| PRINT_ERR("failed to allocate memory for bind_address in defaults"); | ||
| return ERROR_MEMORY_ALLOC; | ||
| } |
Comment on lines
+42
to
+43
| if (test->no_synch == false) | ||
| close(tep->synch_socket); |
Comment on lines
+348
to
+355
| // Handle error return from default_ntttcp_test | ||
| err_code = default_ntttcp_test(test); | ||
| if (err_code != NO_ERROR) { | ||
| PRINT_ERR("main: error when initializing default test parameters"); | ||
| free(test->bind_address); | ||
| free(test); | ||
| exit(err_code); | ||
| } |
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 safer initialization patterns and proper error propagation in ntttcp's core infrastructure. It replaces fatal exit() calls with recoverable error codes and ensures consistent resource management patterns that subsequent PRs will build upon.
Problems Solved
1. Fatal Exit on Memory Allocation Failure
Previous behavior
default_ntttcp_test() assigned bind_address to a string literal ("0.0.0.0"), creating inconsistent memory ownership. This would cause crashes if later code tried to free it.
Root cause
The function had no way to signal allocation failures, forcing immediate program termination.
2. Memory Leak in Test Results Cleanup
How found: ASAN, Valgrind
Issue -
ntttcp.c—e->results->threadsarray pointer never freedfree_ntttcp_test_endpoint_and_test() freed individual thread result structures but forgot to free the threads array itself.
Impact
Every test execution leaked the threads array (typically 16-64 bytes per test).
Evidence
3. Missing Socket Close Guards
How found: Static analysis,
valgrind --track-fds=yesIssue -
main.c— sync socket not closed inrun_ntttcp_sender()error pathsSix error paths in
run_ntttcp_sender()returned ERROR_GENERAL without explicitly checking no_synch before closing synch_socket.Impact
While not causing immediate bugs, inconsistent guard patterns made the code harder to reason about and maintain.