B
B
Blunker2016-03-21 10:56:35
C++ / C#
Blunker, 2016-03-21 10:56:35

What can cause a Segmentation fault?

I took the code from the examples on the library's website. But for some reason, a seg fault crashes at startup. Compiles without error :(

void send_picture(char *filename)
{
    CURL *curl;
  CURLcode res;
  FILE *hd_src;
  struct stat file_info;
  curl_off_t fsize;
    struct curl_slist *headerlist = NULL;
    static const char buf_1[] = "RNFR " UPLOAD_FILE_AS;
    static const char buf_2[] = "RNTO " RENAME_FILE_TO;

  
  if (stat(filename, &file_info))
  {
    printf("Couldnt open '%s': %s\n", filename, strerror(errno));
    
    exit(1);
  }
  
  fsize = (curl_off_t)file_info.st_size;

  printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
  
  hd_src = fopen(filename, "rb");
  
  curl_global_init(CURL_GLOBAL_ALL);
  
  curl = curl_easy_init();
  
  if (curl)
  {
    curl_easy_setopt(curl, CURLOPT_USERNAME, "test");
        headerlist = curl_slist_append(headerlist, buf_1);
        headerlist = curl_slist_append(headerlist, buf_2);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
        curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
        curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize);
    
    res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);
  }
  
  fclose(hd_src);
  
  curl_global_cleanup();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Dubrovin, 2016-03-21
@z3apa3a

You do not check hd_src, in case of problems with opening the file, it will be accessed by a null pointer. Have you defined the read_callback function?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question