c++ - converting from std::string to const char* causes 'Syscall param socketcall.sendto(msg) points to unaddressable byte(s)' error in valgrind -
while using libmicrohttpd library, came across odd error when converting string const char * , calling mhd_create_response_from_buffer it.
this causes webpage response come badly malformed, displaying binary data and, little more rarely, making browser think file , downloading it.
what makes odd error not show if send regular const char, const char *cstring = "this page"; when i'm converting string const char * const char *cstring = page.c_str();
the valgrind output:
==11105== thread 2: ==11105== syscall param socketcall.sendto(msg) points unaddressable byte(s) ==11105== @ 0x617464b: send (send.c:31) ==11105== 0x565986f: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x565737d: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x565da3c: mhd_run_from_select (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x565dc8a: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x565dda1: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x876b0a3: start_thread (pthread_create.c:309) ==11105== 0x617362c: clone (clone.s:111) ==11105== address 0xe499448 24 bytes inside block of size 56 free'd ==11105== @ 0x4c2a360: operator delete(void*) (vg_replace_malloc.c:507) ==11105== 0x401ca5: http_connect(void*, mhd_connection*, char const*, char const*, char const*, char const*, unsigned long*, void**) (in /home/shpoople/projects/http/main) ==11105== 0x5656f70: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x5658427: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x565d988: mhd_run_from_select (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x565dc8a: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x565dda1: ??? (in /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.10.27.0) ==11105== 0x876b0a3: start_thread (pthread_create.c:309) ==11105== 0x617362c: clone (clone.s:111) ==11105== and function sending data (found @ this page , modified use std::string):
static int send_page (struct mhd_connection *connection, std::string page) { int ret; struct mhd_response *response; const char* cstring = page.c_str(); response = mhd_create_response_from_buffer (strlen (cstring), (void *) cstring, mhd_respmem_persistent); if (!response) { return mhd_no; } ret = mhd_queue_response(connection, mhd_http_ok, response); mhd_destroy_response (response); return ret; }
your parameter std::string page local variable. memory freed when function finishes.
on other hand, function mhd_run_from_select , related apparently run on separate thread. when thread tries access buffer, memory of std::string page has been freed.
you should make sure buffer stays alive, either allocating differently or blocking major thread until response.
Comments
Post a Comment