I cant work out where my code is segfaulting?
Basically it connects to a server over an SSL (HTTPS) secured connection and makes a GET, supplying a MachineID (which the program takes as a parameter).
Some custom headers should also be set. I then save the body returned, and the header, as separate text files. (Basically it will be getting a shell script to run, so my remote systems can automagically grab ‘orders’ from my server)
But it segfaults and the only linux machine I have to develop this on is my VPS which, due to thier strange virtualisation, always crashes loading GDB… 😮
Can anyone tell me where the problem is? – I think its almost certainly in my string concatenation – where i build up the header, and request URL.
EDIT: Duh, forgot the 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;
}
You are using string literals as destination for
strcat. You need to allocate memory to the pointers and then use them as destination instrcpyandstrcatfunctionsA string literal generally resides in RO region and writing to such regions might invoke undefined behavior
Change things like
to
Also consider using
nversions ofstrcat,strcpyand appropriate buffer size to prevent inadvertent buffer overflows