curl / Mailing Lists / curl-users / Single Mail

curl-users

Re: valgrind detects memory leaks when using libcurl

From: Ralph Nemitz <nemitz_at_logicway.de>
Date: Thu, 9 Mar 2017 13:31:54 +0100

Here is the write callback:

struct MemoryStruct {
   char *memory;
   size_t size;
};

static size_t WriteMemoryCallback(void *contents, size_t size, size_t
nmemb, void *userp)
{
     size_t realsize = size * nmemb;
     struct MemoryStruct *mem = (struct MemoryStruct *)userp;
     mem->memory = realloc(mem->memory, mem->size + realsize + 1);
     if(mem->memory == NULL) {
         /* out of memory! */
         printf("not enough memory (realloc returned NULL)\n");
         return 0;
     }

     memcpy(&(mem->memory[mem->size]), contents, realsize);
     mem->size += realsize;
     mem->memory[mem->size] = 0;

     return realsize;
}

But even if I don't use it, the errcors occur (during valgrind runs):

====1455== Conditional jump or move depends on uninitialised value(s)
==1455== at 0x65E9A52: ecp_nistz256_point_add (in
/usr/lib/libcrypto.so.1.0.0)
==1455== by 0x4C3EB7521D01E48F: ???
==1455== by 0x1D2935B619EF1A6F: ???
==1455== by 0xF2B0C63745A951B6: ???
==1455== by 0x1ED08D0F3ECB45B7: ???
==1455== by 0x305B042E8D3C36D5: ???
==1455== by 0x28E345CC0597BDA9: ???
==1455== by 0x1089364271DCAC0E: ???
==1455== by 0xAD949019C0D7573D: ???
==1455== by 0xCAC3B5147917FCDB: ???
==1455== by 0x6D734C6B92FC4BC: ???
==1455== by 0x927054D13A73A53E: ???
==1455== Uninitialised value was created by a stack allocation
==1455== at 0x55B4770: ??? (in /usr/lib/libcurl.so.4.4.0)
==1455==
==1455==
==1455== More than 1000 different errors detected. I'm not reporting
any more.
==1455== Final error counts will be inaccurate. Go fix your program!
==1455== Rerun with --error-limit=no to disable this cutoff. Note
==1455== that errors may occur in your program without prior warning from
==1455== Valgrind, because errors are no longer being displayed.
==1455==

After I finished valgrind:

==1455== 3956 errors in context 1000 of 1000:
==1455== Conditional jump or move depends on uninitialised value(s)
==1455== at 0x4C31CA6: rawmemchr (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1455== by 0x5895421: _IO_str_init_static_internal (in
/usr/lib/libc-2.24.so)
==1455== by 0x58845E6: __isoc99_vsscanf (in /usr/lib/libc-2.24.so)
==1455== by 0x5884586: __isoc99_sscanf (in /usr/lib/libc-2.24.so)
==1455== by 0x55BC4F4: ??? (in /usr/lib/libcurl.so.4.4.0)
==1455== by 0x55D4F07: ??? (in /usr/lib/libcurl.so.4.4.0)
==1455== by 0x55E015D: ??? (in /usr/lib/libcurl.so.4.4.0)
==1455== by 0x55E0C50: curl_multi_perform (in /usr/lib/libcurl.so.4.4.0)
==1455== by 0x55D6ABF: curl_easy_perform (in /usr/lib/libcurl.so.4.4.0)
==1455== by 0x404268: main (dcs_data_transfer.c:744)
==1455==
==1455== ERROR SUMMARY: 34862 errors from 1000 contexts (suppressed: 0
from 0)

Is it possible, that this has to do how I have compiled the openssl
library (https://www.openssl.org/docs/faq.html#PROG14) ?

Regards, Ralph

Am 09.03.2017 um 12:00 schrieb curl-users-request_at_cool.haxx.se:
> Send curl-users mailing list submissions to
> curl-users_at_cool.haxx.se
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://cool.haxx.se/cgi-bin/mailman/listinfo/curl-users
> or, via email, send a message with subject or body 'help' to
> curl-users-request_at_cool.haxx.se
>
> You can reach the person managing the list at
> curl-users-owner_at_cool.haxx.se
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of curl-users digest..."
>
>
> Today's Topics:
>
> 1. valgrind detects memory leaks when using libcurl (Ralph Nemitz)
> 2. Re: valgrind detects memory leaks when using libcurl
> (Dan Fandrich)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 8 Mar 2017 17:05:25 +0100
> From: Ralph Nemitz <nemitz_at_logicway.de>
> To: curl-users_at_cool.haxx.se
> Subject: valgrind detects memory leaks when using libcurl
> Message-ID: <e327b2fb-e064-cc80-11dc-0087f94a8206_at_logicway.de>
> Content-Type: text/plain; charset=utf-8; format=flowed
>
> 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)
>
> 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 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.
>
> Regards,
>
> Ralph
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 8 Mar 2017 21:40:54 +0100
> From: Dan Fandrich <dan_at_coneharvesters.com>
> To: curl-users_at_cool.haxx.se
> Subject: Re: valgrind detects memory leaks when using libcurl
> Message-ID: <20170308204054.GA20233_at_coneharvesters.com>
> Content-Type: text/plain; charset=iso-8859-1
>
> 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.
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> curl-users mailing list
> curl-users_at_cool.haxx.se
> https://cool.haxx.se/cgi-bin/mailman/listinfo/curl-users
>
>
> ------------------------------
>
> End of curl-users Digest, Vol 139, Issue 6
> ******************************************

-- 
--
###############################################################
Dipl.-Phys. Ralph Nemitz    Tel.:    (0385) 3993 9354
Logic Way GmbH              Fax:     (0385) 3993 458
Hagenower Strasse 73        HTTP:    http://www.logicway.de
19061 Schwerin              E-Mail:  nemitz_at_logicway.de
###############################################################
-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2017-03-09