cURL 检索到的大于 5000 个字符的页面似乎导致我的应用程序出现段错误。
//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);
}
}
我不是 100% 确定这是否是导致段错误的原因,任何包含多个换行符的返回都可能导致段错误(尚未能够测试)。但我假设缓冲区会在 5000 个字符后自动切断返回的数据,但我想那对我来说太天真了。我怎样才能确保是这种情况?
最佳答案
你的 sprintf 行是错误的。您为格式字符串传递了太多参数。此外,您还可以使用 snprintf,它类似于 sprintf,但能够获取最大长度以防止缓冲区溢出。
关于c++ - 在 X 个字符后限制 libcurl 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8191292/