From 127878ef734b897ab3d66c7c52775afa4ffd436c Mon Sep 17 00:00:00 2001 From: Andrew Canaday Date: Wed, 5 Nov 2014 14:13:04 -0500 Subject: [PATCH 1/3] Adding header table free function to make sure we free cleanly everywhere. --- lib/parsers.c | 9 +++++++++ lib/private-libwebsockets.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/lib/parsers.c b/lib/parsers.c index 20e6fe926..7e32e8eca 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -73,6 +73,15 @@ int lws_allocate_header_table(struct libwebsocket *wsi) return 0; } +int lws_free_header_table(struct libwebsocket *wsi) +{ + if( wsi->u.hdr.ah ) { + free( wsi->u.hdr.ah ); + wsi->u.hdr.ah = 0; + }; + return 0; +}; + LWS_VISIBLE int lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h) { int n; diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 380af8586..591270c86 100755 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -753,6 +753,9 @@ lws_plat_set_socket_options(struct libwebsocket_context *context, int fd); LWS_EXTERN int lws_allocate_header_table(struct libwebsocket *wsi); +LWS_EXTERN int +lws_free_header_table(struct libwebsocket *wsi); + LWS_EXTERN char * lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h); From f309b1a0f73a1322501605a42e5fcdc6121f9aa6 Mon Sep 17 00:00:00 2001 From: Andrew Canaday Date: Wed, 5 Nov 2014 14:19:08 -0500 Subject: [PATCH 2/3] Don't free wsi->u.hdr.ah except through lws_free_header_table (exception: server.c frees 'ah' after copying the header pointer and transitioning the union state). --- lib/libwebsockets.c | 2 +- lib/server-handshake.c | 5 +---- lib/server.c | 9 +++------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 06c6f58e2..b421975d9 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -85,7 +85,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context, context->protocols[0].callback(context, wsi, LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0); - free(wsi->u.hdr.ah); + lws_free_header_table(wsi); goto just_kill_connection; } diff --git a/lib/server-handshake.c b/lib/server-handshake.c index 56cf9a13e..57b2750a7 100644 --- a/lib/server-handshake.c +++ b/lib/server-handshake.c @@ -271,10 +271,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) bail: /* free up his parsing allocations */ - - if (wsi->u.hdr.ah) - free(wsi->u.hdr.ah); - + lws_free_header_table(wsi); return -1; } diff --git a/lib/server.c b/lib/server.c index da08d07b7..c6f9c8ce2 100644 --- a/lib/server.c +++ b/lib/server.c @@ -463,9 +463,7 @@ int lws_handshake_server(struct libwebsocket_context *context, } /* drop the header info -- no bail_nuke_ah after this */ - - if (wsi->u.hdr.ah) - free(wsi->u.hdr.ah); + lws_free_header_table(wsi); wsi->mode = LWS_CONNMODE_WS_SERVING; @@ -503,8 +501,7 @@ int lws_handshake_server(struct libwebsocket_context *context, bail_nuke_ah: /* drop the header info */ - if (wsi->u.hdr.ah) - free(wsi->u.hdr.ah); + lws_free_header_table(wsi); return 1; } @@ -599,7 +596,7 @@ int lws_server_socket_service(struct libwebsocket_context *context, lwsl_info("lws_server_skt_srv: read 0 len\n"); /* lwsl_info(" state=%d\n", wsi->state); */ if (!wsi->hdr_parsing_completed) - free(wsi->u.hdr.ah); + lws_free_header_table(wsi); /* fallthru */ case LWS_SSL_CAPABLE_ERROR: libwebsocket_close_and_free_session( From 1cc2240bc6108ed12e1b80ce0d9f592321f7d122 Mon Sep 17 00:00:00 2001 From: Andrew Canaday Date: Wed, 5 Nov 2014 14:31:27 -0500 Subject: [PATCH 3/3] Use NULL not 0. --- lib/parsers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parsers.c b/lib/parsers.c index 7e32e8eca..769469052 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -77,7 +77,7 @@ int lws_free_header_table(struct libwebsocket *wsi) { if( wsi->u.hdr.ah ) { free( wsi->u.hdr.ah ); - wsi->u.hdr.ah = 0; + wsi->u.hdr.ah = NULL; }; return 0; };