gpt4 book ai didi

c++ - curl_easy_perform() 在 Ubuntu Server 14 上崩溃

转载 作者:太空狗 更新时间:2023-10-29 21:43:13 26 4
gpt4 key购买 nike

这是我的上传功能。

目标是:根据偏移量和 block 大小仅上传文件 block

在这个函数中,如果p_offset不为零,我调用fseek()我自己然后让 libcurl 使用 fread() 读取文件的内容.

函数的调用者负责提供正确有效的 block 大小,确保 p_offset + p_sizeOfChunk <= ACTUAL_SIZE_OF_FILE

服务器的回答应该是一个字符串。我通过我的回调得到它 writeToString()

该代码在 Windows 和 OS X 上运行良好。但是 curl_easy_perform()有时在 Ubuntu 14 上崩溃。

我的代码中是否缺少任何可能导致此崩溃的内容?

void uploadFile( const string & p_filename, const string & p_url, size_t p_offset, size_t p_sizeOfChunk )
{
FILE * file( fopen( p_filename.c_str(), "rb" ) );

if ( !file )
{
throw Exception( __FILE__, __LINE__ ) << "Could not open file " << p_filename << " when posting to " << p_url;
}

if ( p_offset )
{
if ( fseek( file, (long)p_offset, SEEK_SET ) )
{
throw Exception( __FILE__, __LINE__ ) << "Could not seek in file " << p_filename << " when posting to " << p_url;
}
}

CURL * curl( curl_easy_init() );

if ( !curl )
{
throw Exception( __FILE__, __LINE__ ) << "Could not initialize cURL when posting " << p_filename << " to " << p_url;
}

// URL
curl_easy_setopt( curl, CURLOPT_URL, p_url.c_str() );

// PUT HTTP method
string answer;
curl_easy_setopt( curl, CURLOPT_UPLOAD, 1L );
curl_easy_setopt( curl, CURLOPT_READFUNCTION, fread );
curl_easy_setopt( curl, CURLOPT_READDATA, file );
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeToString );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &answer );
curl_easy_setopt( curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)p_sizeOfChunk );

char errorBuffer[ CURL_ERROR_SIZE + 1 ];
curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, errorBuffer );

// No signal handlers...
curl_easy_setopt( curl, CURLOPT_NOSIGNAL, 1 );
curl_easy_setopt( curl, CURLOPT_TIMEOUT_MS, 120000 );

// HEADER
char contentLength[ 512 ];
snprintf( contentLength, sizeof( contentLength ), "Content-Length: %zu", p_sizeOfChunk );

struct curl_slist * headers( nullptr );
headers = curl_slist_append( headers, contentLength );
curl_easy_setopt( curl, CURLOPT_HTTPHEADER, headers );

// SSL
curl_easy_setopt( curl, CURLOPT_CAINFO, "path/to/cacert.pem" );

CURLcode res( curl_easy_perform( curl ) );

fclose( file );

if ( res != CURLE_OK && res != CURLE_SEND_ERROR )
{
curl_easy_cleanup( curl );
throw Exception( __FILE__, __LINE__ ) << "cURL error when posting " << p_filename << " to " << p_url << ": " << errorBuffer;
}

long httpResponseCode( 0 );
curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &httpResponseCode );
curl_easy_cleanup( curl );

if ( ( httpResponseCode / 100 ) != 2 )
{
cout << answer << endl;
throw Exception( __FILE__, __LINE__ ) << "HTTP error " << httpResponseCode << " when posting " << p_filename;
}
}

我得到答案并将其记录在 std::string 上与 writeToString() .这肯定不是崩溃的原因。我测试它只是返回 size * count崩溃仍然发生。

static size_t writeToString( const void * ptr, size_t size, size_t count, FILE * stream )
{
string & retContent( *( reinterpret_cast< string * >( stream ) ) );

if ( !retContent.length() )
{
int skipBOM( ( reinterpret_cast< const unsigned char * >( ptr )[ 0 ] == 0xEF && reinterpret_cast< const unsigned char * >( ptr )[ 1 ] == 0xBB && reinterpret_cast< const unsigned char * >( ptr )[ 2 ] == 0xBF ) ? 3 : 0 );
retContent += string( static_cast< const char * >( ptr ) + skipBOM, static_cast< int >( size * count ) - skipBOM );
}
else
{
retContent += string( static_cast< const char * >( ptr ), size * count );
}

return size * count;
}

这是崩溃时刻的堆栈!好像跟OpenSSL有关。

#0  0x00007ffff65ad35d in write () at ../sysdeps/unix/syscall-template.S:81
#1 0x00007ffff73187a6 in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#2 0x00007ffff731684b in BIO_write () from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
#3 0x00007ffff6ffcb72 in ?? () from /lib/x86_64-linux-gnu/libssl.so.1.0.0
#4 0x00007ffff6ffd273 in ?? () from /lib/x86_64-linux-gnu/libssl.so.1.0.0
#5 0x00007ffff76873e1 in ossl_send (conn=0x7ffef8013b28, sockindex=0, mem=0x7ffef8005379, len=16384, curlcode=0x7fff127fa5c0) at vtls/openssl.c:2720
#6 0x00007ffff762fe0f in Curl_write (conn=0x7ffef8013b28, sockfd=64, mem=0x7ffef8005379, len=16384, written=0x7fff127fa608) at sendf.c:233
#7 0x00007ffff764fb01 in readwrite_upload (data=0x7ffef8000a78, conn=0x7ffef8013b28, k=0x7ffef8000af0, didwhat=0x7fff127fa664) at transfer.c:954
#8 0x00007ffff764fdd9 in Curl_readwrite (conn=0x7ffef8013b28, done=0x7fff127fa6dc) at transfer.c:1059
#9 0x00007ffff765ced7 in multi_runsingle (multi=0x7ffef800a668, now=..., data=0x7ffef8000a78) at multi.c:1484
#10 0x00007ffff765d60c in curl_multi_perform (multi_handle=0x7ffef800a668, running_handles=0x7fff127fa870) at multi.c:1759
#11 0x00007ffff7652103 in easy_transfer (multi=0x7ffef800a668) at easy.c:705
#12 0x00007ffff7652311 in easy_perform (data=0x7ffef8000a78, events=false) at easy.c:793
#13 0x00007ffff7652364 in curl_easy_perform (easy=0x7ffef8000a78) at easy.c:812
#14 ...

最佳答案

首先...使用调试器运行它(或获得适当的核心转储)以找出真正失败的地方。

不确定这是否可能是问题...但查看 documentation ,您在 CURLOPT_WRITEFUNCTION 中传递的函数必须具有以下签名。

size_t function( char *ptr, size_t size, size_t nmemb, void *userdata);

但是在你的情况下:

size_t writeToString(const void * ptr, size_t size, size_t count, FILE * stream);

显然,CURLOPT_WRITEFUNCTION 的默认实现需要一个 FILE * 在那里......但以 void * 形式

关于c++ - curl_easy_perform() 在 Ubuntu Server 14 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23482727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com