2011-02-02 68 views
5

所以我有一個非常基本的通過https與Facebook服務器交談的例子,但是valgrind抱怨地抱怨。所以我認爲我沒有設置錯誤的東西......有誰知道我做錯了什麼?當使用https時,valgrind會報告libcurl的問題

這裏是我的代碼:

#include <string> 
#include <iostream> 
#include <curl/curl.h> 

size_t write_fn_impl(void* ptr, size_t size, size_t nmemb, void * data) 
{ 
    std::string * result = static_cast<std::string*>(data); 
    *result += std::string((char*)ptr, size*nmemb); 
    return size*nmemb; 
} 

int main() 
{ 
    std::string url_full="https://graph.facebook.com/me"; 
    std::string useragent = "Facebook API C++ Client (curl)"; 

    CURL * ch_ = curl_easy_init(); 
    char error_buffer[CURL_ERROR_SIZE]; 
    curl_easy_setopt(ch_, CURLOPT_ERRORBUFFER, error_buffer); 
    curl_easy_setopt(ch_, CURLOPT_WRITEFUNCTION, &write_fn_impl); 
    std::string result; 
    curl_easy_setopt(ch_, CURLOPT_WRITEDATA, &result); 
    int id = 1; 
    curl_easy_setopt(ch_, CURLOPT_VERBOSE, id); 

    curl_easy_setopt(ch_, CURLOPT_URL, url_full.c_str()); 
    curl_easy_setopt(ch_, CURLOPT_USERAGENT, useragent.c_str()); 
    curl_easy_setopt(ch_, CURLOPT_CONNECTTIMEOUT, 10); 
    curl_easy_setopt(ch_, CURLOPT_TIMEOUT, 30); 
    curl_easy_perform(ch_); 
    curl_easy_cleanup(ch_); 

    std::cout<< result<<std::endl; 
} 

什麼的valgrind說的是:

