Like Tree1Likes
  • 1 Post By Salem

libcurl program crashes on curl_easy_init?

This is a discussion on libcurl program crashes on curl_easy_init? within the C Programming forums, part of the General Programming Boards category; I wrote a simple libcurl program with several calls to the following function: Code: char* import(char* in) { CURL *curl; ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    libcurl program crashes on curl_easy_init?

    I wrote a simple libcurl program with several calls to the following function:

    Code:
    char* import(char* in) {
    
    CURL *curl;
    puts("*curl");
    curl = curl_easy_init();
    puts("init");
    
    if(curl == NULL)
    {
    puts("EASY_INIT WENT WRONG.");
    exit(0);
    }
    
    char *data;
    
    /* Create the write buffer */
    data = malloc(1024 * 256);
    
    if (data == NULL)
    {
    puts("Not enough memory for import buffer.");
    exit(0);
    }
    
    struct write_result write_result = {
    .data = data,
    .pos = 0
    };
    
    /* Set curl's parameters */
    curl_easy_setopt(curl, CURLOPT_URL,in);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
    
    curl_easy_perform(curl);
    
    /* null terminate the string */
    data[write_result.pos] = '\0';
    
    curl_easy_cleanup(curl);
    curl_global_cleanup();
    
    return(data);
    
    }
    The weird thing is, I use the function often in the main program, and sometimes it works, sometimes it doesn't. When it doesn't work, it gets as far as printing "*curl", and then just crashes. Generally what happens is, it crashes several times in a row, on the exact same call (the previous work perfectly), and then inexplicably works fine again later on...

    Does anyone have any idea what may be causing this and how to stop it???
    Any help would be greatly appreciated

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And what is the state of your network while all this is going on?

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    My connection seems okay; as the previous calls to the function work, just one particular call fails for some reason

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
    What does this function do (curl_write) ?

    In particular, how does it know how much data is available in the struct write_result structure?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Sorry, i forgot about the other definitions:

    Code:
    struct write_result {
    char *data;
    int pos;
    };
    and

    Code:
    static size_t curl_write( void *ptr, size_t size, size_t nmemb, void *stream) {
    
    struct write_result *result = (struct write_result *)stream;
    
    /* Will we overflow on this write? */
    if(result->pos + size * nmemb >= (1024 * 256) - 1) {
    return 0;
    }
    
    /* Copy curl's stream buffer into our own buffer */
    memcpy(result->data + result->pos, ptr, size * nmemb);
    
    /* Advance the position */
    result->pos += size * nmemb;
    
    return (size * nmemb);
    }
    I don't think the problem is related to free()ing memory properly either; every time i call inport(), I make sure to free the malloc'd pointer "data" as soon as I've used the returned string... I wonder is there any other error condition that can cause easy_init to crash apart from returning a NULL value? Oddly the program is now into a phase where it's working again (this will no doubt be short-lived), though I haven't changed the code at all, and my connection seems to be just as good as it was before

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    Use a debugger to help pinpoint the problem. Gdb will tell you where the crash is happening, Valgrind might be able to tell you what you're doing that's causing the crash.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > /* Advance the position */
    > result->pos += size * nmemb;
    How does this affect the validity of this calculation the next time around?

    > if(result->pos + size * nmemb >= (1024 * 256) - 1)



    Add a .size member to your structure, and update it with the real amount of space still available, as you add data.
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-30-2011, 10:39 AM
  2. to get output of libcurl program
    By little in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2011, 05:52 AM
  3. libcurl program no output
    By shady_Dev in forum Linux Programming
    Replies: 5
    Last Post: 01-04-2008, 03:25 AM
  4. My program crashes :|
    By Testify in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2007, 11:48 AM
  5. My I/O Program crashes, I need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 01:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21