Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:46:45+00:00 2026-06-02T20:46:45+00:00

I cant work out where my code is segfaulting? Basically it connects to a

  • 0

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;
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:46:48+00:00Added an answer on June 2, 2026 at 8:46 pm

    You are using string literals as destination for strcat. You need to allocate memory to the pointers and then use them as destination in strcpy and strcat functions

    A string literal generally resides in RO region and writing to such regions might invoke undefined behavior

    Change things like

    char *url
    

    to

    char url[100];
    

    Also consider using n versions of strcat, strcpy and appropriate buffer size to prevent inadvertent buffer overflows

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure out why the following code doesn't work. For the time being,
This is fairly simple but I cant work it out. I'm building a wordpress
This seems in my head like it should work but I cant figure out
I can't work out how to get the mysql client to return the number
I just cant get this working. I tried using the code below with onTouchEventand
Basically I want to switch between the drawable resources, and I cant figure out
For the life of me i cant work out why I am getting an
Im trying to get my head around javascript inheritance and this code doesnt work
I cant for the life of me figure out why this doesn't work: javascript:
I can't work out how to get the contents of the second span 'cantfindme'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.