From: Alejandro Mery Date: Thu, 4 Dec 2014 23:09:20 +0000 (+0100) Subject: Subject: [PATCH] Introduce lws_free2() helper to free and re-NULL pointers X-Git-Tag: upstream/1.7.3~455 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ac3ec39bb17941a358c23542c9d4653f043b95f0;p=platform%2Fupstream%2Flibwebsockets.git Subject: [PATCH] Introduce lws_free2() helper to free and re-NULL pointers Signed-off-by: Alejandro Mery --- diff --git a/lib/client.c b/lib/client.c index 4cf6a55..4a094a8 100755 --- a/lib/client.c +++ b/lib/client.c @@ -781,8 +781,7 @@ check_accept: return 0; bail3: - lws_free(wsi->u.ws.rx_user_buffer); - wsi->u.ws.rx_user_buffer = NULL; + lws_free2(wsi->u.ws.rx_user_buffer); close_reason = LWS_CLOSE_STATUS_NOSTATUS; bail2: diff --git a/lib/daemonize.c b/lib/daemonize.c index 413d3fe..f5caf0b 100644 --- a/lib/daemonize.c +++ b/lib/daemonize.c @@ -76,8 +76,7 @@ static void lws_daemon_closing(int sigact) if (getpid() == pid_daemon) if (lock_path) { unlink(lock_path); - lws_free(lock_path); - lock_path = NULL; + lws_free2(lock_path); } kill(getpid(), SIGKILL); diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 63709c3..d873306 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -208,35 +208,26 @@ just_kill_connection: remove_wsi_socket_from_fds(context, wsi); wsi->state = WSI_STATE_DEAD_SOCKET; - - if (wsi->rxflow_buffer) { - lws_free(wsi->rxflow_buffer); - wsi->rxflow_buffer = NULL; - } + + lws_free2(wsi->rxflow_buffer); if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING && wsi->u.hdr.ah) { - lws_free(wsi->u.hdr.ah); - wsi->u.hdr.ah = NULL; + lws_free2(wsi->u.hdr.ah); } - + if ((old_state == WSI_STATE_ESTABLISHED || wsi->mode == LWS_CONNMODE_WS_SERVING || wsi->mode == LWS_CONNMODE_WS_CLIENT)) { - if (wsi->u.ws.rx_user_buffer) { - lws_free(wsi->u.ws.rx_user_buffer); - wsi->u.ws.rx_user_buffer = NULL; - } + lws_free2(wsi->u.ws.rx_user_buffer); if (wsi->truncated_send_malloc) { /* not going to be completed... nuke it */ - lws_free(wsi->truncated_send_malloc); - wsi->truncated_send_malloc = NULL; + lws_free2(wsi->truncated_send_malloc); wsi->truncated_send_len = 0; } if (wsi->u.ws.ping_payload_buf) { - lws_free(wsi->u.ws.ping_payload_buf); - wsi->u.ws.ping_payload_buf = NULL; + lws_free2(wsi->u.ws.ping_payload_buf); wsi->u.ws.ping_payload_alloc = 0; wsi->u.ws.ping_payload_len = 0; } diff --git a/lib/parsers.c b/lib/parsers.c index def5c3b..2b4a900 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -77,10 +77,8 @@ int lws_allocate_header_table(struct libwebsocket *wsi) int lws_free_header_table(struct libwebsocket *wsi) { - if (wsi->u.hdr.ah) { - lws_free(wsi->u.hdr.ah); - wsi->u.hdr.ah = NULL; - } + lws_free2(wsi->u.hdr.ah); + wsi->u.hdr.ah = NULL; return 0; }; @@ -894,8 +892,7 @@ spill: /* if existing buffer is too small, drop it */ if (wsi->u.ws.ping_payload_buf && wsi->u.ws.ping_payload_alloc < wsi->u.ws.rx_user_buffer_head) { - lws_free(wsi->u.ws.ping_payload_buf); - wsi->u.ws.ping_payload_buf = NULL; + lws_free2(wsi->u.ws.ping_payload_buf); } /* if no buffer, allocate it */ diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 6fb1668..c4550e9 100755 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -1168,6 +1168,7 @@ lws_zalloc(size_t size); #define lws_malloc(S) lws_realloc(NULL, S) #define lws_free(P) lws_realloc(P, 0) +#define lws_free2(P) do { lws_realloc(P, 0); (P) = NULL; } while(0) /* * lws_plat_ diff --git a/lib/server.c b/lib/server.c index c75d613..ed5c47b 100644 --- a/lib/server.c +++ b/lib/server.c @@ -295,9 +295,7 @@ got_uri: } /* now drop the header info we kept a pointer to */ - lws_free(wsi->u.http.ah); - /* not possible to continue to use past here */ - wsi->u.http.ah = NULL; + lws_free2(wsi->u.http.ah); if (n) { lwsl_info("LWS_CALLBACK_HTTP closing\n"); @@ -320,11 +318,8 @@ got_uri: bail_nuke_ah: /* drop the header info */ - if (wsi->u.hdr.ah) { - lws_free(wsi->u.hdr.ah); - wsi->u.hdr.ah = NULL; - } - + lws_free2(wsi->u.hdr.ah); + return 1; } diff --git a/lib/service.c b/lib/service.c index c3e9dc5..d06bba1 100644 --- a/lib/service.c +++ b/lib/service.c @@ -564,8 +564,7 @@ drain: if (draining_flow && wsi->rxflow_buffer && wsi->rxflow_pos == wsi->rxflow_len) { lwsl_info("flow buffer: drained\n"); - lws_free(wsi->rxflow_buffer); - wsi->rxflow_buffer = NULL; + lws_free2(wsi->rxflow_buffer); /* having drained the rxflow buffer, can rearm POLLIN */ _libwebsocket_rx_flow_control(wsi); /* n ignored, needed for NO_SERVER case */ }