Subject: [PATCH] Introduce lws_free2() helper to free and re-NULL pointers
authorAlejandro Mery <amery@geeks.cl>
Thu, 4 Dec 2014 23:09:20 +0000 (00:09 +0100)
committerAndy Green <andy.green@linaro.org>
Thu, 4 Dec 2014 23:27:52 +0000 (07:27 +0800)
Signed-off-by: Alejandro Mery <amery@geeks.cl>
lib/client.c
lib/daemonize.c
lib/libwebsockets.c
lib/parsers.c
lib/private-libwebsockets.h
lib/server.c
lib/service.c

index 4cf6a55..4a094a8 100755 (executable)
@@ -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:
index 413d3fe..f5caf0b 100644 (file)
@@ -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);
index 63709c3..d873306 100644 (file)
@@ -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;
                }
index def5c3b..2b4a900 100644 (file)
@@ -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 */
index 6fb1668..c4550e9 100755 (executable)
@@ -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_
index c75d613..ed5c47b 100644 (file)
@@ -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;
 }
 
index c3e9dc5..d06bba1 100644 (file)
@@ -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 */
                }