Fix all Valgrind errors: uninitialized values and memory leak

This commit resolves all Valgrind errors reported in the test run:

1. Fixed uninitialized 'removed' array in tcp_translate_events (tcp.c:179)
   - Added memset to initialize the array to zero before use

2. Fixed uninitialized RequestQueue in client initialization (client.c)
   - Added initialization of metadata_server.reqs and chunk_servers[].reqs
   - Added forward declaration of request_queue_init function

3. Fixed uninitialized descriptor generation field (system.c:324)
   - Initialize generation to 0 when creating process descriptors
   - Prevents uninitialized value errors in handle_to_desc

4. Fixed 34-byte memory leak in tcp_context_free (tcp.c:91-103)
   - Added cleanup of connection byte queues when freeing TCP context
   - Frees both input and output ByteQueues for all connections

All Valgrind errors have been resolved:
- ERROR SUMMARY: 0 errors from 0 contexts
- All heap blocks freed - no leaks possible
This commit is contained in:
Claude
2025-11-02 13:24:18 +00:00
parent eab8fd118c
commit 3ae2f13610
3 changed files with 27 additions and 1 deletions
+9
View File
@@ -90,6 +90,14 @@ void tcp_context_init(TCP *tcp)
void tcp_context_free(TCP *tcp)
{
// Free all connection byte queues without closing sockets
// (sockets are managed by the simulation and will be cleaned up separately)
for (int i = 0; i < tcp->num_conns; i++) {
byte_queue_free(&tcp->conns[i].input);
byte_queue_free(&tcp->conns[i].output);
}
tcp->num_conns = 0;
if (tcp->listen_fd != INVALID_SOCKET)
CLOSE_SOCKET(tcp->listen_fd);
}
@@ -177,6 +185,7 @@ int tcp_register_events(TCP *tcp, void **contexts, struct pollfd *polled)
int tcp_translate_events(TCP *tcp, Event *events, void **contexts, struct pollfd *polled, int num_polled)
{
bool removed[MAX_CONNS+1];
memset(removed, 0, sizeof(removed));
int num_events = 0;
for (int i = 0; i < num_polled; i++) {