简单的 libcurl 应用程序 - 段错误
simple libcurl app - segfault
我无法弄清楚我的代码在哪里出现段错误?
基本上,它通过SSL(HTTPS)安全连接连接到服务器并进行GET,提供MachineID(程序将其作为参数)。
还应设置一些自定义标头。然后,我将返回的正文和标头保存为单独的文本文件。(基本上它将运行一个shell脚本,因此我的远程系统可以自动从我的服务器获取"订单")
但它存在段错误,我唯一必须开发的 Linux 机器是我的 VPS,由于它们奇怪的虚拟化,加载 GDB 总是崩溃...... :o
谁能告诉我问题出在哪里? - 我认为它几乎可以肯定在我的字符串连接中 - 我在那里建立标头并请求 URL。
编辑:呃,忘记密码了!
#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;
}
您使用字符串文字作为strcat
的目标。您需要为指针分配内存,然后在strcpy
和strcat
函数中将它们用作目标
字符串文本通常驻留在 RO 区域中,写入此类区域可能会调用未定义的行为
更改类似内容
char *url
自
char url[100];
还可以考虑使用n
版本的strcat
、strcpy
和适当的缓冲区大小,以防止意外的缓冲区溢出
如果不自己分配内存,就无法执行strcat
。
不管给出以供参考的代码中可能存在的错误,在我过去的观点中,我发现libssl的使用默认情况下不是线程安全的。如果您使用的是libssl,请告诉我们,如果是这种情况,我将能够提供更多信息
请通过陷阱 #2 的http://horstr.blogspot.in/2008/04/on-libcurl-openssl-and-thread-safety.html为了更多的启蒙
相关文章:
- 为什么PyImport_ImportModule python 3.7.2 中出现段错误?
- 为什么在访问 vtkRenderWindow 的"交互器"变量时会发生段错误?
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么重载运算符<<打印特征类成员会导致段错误?
- 更改条件段错误
- 使用 TTF_RenderText() 加载字体时获取段错误 TTF_OpenFontRW()
- 注册对对象工厂的调用会导致段错误
- pthread_create在构造函数段错误中
- Nanoflann发现邻居提出段错误
- C++ 中的构造函数、继承、堆栈、堆、this-pointer 和段错误
- 具有unique_ptr的 CRTP 会导致段错误
- 增强纤维work_stealing屏障会导致段错误
- 当我返回指向结构的指针向量时出现段错误
- C++为什么我的代码没有爆炸/段错误?
- Pthread段错误,使用指向main中变量的指针
- C++ 模板中的段错误
- 相当于Windows/MSVC上的段错误?
- 为什么我的 LLVM JIT 实现出现段错误?
- 为什么访问我的引用捕获变量会导致我的 lambda 函数出现段错误?
- 为什么自删除的全局 Vulkan 实例仅在添加层时才导致段错误?