binance-exchange / binacpp

Binance C++ library
MIT License
214 stars 108 forks source link

curl用法不当,造成不知名的错误 #37

Closed techiethink closed 5 years ago

techiethink commented 5 years ago

错误信息如下:

0 0x00007f7eef07a837 in raise () from /lib64/libc.so.6

1 0x00007f7eef07be7a in abort () from /lib64/libc.so.6

2 0x00007f7eef9b6192 in __gnu_cxx::__verbose_terminate_handler () at ../../../../libstdc++-v3/libsupc++/vterminate.cc:95

3 0x00007f7eef9b4076 in cxxabiv1::terminate(void (*)()) () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:47

4 0x00007f7eef9b40b1 in std::terminate () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:57

5 0x00007f7eef9b42f3 in cxxabiv1::cxa_throw (obj=, tinfo=0x7f7eefc99690 ,

dest=0x7f7eef9b2500 <std::bad_alloc::~bad_alloc()>) at ../../../../libstdc++-v3/libsupc++/eh_throw.cc:95

6 0x00007f7eef9b47bc in operator new (sz=280366331249121) at ../../../../libstdc++-v3/libsupc++/new_op.cc:54

7 0x00007f7eefa4233b in std::__cxx11::basic_string<char, std::char_traits, std::allocator >::_M_mutate (

this=this@entry=0x7f7ee97f1e40, __pos=140183165624672, __len1=__len1@entry=0, 
__s=0x7f7edc0f5e00 "[{\"symbol\":\"ETHBTC\",\"price\":\"0.02928100\"},{\"symbol\":\"LTCBTC\",\"price\":\"0.01518500\"},{\"symbol\":\"BNBBTC\",\"price\":\"0.00387090\"},{\"symbol\":\"NEOBTC\",\"price\":\"0.00149900\"},{\"symbol\":\"QTUMETH\",\"price\":\"0.0133"..., 
__len2=16384) at /data/software/gcc-8.3.0/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc:310

8 0x00007f7eefa438d3 in std::__cxx11::basic_string<char, std::char_traits, std::allocator >::_M_append (

this=0x7f7ee97f1e40, __s=<optimized out>, __n=<optimized out>)
at /data/software/gcc-8.3.0/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:287

9 0x00000000004a938a in BinaCPP::curl_cb (content=0x7f7edc0f5e00, size=1, nmemb=16384, buffer=0x7f7ee97f1e40)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:1768

10 0x00007f7ef1170960 in Curl_client_write () from /lib64/libcurl.so

11 0x00007f7ef118fb65 in inflate_stream () from /lib64/libcurl.so

12 0x00007f7ef118ff6d in Curl_unencode_gzip_write () from /lib64/libcurl.so

13 0x00007f7ef118a584 in Curl_httpchunk_read () from /lib64/libcurl.so

14 0x00007f7ef1184b57 in Curl_readwrite () from /lib64/libcurl.so

15 0x00007f7ef118ea5f in multi_runsingle () from /lib64/libcurl.so

16 0x00007f7ef118f3b1 in curl_multi_perform () from /lib64/libcurl.so

17 0x00007f7ef1186623 in curl_easy_perform () from /lib64/libcurl.so

18 0x00000000004a972f in BinaCPP::curl_api_with_header (url=..., str_result=..., extra_http_header=..., post_data=..., action=...)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:1825

19 0x00000000004a943a in BinaCPP::curl_api (url=..., result_json=...)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:1785

20 0x00000000004a27dd in BinaCPP::get_allPrices (json_result=...)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:95

21 0x00000000004a299f in BinaCPP::get_price (symbol=0x7f7ee9ff2ff0 "BTCUSDT")

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:125

修正方法: CURLOPT_WRITEDATA这个选项在C++中使用时就要注意了,

include <curl/curl.h>

CURLcode curl_easy_setopt(CURL handle, CURLOPT_WRITEDATA, void pointer); 这里pointer必须是struct,不能是类class,否则会有莫名奇怪的错误 这样的后果是,最后下载的文件长度与原有长度不符,使用md5sum计算得到的校验值与标准值不符。这证明是错误的文件

请参考: https://blog.csdn.net/hqbvqv/article/details/70187608

techiethink commented 5 years ago

错误信息如下:

0 0x00007f7eef07a837 in raise () from /lib64/libc.so.6

1 0x00007f7eef07be7a in abort () from /lib64/libc.so.6

