Reputation: 5674
Here's my upload function.
The goal is: to upload only a chunk of a file depending on the offset and chunk size
In this function, if p_offset
is not zero, I call fseek()
by myself and then I let libcurl read the contents of the file using fread()
.
The caller of the function is responsible for giving a correct and valid size of chunk, making sure that p_offset + p_sizeOfChunk <= ACTUAL_SIZE_OF_FILE
The answer from the server is supposed to be a string. I get it via my callback writeToString()
The code works fine on Windows and OS X. But curl_easy_perform()
crashes on sometimes on Ubuntu 14.
Is there anything in my code that I am missing that could cause this crash?
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;
}
}
I get the answer and record it on a std::string
with writeToString()
. It's for sure not the reason for the crash. I tested it just returning the size * count
and the crash still happens.
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;
}
Here is the stack in the moment of crash! It seems to be related to 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 ...
Upvotes: 3
Views: 2151
Reputation: 5674
The problem was in a detail that I've completely misunderstood.
curl_easy_setopt( curl, CURLOPT_NOSIGNAL, 1 );
The effect of CURLOPT_NOSIGNAL
prevents libcurl from handling any signals! My original interpretation was completely the inverse. The mistake happened because on OS X it's working [and the pipe handling also exists], so I assumed that the "signal management" was correct. I don't know why on OS X the same bug didn't show up. Anyways, I removed this line and it's working perfectly.
And, of course, another possible solution is to explicit set ir to zero:
curl_easy_setopt( curl, CURLOPT_NOSIGNAL, 0 );
Upvotes: 1
Reputation: 5102
First thing... run it with the debugger (or get a proper core dump) to find out where is really failing.
Not sure if this might be the problem... but looking at the documentation, the function you pass in CURLOPT_WRITEFUNCTION
must have the following signature.
size_t function( char *ptr, size_t size, size_t nmemb, void *userdata);
However in your case:
size_t writeToString(const void * ptr, size_t size, size_t count, FILE * stream);
Apparently the default implementation for CURLOPT_WRITEFUNCTION expects a FILE * there... but in void * form
Upvotes: 1