libcurl中使用curl_easy_getinfo 產(chǎn)生段錯(cuò)誤分析
踩棧的典型案例:
最近再寫一個(gè)hsf的代理程序。需要使用libcurl與后端的nginx通信。程序編寫過程中遇到一個(gè)蹊蹺的問題。
調(diào)用 code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rsp_code); 后會(huì)報(bào)段錯(cuò)誤。
static int http_proxy(std::string domain, std::string path, std::string params, std::string &rsp_cont, std::string host = ""){ string url; int rsp_code; //此處設(shè)置為int類型 會(huì)有段錯(cuò)誤。如果long類型沒問題。 char str_rsp_code[5] = {'\0'}; CURLcode code; CURL* curl; curl_slist *headers = NULL; curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_USERAGENT, "hsfproxy"); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, on_write); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&rsp_cont); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5); code = curl_easy_perform(curl); code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rsp_code); curl_easy_cleanup(curl); sprintf(str_rsp_code,"%d",rsp_code); log("curl: http code["+ (std::string)str_rsp_code +"] url[" + (std::string)url + "] domain["+ domain +"]", __FILE__, __LINE__, __FUNCTION__, LOG_VERBOSE); return 1; }
#undef curl_easy_getinfo CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...) { va_list arg; void *paramp; CURLcode ret; struct SessionHandle *data = (struct SessionHandle *)curl; va_start(arg, info); paramp = va_arg(arg, void *); ret = Curl_getinfo(data, info, paramp); va_end(arg); return ret; }
驗(yàn)證:
編寫了示例代碼,驗(yàn)證了假設(shè)。注意此代碼在32位操作系統(tǒng)上不會(huì)報(bào)錯(cuò),在64位操作系統(tǒng)上會(huì)報(bào)段錯(cuò)誤。注意只有在int和long類型長度不一致時(shí)才會(huì)出現(xiàn)段錯(cuò)誤。如在64位操作系統(tǒng)。.
#include <iostream> #include <string> #include <cstdarg> using namespace std; void f(char chr, ...){ long value = 202; long *paramp; va_list arg_ptr; va_start(arg_ptr, chr); paramp = va_arg(arg_ptr, long *); va_end(arg_ptr); *paramp = value; } int main(){ string a; int p=0; string b; a = "a"; b = "b"; f('a',&p); cout << "p value " << p << endl; cout << "a value " << a << endl; cout << "b value " << b << endl;
輸出:
看來,寬松意為著需要更加嚴(yán)謹(jǐn)。沒有條條框框的約束,得做好自我約束。
*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。