remove lws_ensure_user_space from public api change return 22/3122/1
authorAndy Green <andy.green@linaro.org>
Mon, 18 Feb 2013 08:30:10 +0000 (16:30 +0800)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 7 Mar 2013 21:01:37 +0000 (13:01 -0800)
The function has a logical problem when the size of the requested
allocation is 0, it will return NULL which is overloaded as
failure.

Actually the whole function is evil as an api, this patch moves
it out of the public API space and fixes it to return 0 for
success or 1 for fail.  Private code does not need to to return
wsi->user_space and public code should only get that from the
callback as discussed on trac recently.

Thanks to Edwin for debugging the problem.

Reported-by: Edwin van den Oetelaar <oetelaar.automatisering@gmail.com>
Signed-off-by: Andy Green <andy.green@linaro.org>
lib/client.c
lib/handshake.c
lib/libwebsockets.c
lib/libwebsockets.h
lib/private-libwebsockets.h
lib/server-handshake.c
libwebsockets-api-doc.html

index 6422fb7..576c7ff 100644 (file)
@@ -596,8 +596,7 @@ check_accept:
        }
 
        /* allocate the per-connection user memory (if any) */
-       if (wsi->protocol->per_session_data_size &&
-                                        !libwebsocket_ensure_user_space(wsi)) {
+       if (libwebsocket_ensure_user_space(wsi)) {
                lwsl_err("Problem allocating wsi user mem\n");
                goto bail2;
        }
index b21d73d..50138dd 100644 (file)
@@ -122,7 +122,7 @@ libwebsocket_read(struct libwebsocket_context *context,
                        lwsl_info("HTTP request for '%s'\n",
                                lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI));
 
-                       if (libwebsocket_ensure_user_space(wsi) == NULL) {
+                       if (libwebsocket_ensure_user_space(wsi)) {
                                /* drop the header info */
                                if (wsi->u.hdr.ah)
                                        free(wsi->u.hdr.ah);
index 849ffb4..65adcf5 100644 (file)
@@ -2154,16 +2154,11 @@ libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
        return wsi->u.ws.rsv;
 }
 
-/**
- * libwebsocket_ensure_user_space(): return the user context for the connection if possible
- * @wsi:       websocket connection instance
- */
-
-void *
+int
 libwebsocket_ensure_user_space(struct libwebsocket *wsi)
 {
        if (!wsi->protocol)
-               return NULL;
+               return 1;
 
        /* allocate the per-connection user memory (if any) */
 
@@ -2172,12 +2167,12 @@ libwebsocket_ensure_user_space(struct libwebsocket *wsi)
                                  wsi->protocol->per_session_data_size);
                if (wsi->user_space  == NULL) {
                        lwsl_err("Out of memory for conn user space\n");
-                       return NULL;
+                       return 1;
                }
                memset(wsi->user_space, 0,
                                         wsi->protocol->per_session_data_size);
        }
-       return wsi->user_space;
+       return 0;
 }
 
 static void lwsl_emit_stderr(int level, const char *line)
index e1c04cc..779af2e 100644 (file)
@@ -880,9 +880,6 @@ libwebsocket_is_final_fragment(struct libwebsocket *wsi);
 LWS_EXTERN unsigned char
 libwebsocket_get_reserved_bits(struct libwebsocket *wsi);
 
-LWS_EXTERN void *
-libwebsocket_ensure_user_space(struct libwebsocket *wsi);
-
 LWS_EXTERN int
 libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable);
 
index 6c2ae74..3bde491 100644 (file)
@@ -508,6 +508,9 @@ LWS_EXTERN int
 lws_hdr_simple_create(struct libwebsocket *wsi,
                                enum lws_token_indexes h, const char *s);
 
+LWS_EXTERN int
+libwebsocket_ensure_user_space(struct libwebsocket *wsi);
+
 #ifndef LWS_NO_SERVER
 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
                                                      struct libwebsocket *wsi);
index 627fb31..75938fe 100644 (file)
@@ -75,8 +75,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
        }
 
        /* allocate the per-connection user memory (if any) */
-       if (wsi->protocol->per_session_data_size &&
-                                         !libwebsocket_ensure_user_space(wsi))
+       if (libwebsocket_ensure_user_space(wsi))
                goto bail;
 
        /* create the response packet */
index 306f18e..c0ea6cd 100644 (file)
@@ -382,16 +382,6 @@ Some apis can act on all live connections of a given protocol,
 this is how you can get a pointer to the active protocol if needed.
 </blockquote>
 <hr>
-<h2>libwebsocket_ensure_user_space - </h2>
-<i>void *</i>
-<b>libwebsocket_ensure_user_space</b>
-(<i>struct libwebsocket *</i> <b>wsi</b>)
-<h3>Arguments</h3>
-<dl>
-<dt><b>wsi</b>
-<dd>websocket connection instance
-</dl>
-<hr>
 <h2>lws_set_log_level - Set the logging bitfield</h2>
 <i>void</i>
 <b>lws_set_log_level</b>