c++stringmemory-managementlibwebsockets

Libwesockets.h: Issue with lws_write: C++ string to C conversion and send


I'm using g++.

Code:

        std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        char *cstr = strdup(str.c_str());
        lwsl_notice("\n%s", cstr);
        return lws_write(wsi, (unsigned char*)cstr, strlen(cstr), LWS_WRITE_TEXT);

This also doesn't work:

        std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        return lws_write(wsi, (unsigned char*)str.c_str(), strlen(str.c_str()), LWS_WRITE_TEXT);

But this works fine (runs many times without some error):

        char cstr[96] = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        return lws_write(wsi, (unsigned char*)cstr, strlen(cstr), LWS_WRITE_TEXT);

Tried also to create string with malloc but this doesn't work as well:

        std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        char *cstr = (char *)malloc((str.length() + 1) * sizeof(char));
        strcpy(cstr, str.c_str());
        lwsl_notice("\n%s", cstr);
        return lws_write(wsi, (unsigned char*)cstr, strlen(cstr), LWS_WRITE_TEXT);

I can run this code couple times but eventually I'm getting this error: free(): invalid next size (fast) (fails after data was sent)

I tried also couple experiments with LWS_PRE but when I add this to the string it adds couple symbols at the start of message like: a":

When I try free(cstr) after sending the data it fails immediately with double free or corruption (out) error.

lws version: 1.7.1 os: ubuntu x64


Solution

  • According to documentation https://libwebsockets.org/lws-api-doc-master/html/group__sending-data.html#gafd5fdd285a0e25ba7e3e1051deec1001

    IMPORTANT NOTICE!

    When sending with websocket protocol

    LWS_WRITE_TEXT, LWS_WRITE_BINARY, LWS_WRITE_CONTINUATION, LWS_WRITE_PING, LWS_WRITE_PONG,

    or sending on http/2,

    the send buffer has to have LWS_PRE bytes valid BEFORE the buffer pointer you pass to lws_write().

    This means that you have to allocate extra LWS_PRE bytes for your buffer i.e

    std::string str(LWS_PRE, ' '); //Allocate LWS_PRE bytes
    str += "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}"
    return lws_write(wsi, (unsigned char*)&str[LWS_PRE], str.size(), LWS_WRITE_TEXT);
    

    Using malloc

    char* createMessage() {
      std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
      char *cstr = (char *)malloc(LWS_PRE + (str.length() + 1) * sizeof(char));
      cstr += LWS_PRE;
      strcpy(cstr, str.c_str());
      lwsl_notice("\n%s", cstr);
      return cstr;
    }
    ...
    char* msg = createMessage();
    lws_write(wsi, 
              (unsigned char*)msg, 
              strlen(msg) /* add 1 if receiver expects the null character*/, 
              LWS_WRITE_TEXT);
    free(msg - LWS_PRE);