diff options
author | Gisle Vanem <gvanem@yahoo.no> | 2021-11-10 04:19:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-09 19:19:25 -0800 |
commit | af2e8c3ca3f630887a3ce08385a771df6b2d43bf (patch) | |
tree | 7ea503e9239edd36712d2bef1aafbe8ba478c512 | |
parent | 3d0c53b63d3cfd2608e7614777d2fb01dc67d367 (diff) |
Plug server mem-leaks (#196)
Building server with MSVC and in `_DEBUG` mode, showed 2 memory leaks:
```
tests/server.c(120) : {524} normal block at 0x0E3B5B48, 10047 bytes long.
Data: <HTTP/1.1 200 OK > 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D
tests/common.c(189) : {523} normal block at 0x0E3B5318, 2048 bytes long.
Data: <GET / HTTP/1.1 > 47 45 54 20 2F 20 48 54 54 50 2F 31 2E 31 0D 0A
```
This patch frees the `response` and the plain-text vector.
-rw-r--r-- | tests/server.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/tests/server.c b/tests/server.c index e864003..2242474 100644 --- a/tests/server.c +++ b/tests/server.c @@ -144,7 +144,7 @@ send_response(struct conndata *conn) struct rustls_connection *rconn = conn->rconn; const char *prefix = "HTTP/1.1 200 OK\r\nContent-Length:"; const int body_size = 10000; - const int response_size = strlen(prefix) + 15 + body_size; + size_t response_size = strlen(prefix) + 15 + body_size; char *response = malloc(response_size); size_t n; @@ -156,11 +156,14 @@ send_response(struct conndata *conn) n = sprintf(response, "%s %d\r\n\r\n", prefix, body_size); memset(response + n, 'a', body_size); *(response + n + body_size + 1) = '\0'; - fprintf(stderr, "strlen response %ld\n", strlen(response)); + response_size = strlen(response); + fprintf(stderr, "strlen response %ld\n", response_size); rustls_connection_write( - rconn, (const uint8_t *)response, strlen(response), &n); - if(n != strlen(response)) { + rconn, (const uint8_t *)response, response_size, &n); + + free(response); + if(n != response_size) { fprintf(stderr, "failed to write all response bytes. wrote %ld\n", n); return CRUSTLS_DEMO_ERROR; } @@ -269,6 +272,8 @@ cleanup: if(sockfd > 0) { close(sockfd); } + if(conn->data.data) + free(conn->data.data); free(conn); } |