c++ - 简单的 libcurl 应用程序 - 段错误

标签 c++ c curl segmentation-fault libcurl

我不知道我的代码哪里有段错误?

基本上,它通过 SSL (HTTPS) 安全连接连接到服务器并进行 GET,提供 MachineID(程序将其作为参数)。

还应设置一些自定义 header 。然后我将返回的正文和标题保存为单独的文本文件。 (基本上它将运行一个 shell 脚本,因此我的远程系统可以自动从我的服务器获取“订单”)

但它会出现段错误,我必须在其上开发它的唯一 linux 机器是我的 VPS,由于其奇怪的虚拟化,加载 GDB 时总是崩溃...:o

谁能告诉我问题出在哪里? - 我认为它几乎肯定是在我的字符串连接中 - 我在那里构建 header 和请求 URL。

编辑:呃,忘记密码了!

<p></p>

<pre><code>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
#include <string.h>

 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
 int written = fwrite(ptr, size, nmemb, (FILE *)stream);
 return written;
}

int main(int argc, char *argv[])
{
static const char *headerfilename = "head.out";
static const char *bodyfilename = "body.out";

char *url = "https://fakeserver.fakesite.com:8443/SystemManager/getOrders.jsp?machineID=";
char *customHeader = "MachineID:";
char *machineID = NULL;

struct curl_slist *chunk = NULL;

CURL *curl;
CURLcode res;
FILE *headerfile;
FILE *bodyfile;

    if (argc == 2)
{
    machineID = argv[1];
    strcat(url,machineID);
}
else
{
 printf("Usage: %s <MachineID>\n", argv[0]);
 return 1;
}

 curl_global_init(CURL_GLOBAL_SSL);

 // init the curl session 
 curl = curl_easy_init();

 if(curl) {
    // set URL to get 

 curl_easy_setopt(curl, CURLOPT_URL, url);

    // no progress meter please 
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);

    // send all data to this function 
 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

 // some servers don't like requests that are made without a user-agent field, so we provide one 
 curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

 // Also add a custom MachineID header 
 strcat(customHeader, machineID);
 chunk = curl_slist_append(chunk, customHeader);
 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);

    // These tweaks must be enabled for my dodgy self-signed certificate.

    // DONT bother verifying our certificate is signed by a trusted CA.
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    // DONT check the hostname on the certificate matcheds the remote system.
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);


    // open the files 
 headerfile = fopen(headerfilename,"w");
 if (headerfile == NULL) {
 curl_easy_cleanup(curl);
 return -1;
 }
 else
 {
 // we want the headers to this file handle 
 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerfile);
 }

 bodyfile = fopen(bodyfilename,"w");
 if (bodyfile == NULL) {
 curl_easy_cleanup(curl);
 return -1;
 }
 else
 {
 // we want the body to this file handle 
 curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile);
 }
     // get it! 
 res = curl_easy_perform(curl);

    // close the files 
 fclose(headerfile);
 fclose(bodyfile);

 // always cleanup curl stuff 
 curl_easy_cleanup(curl);
 }
 curl_global_cleanup();
 return 0;
}
</code></pre>

<p></p>

最佳答案

您正在使用字符串文字作为 strcat 的目标。您需要为指针分配内存,然后将它们用作 strcpystrcat 函数中的目标

字符串文字通常驻留在 RO 区域中,写入此类区域可能会调用未定义的行为

像这样改变

char *url

char url[100];

同时考虑使用 n 版本的 strcatstrcpy 和适当的缓冲区大小以防止无意的缓冲区溢出

关于c++ - 简单的 libcurl 应用程序 - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10362487/

相关文章:

C++ - 当尝试在 TCP 客户端上连接到不存在的地址时,程序表现出奇怪的行为

c - 使用 %d 打印 unsigned long long

c - 获取文本主体C

windows - 在 Windows 7 中使用 Curl 获取 Https 网页

php - 通过 CURL 尝试 OAuth2 - 请求中未指定授权类型

c++ - 多个类头文件需要枚举类型

c++ - 行号 : Any way to use __LINE__ to return line number of input file?

php - 后 curl 问题

c++ - 尽管设置了路径,但 g++/Eclipse 找不到包含文件

objective-c - 宏声明