2 0x00007f7eef9b6192 in __gnu_cxx::__verbose_terminate_handler () at ../../../../libstdc++-v3/libsupc++/vterminate.cc:95

3 0x00007f7eef9b4076 in cxxabiv1::terminate(void (*)()) () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:47

4 0x00007f7eef9b40b1 in std::terminate () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:57

5 0x00007f7eef9b42f3 in cxxabiv1::cxa_throw (obj=, tinfo=0x7f7eefc99690 ,

dest=0x7f7eef9b2500 std::bad_alloc::~bad_alloc()) at ../../../../libstdc++-v3/libsupc++/eh_throw.cc:95

6 0x00007f7eef9b47bc in operator new (sz=280366331249121) at ../../../../libstdc++-v3/libsupc++/new_op.cc:54

7 0x00007f7eefa4233b in std::__cxx11::basic_string<char, std::char_traits, std::allocator >::_M_mutate (

this=this@entry=0x7f7ee97f1e40, pos=140183165624672, len1=len1@entry=0, s=0x7f7edc0f5e00 "[{"symbol":"ETHBTC","price":"0.02928100"},{"symbol":"LTCBTC","price":"0.01518500"},{"symbol":"BNBBTC","price":"0.00387090"},{"symbol":"NEOBTC","price":"0.00149900"},{"symbol":"QTUMETH","price":"0.0133"..., __len2=16384) at /data/software/gcc-8.3.0/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc:310

8 0x00007f7eefa438d3 in std::__cxx11::basic_string<char, std::char_traits, std::allocator >::_M_append (

this=0x7f7ee97f1e40, s=, n=) at /data/software/gcc-8.3.0/build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:287

9 0x00000000004a938a in BinaCPP::curl_cb (content=0x7f7edc0f5e00, size=1, nmemb=16384, buffer=0x7f7ee97f1e40)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:1768

10 0x00007f7ef1170960 in Curl_client_write () from /lib64/libcurl.so

11 0x00007f7ef118fb65 in inflate_stream () from /lib64/libcurl.so

12 0x00007f7ef118ff6d in Curl_unencode_gzip_write () from /lib64/libcurl.so

13 0x00007f7ef118a584 in Curl_httpchunk_read () from /lib64/libcurl.so

14 0x00007f7ef1184b57 in Curl_readwrite () from /lib64/libcurl.so

15 0x00007f7ef118ea5f in multi_runsingle () from /lib64/libcurl.so

16 0x00007f7ef118f3b1 in curl_multi_perform () from /lib64/libcurl.so

17 0x00007f7ef1186623 in curl_easy_perform () from /lib64/libcurl.so

18 0x00000000004a972f in BinaCPP::curl_api_with_header (url=..., str_result=..., extra_http_header=..., post_data=..., action=...)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:1825

19 0x00000000004a943a in BinaCPP::curl_api (url=..., result_json=...)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:1785

20 0x00000000004a27dd in BinaCPP::get_allPrices (json_result=...)

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:95

21 0x00000000004a299f in BinaCPP::get_price (symbol=0x7f7ee9ff2ff0 "BTCUSDT")

at /data/home/aron/topex.code.chuanxi/ygw-server/app/exchange-btcusdt/binance/binacpp.cpp:125

修正方法: CURLOPT_WRITEDATA这个选项在C++中使用时就要注意了,

include <curl/curl.h>

CURLcode curl_easy_setopt(CURL handle, CURLOPT_WRITEDATA, void pointer); 这里pointer必须是struct,不能是类class,否则会有莫名奇怪的错误 这样的后果是,最后下载的文件长度与原有长度不符,使用md5sum计算得到的校验值与标准值不符。这证明是错误的文件

请参考: https://blog.csdn.net/hqbvqv/article/details/70187608

参考代码: size_t BinaCPP::curl_cb( void content, size_t size, size_t nmemb, void response ) {
BinaCPP_logger::write_log( " " ) ;

//buffer->append((char*)content, size*nmemb);

size_t realsize          = size * nmemb;
struct MemoryStruct* mem = (struct MemoryStruct *)response;

char *ptr = (char*) realloc((void*)mem->memory, mem->size + realsize + 1);
if(ptr == NULL) {
    /* out of memory! */ 
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
}

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

BinaCPP_logger::write_log( "<BinaCPP::curl_cb> done" ) ;

return realsize;

}