fixed segfault with double free of get ranged request
authorKevron Rees <kevron_m_rees@linux.intel.com>
Mon, 20 May 2013 19:27:55 +0000 (12:27 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Mon, 20 May 2013 19:27:55 +0000 (12:27 -0700)
plugins/websocketsink/websocketsinkmanager.cpp
plugins/websocketsourceplugin/websocketsource.cpp

index 5ada644..72bc97b 100644 (file)
@@ -478,25 +478,32 @@ static int websocket_callback(struct libwebsocket_context *context,struct libweb
                        //TODO: Verify that ALL requests get sent via LWS_CALLBACK_HTTP, so we can use that instead of LWS_CALLBACK_RECIEVE
                        //TODO: Do we want exceptions, or just to return an invalid json reply? Probably an invalid json reply.
                        DebugOut() << __SMALLFILE__ << ":" << __LINE__ << " Requested: " << (char*)in << "\n";
-                       GError* error = nullptr;
 
+                       std::string tempInput((char*)in);
 
                        json_object *rootobject;
                        json_tokener *tokener = json_tokener_new();
                        enum json_tokener_error err;
                        do
                        {
-                               rootobject = json_tokener_parse_ex(tokener, (char*)in,len);
+                               rootobject = json_tokener_parse_ex(tokener, tempInput.c_str(),len);
                        } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
                        if (err != json_tokener_success)
                        {
                                fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
+                               throw std::runtime_error("JSON Parsing error");
                                // Handle errors, as appropriate for your application.
                        }
+                       if(!rootobject)
+                       {
+                               DebugOut(0)<<"failed to parse json: "<<tempInput<<endl;
+                       }
+
                        if (tokener->char_offset < len) // XXX shouldn't access internal fields
                        {
                                // Handle extra characters after parsed object as desired.
                                // e.g. issue an error, parse another object from that point, etc...
+
                        }
                        // Success, use jobj here.
                        json_object *typeobject = json_object_object_get(rootobject,"type");
@@ -766,7 +773,7 @@ bool gioPollingFunc(GIOChannel *source, GIOCondition condition,gpointer data)
 
        if(condition & G_IO_ERR)
        {
-               DebugOut(0)<<"websocketsink polling error."<<endl;
+               DebugOut(0)<< __SMALLFILE__ <<":"<< __LINE__ <<" websocketsink polling error."<<endl;
        }
 
        if (condition & G_IO_HUP)
index 3a10e68..6369aeb 100644 (file)
@@ -393,13 +393,6 @@ static int callback_http_only(libwebsocket_context *context,struct libwebsocket
                                        {
                                                DebugOut() << "getRanged methodReply has been recieved, without a request being in!. This is likely due to a request coming in after the timeout has elapsed.\n";
                                        }
-                                       while (propertylist.size() > 0)
-                                       {
-                                               
-                                               AbstractPropertyType *type = propertylist.front();
-                                               delete type;
-                                               propertylist.pop_front();
-                                       }
                                }
                                else if (name == "get")
                                {
@@ -446,11 +439,7 @@ static int callback_http_only(libwebsocket_context *context,struct libwebsocket
                                json_object_put(dataobject);
                        }
                        json_object_put(rootobject);
-                       ///TODO: this will probably explode:
-                       //mlc: I agree with Kevron here, it does explode.
-                       //if(error) g_error_free(error);
 
-                       
                        break;
 
                }
@@ -465,16 +454,12 @@ static int callback_http_only(libwebsocket_context *context,struct libwebsocket
                  DebugOut(5) << __SMALLFILE__ << ":" << __LINE__ << "Adding poll for websocket IO channel" << endl;
                        //Add a FD to the poll list.
                        GIOChannel *chan = g_io_channel_unix_new(libwebsocket_get_socket_fd(wsi));
+
                        /// TODO: I changed this to be more consistent with the websocket sink end. it may not be correct. TEST
 
                        g_io_add_watch(chan,GIOCondition(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),(GIOFunc)gioPollingFunc,0);
                        g_io_channel_set_close_on_unref(chan,true);
                        g_io_channel_unref(chan); //Pass ownership of the GIOChannel to the watch.
-
-                       //g_io_add_watch(chan,G_IO_PRI,(GIOFunc)gioPollingFunc,0);
-                       //g_io_add_watch(chan,G_IO_ERR,(GIOFunc)gioPollingFunc,0);
-                       //g_io_add_watch(chan,G_IO_HUP,(GIOFunc)gioPollingFunc,0);
-                       
                        
                        break;
                }
@@ -570,7 +555,7 @@ void WebSocketSource::getPropertyAsync(AsyncPropertyReply *reply)
        
        s << "{\"type\":\"method\",\"name\":\"get\",\"data\":[\"" << reply->property << "\"],\"transactionid\":\"" << uuid << "\"}";
        string replystr = s.str();
-       DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Reply:" << replystr <<endl;
+       DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Sending:" << replystr <<endl;
        //printf("Reply: %s\n",replystr.c_str());
        char *new_response = new char[LWS_SEND_BUFFER_PRE_PADDING + strlen(replystr.c_str()) + LWS_SEND_BUFFER_POST_PADDING];
        new_response+=LWS_SEND_BUFFER_PRE_PADDING;