Pages retrieved by cURL bigger than 5000 characters seem to be causing a segmentation fault in my application.
//Includes etc.
#include "mainheader.h"
using namespace std;
using namespace boost;
//Buffer is defined right after the includes
char buffer [5000];
//cURL result to buffer, which is declared above.
void function_pt(void *ptr, size_t size, size_t nmemb, void *stream)
{
int n;
n=sprintf(buffer,"%s", ptr, size, nmemb, stream);
}
//Function to check one URL
void checkurl(char* thisurl)
{
//Start curl
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, thisurl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
I’m not 100% sure if this is what’s causing the segmentation fault, there’s a possibility any return containing multiple newlines causes the segmentation fault (haven’t been able to test yet). But I was assuming the buffer would automatically cut off the returned data after 5000 characters, but I guess that was naive of me. How can I make sure this is the case?
Your sprintf line is wrong. You pass too many arguments for your format string. Also, you can use snprintf, which is like sprintf but capable of taking max length to prevent overflowing your buffer.