Skip to content

Commit 8d83b41

Browse files
authored
HTTP TCP server: fix realloc failure handling to prevent request buffer leak (#1990)
* Update http_tcp_server.c * Update new_tcp_server.c
1 parent 3eb8ef8 commit 8d83b41

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/httpserver/http_tcp_server.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ static void tcp_client_thread(beken_thread_arg_t arg)
9595
}
9696
// grow by 1024
9797
request.receivedLenmax += 1024;
98-
request.received = (char*)realloc(request.received, request.receivedLenmax+2);
99-
if (request.received == NULL) {
98+
char *newbuf = (char*)realloc(request.received, request.receivedLenmax + 2);
99+
if (newbuf == NULL) {
100100
// no memory
101-
return;
101+
goto exit;
102102
}
103+
request.received = buf = newbuf;
103104
}
104105
request.received[request.receivedLen] = 0;
105106
#endif

src/httpserver/new_tcp_server.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ static void tcp_client_thread(tcp_thread_t* arg)
8585
}
8686
// grow by INCOMING_BUFFER_SIZE
8787
request.receivedLenmax += INCOMING_BUFFER_SIZE;
88-
request.received = (char*)realloc(request.received, request.receivedLenmax + 2);
89-
if(request.received == NULL)
88+
char *newbuf = (char*)realloc(request.received, request.receivedLenmax + 2);
89+
if(newbuf == NULL)
9090
{
9191
// no memory
9292
goto exit;
9393
}
94+
request.received = buf = newbuf;
9495
}
9596
request.received[request.receivedLen] = 0;
9697

0 commit comments

Comments
 (0)