curl / Mailing Lists / curl-users / Single Mail

curl-users

Re: valgrind detects memory leaks when using libcurl

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Wed, 8 Mar 2017 21:40:54 +0100

On Wed, Mar 08, 2017 at 05:05:25PM +0100, Ralph Nemitz wrote:
> Hi together,
>
> I am using arch linux and libcurl 7.53.1-1.
> I ran valgrind to check for memory leaks. And to my surprise, I got
> thousands of errors, here only the last context:
>
> ==18435== 3974 errors in context 1000 of 1000:
> ==18435== Conditional jump or move depends on uninitialised value(s)
> ==18435== at 0x4C31CA6: rawmemchr (in
> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
> ==18435== by 0x5895421: _IO_str_init_static_internal (in
> /usr/lib/libc-2.24.so)
> ==18435== by 0x58845E6: __isoc99_vsscanf (in /usr/lib/libc-2.24.so)
> ==18435== by 0x5884586: __isoc99_sscanf (in /usr/lib/libc-2.24.so)
> ==18435== by 0x55BC4F4: ??? (in /usr/lib/libcurl.so.4.4.0)
> ==18435== by 0x55D4F07: ??? (in /usr/lib/libcurl.so.4.4.0)
> ==18435== by 0x55E015D: ??? (in /usr/lib/libcurl.so.4.4.0)
> ==18435== by 0x55E0C50: curl_multi_perform (in /usr/lib/libcurl.so.4.4.0)
> ==18435== by 0x55D6ABF: curl_easy_perform (in /usr/lib/libcurl.so.4.4.0)
> ==18435== by 0x404268: main (dcs_data_transfer.c:744)
> ==18435==
> ==18435== ERROR SUMMARY: 35049 errors from 1000 contexts (suppressed: 0 from
> 0)

There are a number of autobuilds running with Valgrind that aren't showing any
problems, so it's likely something to do with your environment. Note that this
isn't showing a memory leak but rather uninitialized memory access. It could
be a simple bug in this glibc version (it wouldn't be the first one) or it
could be a harmless optimization. You could run it under gdb and take a look at
what curl passes in at that point to see if it's a curl problem or not.

> Here is the code that valgrind complains about:
>
> curl_global_init(CURL_GLOBAL_DEFAULT);
> curl = curl_easy_init();
> if ( curl ) {
> struct curl_slist *headers=NULL;
> headers = curl_slist_append(headers, "Content-Type: text/xml");
> /* pass our list of custom made headers */
> curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
> /* send all data to this function */
> curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
> /* we pass our 'chunk' struct to the callback function */
> curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
> /* CURLOPT_COOKIEFILE - file name to read cookies from */
> curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/etc/farming/cookie.txt" );
> /* CURLOPT_COOKIEJAR - file name to store cookies to */
> curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "/etc/farming/cookie.txt" );
> /* Set the CA info */
> curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/logicway.ca.cert.pem");
> /* This is just the server URL */
> curl_easy_setopt(curl, CURLOPT_URL, url_string);
> /* tell it to "upload" to the URL */
> curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
> /* set where to read from (on Windows you need to use READFUNCTION too) */
> curl_easy_setopt(curl, CURLOPT_READDATA, fd);
> /* and give the size of the upload (optional) */
> curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
> /* enable verbose for easier tracing */
> curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
>
> /* Perform the request, res will get the return code */
> res = curl_easy_perform(curl);
> /* Check for errors */
> if(res != CURLE_OK) {
> Log ( 1, "Line: %d curl_easy_perform() failed: %s\n", __LINE__, curl_easy_strerror(res));
> }
> else {
> Log ( 200, "Line: %d chunk.memory: %s\n", __LINE__, chunk.memory );
> if( strstr( chunk.memory, "err:401") ) {
> cookie_okay = false;
> }
> }
> curl_slist_free_all(headers); /* free the header list */
> /* free the allocated memory */
> free(chunk.memory);
> /* always cleanup */
> curl_easy_cleanup(curl);
> /* close open file */
> fclose( fd );
> }
> /* we're done with libcurl, so clean it up */
> curl_global_cleanup();

I don't spot anything wrong here, but you're only showing a part of the
program. Is the write callback setting all the correct values?

> I think I've done all initializations and also cleanups/frees. Why there are
> so many errors? Does anybody got an idea?
>
> Any help would be appreciated.
-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2017-03-08