==14149== Memcheck, a memory error detector 
==14149== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al. 
==14149== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info 
==14149== Command: ./a.out 
==14149== 
* About to connect() to graph.facebook.com port 443 (#0) 
* Trying 66.220.146.47... * connected 
* Connected to graph.facebook.com (66.220.146.47) port 443 (#0) 
* successfully set certificate verify locations: 
* CAfile: none 
    CApath: /etc/ssl/certs 
==14149== Syscall param write(buf) points to uninitialised byte(s) 
==14149== at 0x4268113: __write_nocancel (in /lib/tls/i686/cmov/libc-2.10.1.so) 
==14149== by 0x44A5A8E: BIO_write (in /lib/i686/cmov/libcrypto.so.0.9.8) 
==14149== by 0x43E49B8: ssl23_write_bytes (in /lib/i686/cmov/libssl.so.0.9.8) 
==14149== by 0x43E39AB: ssl23_connect (in /lib/i686/cmov/libssl.so.0.9.8) 
==14149== by 0x43F0D49: SSL_connect (in /lib/i686/cmov/libssl.so.0.9.8) 
==14149== by 0x4050EB0: ossl_connect_common (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x4052202: Curl_ossl_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x406597F: Curl_ssl_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x403FF1B: Curl_http_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x4046F6D: Curl_protocol_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x404C396: Curl_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x4059B23: Curl_perform (in /usr/lib/libcurl.so.4.1.1) 
==14149== Address 0x47e92df is 15 bytes inside a block of size 21,848 alloc'd 
==14149== at 0x4024C1C: malloc (vg_replace_malloc.c:195) 
==14149== by 0x4446EFD: ??? (in /lib/i686/cmov/libcrypto.so.0.9.8) 
==14149== by 0x444755B: CRYPTO_malloc (in /lib/i686/cmov/libcrypto.so.0.9.8) 
==14149== by 0x44A4EF7: BUF_MEM_grow (in /lib/i686/cmov/libcrypto.so.0.9.8) 
==14149== by 0x43E3BAB: ssl23_connect (in /lib/i686/cmov/libssl.so.0.9.8) 
==14149== by 0x43F0D49: SSL_connect (in /lib/i686/cmov/libssl.so.0.9.8) 
==14149== by 0x4050EB0: ossl_connect_common (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x4052202: Curl_ossl_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x406597F: Curl_ssl_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x403FF1B: Curl_http_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x4046F6D: Curl_protocol_connect (in /usr/lib/libcurl.so.4.1.1) 
==14149== by 0x404C396: Curl_connect (in /usr/lib/libcurl.so.4.1.1) 

和頁面更....

回答

4

MK部分正確。 Valgrind提出了很多關於OpenSSL的垃圾警告。但就我而言,我所看到的崩潰似乎是由於我使用openSSL時遇到了一些麻煩。特別是它沒有正確設置爲多線程使用。 curl文檔涵蓋你需要做什麼..但不知何故,我一定錯過了它)

無論如何這裏抑制文件,幫助我找到它..它可能是有用的..但要小心它可能不'趕上你想要捕捉的一切,它可能捕捉一些事情你不想...

{ 
    openssl-rand-write 
    Memcheck:Param 
    write(buf) 
    fun:__write_nocancel 
    fun:BIO_write 
    fun:ssl23_write_bytes 
    fun:ssl23_connect 
    fun:SSL_connect 
    fun:ossl_connect_common 
} 
{ 
    openssl-rand-write2 
    Memcheck:Param 
    write(buf) 
    fun:__write_nocancel 
    fun:BIO_write 
    fun:ssl3_write_pending 
    obj:/lib/i686/cmov/libssl.so.0.9.8 
    fun:ssl3_write_bytes 
    fun:ssl3_write 
    fun:SSL_write 
} 
{ 
    openssl-rand-write3 
    Memcheck:Param 
    write(buf) 
    fun:__write_nocancel 
    fun:BIO_write 
    ... 
    fun:ossl_connect_common 
} 
{ 
    openssl-rand-uninit_mod_inverse 
    Memcheck:Cond 
    fun:BN_mod_inverse 
    ... 
    obj:/lib/i686/cmov/libcrypto.so.0.9.8 
} 
{ 
    openssl-rand-uninit_div 
    Memcheck:Cond 
    fun:BN_div 
    ... 
    obj:/lib/i686/cmov/libcrypto.so.0.9.8 
} 
{ 
    openssl-uninit-padding 
    Memcheck:Cond 
    fun:RSA_padding_add_PKCS1_type_2 
    obj:/lib/i686/cmov/libcrypto.so.0.9.8 
} 
{ 
    openssl-uninit-ucmp 
    Memcheck:Cond 
    fun:BN_ucmp 
    obj:/lib/i686/cmov/libcrypto.so.0.9.8 
} 
{ 
    openssl-uninit-encrypt 
    Memcheck:Cond 
    obj:/lib/i686/cmov/libcrypto.so.0.9.8 
    fun:RSA_public_encrypt 
} 
{ 
    openssl-uninit-ssl3_read_bytes 
    Memcheck:Cond 
    fun:ssl3_read_bytes 
    fun:ssl3_read 
    fun:SSL_read 
} 
{ 
    openssl-uninit-get_finished 
    Memcheck:Cond 
    fun:ssl3_get_finished 
    fun:ssl3_connect 
    fun:SSL_connect 
    fun:ossl_connect_common 
} 
{ 
    openssl-uninit-read_bytes 
    Memcheck:Cond 
    ... 
    fun:ossl_connect_common 
} 
{ 
    openssl-value4-connect_common 
    Memcheck:Value4 
    ... 
    fun:ossl_connect_common 
} 
{ 
    openssl-uninit-encrypt 
    Memcheck:Cond 
    ...  
    fun:RSA_public_encrypt 
} 
1

最後一次有人試圖以確保OpenSSL的下運行乾淨valgrind Bad Things(tm)發生了: http://blogs.fsfe.org/tonnerre/archives/24

所以我會忽略任何有關OpenSSL的vlagrind警告。

+1

那篇文章很有趣,但它忽略了一個事實的valgrind *沒有發現其實OpenSSL中的一個bug *。 OpenSSL是(並且可能仍然是)依賴*未定義行爲*作爲熵源,而不是從合法來源獲得熵。 Debian添加的非常相同的「bug」可能很容易從編譯器或庫更改中出現,這些更改導致未初始化的數據OpenSSL讀取「不太隨意」。簡而言之,如果valgrind正在報告這樣的問題,那麼它幾乎肯定意味着代碼是*錯誤*,但是微小的修復可能會更糟。 :-) – 2011-02-02 05:15:31