[Libwebsockets] Strange off by one on lws_hdr_total_length & lws_hdr_copy

"Andy Green (林安廸)" andy at warmcat.com
Fri Mar 7 01:15:59 CET 2014


On 07/03/14 03:54, the mail apparently from Bas Appelo included:
> Hi,
>
>
> Today I fought a bit with the mentioned function, (lws_hdr_total_length & lws_hdr_copy), and the code I had was:
>
>
>         const int size = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE);
>
>          if(size) {
>              std::vector<char> buffer;
>              buffer.resize(size + 1);
>
>              char *cBuf = &(buffer.at(0));
>              lws_hdr_copy(wsi, cBuf, size + 1, WSI_TOKEN_HTTP_COOKIE);
>
>
> Now this works, the weird thing is the line:
>              lws_hdr_copy(wsi, cBuf, size + 1, WSI_TOKEN_HTTP_COOKIE);
> especially the size + 1, if I just give it size, the cBuf will stay empty, while size is just returned from lws_hdr_total_length, why the extra 1 to make it work?

Isn't it just space for a terminating '\0'?

"a" has length 1 but in most cases you want the terminating NUL, meaning 
it needs 2 bytes (len + 1).

-Andy

> Anyways, could also be me, otherwise good luck!
> Bas
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>




More information about the Libwebsockets mailing list