cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: curl_slist_append segfault

From: Tim Johnson <tbj002_at_gmail.com>
Date: Thu, 1 Sep 2011 11:26:59 -0500

On Thu, Sep 1, 2011 at 11:13 AM, Lars Nilsson <chamaeleon_at_gmail.com> wrote:

> On Thu, Sep 1, 2011 at 11:42 AM, Tim Johnson <tbj002_at_gmail.com> wrote:
> > I've got an application where I'm using cURL in a C program to post data
> > read out of a file to a server. The problem I'm having with it is, the
> > curl_slist_append call I'm using seems to be causing a segfault
> > intermittently, and seems to vary depending on the machine it is being
> used
> > on. To demonstrate, I've stripped the program down to the point that it
> > isn't actually posting anything anymore. It is just creating a form and
> an
> > slist, then cleaning it up, and reattempting the same thing. I've posted
> it
> > below.
> > I'm sure I'm doing something wrong here, I just don't know what it is.
> >
> > Thanks,
> > -Tim
> > -----------------------------------------------
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h> /* string manip */
> > #include <unistd.h> /* for usleep */
> > #include <curl/curl.h>
> > #include <curl/types.h>
> > #include <curl/easy.h>
> >
> > int main()
> > {
> > CURL *curl;
> >
> > struct curl_httppost *formpost=NULL;
> > struct curl_httppost *lastptr=NULL;
> > struct curl_slist *headerlist=NULL;
> > static const char buf[] = "Expect:";
> > char CharArray[] = "This is the text to post....or not.\0";
> > int loop_count = 0;
> > curl_global_init(CURL_GLOBAL_ALL);
> > loop_count = 0;
> > while (1)
> > {
> > {
> > sleep(1);
> > if (loop_count<86400)
> > { loop_count++; }
> > else
> > { loop_count=0; }
> > }
> > printf("\n\nFill out a form to post: %s\n",CharArray);
> > /* Fill in the operation field */
> > curl_formadd(&formpost,
> > &lastptr,
> > CURLFORM_COPYNAME, "operation",
> > CURLFORM_COPYCONTENTS, "PostData",
> > CURLFORM_END);
> > /* Fill in the file data field */
> > curl_formadd(&formpost,
> > &lastptr,
> > CURLFORM_COPYNAME, "filedata",
> > CURLFORM_COPYCONTENTS, CharArray,
> > CURLFORM_END);
> >
> > /* Fill in the submit field too, even if this is rarely needed */
> > curl_formadd(&formpost,
> > &lastptr,
> > CURLFORM_COPYNAME, "submit",
> > CURLFORM_COPYCONTENTS, "send",
> > CURLFORM_END);
> > curl = curl_easy_init();
> > /* initalize custom header list */
> > printf("calling curl_slist_append......\n");
> > headerlist = curl_slist_append(headerlist, buf); /******segfault
> > here 2nd time through******/
> > printf("curl_slist_append complete.\n");
> > if(curl)
> > {
> > /* Don't actually do anything with the data for this example */
> > /* however act like we did and start cleanup */
> > curl_easy_cleanup(curl);
> > curl_formfree(formpost);
> >
> > printf("About to free slist....\n");
> > curl_slist_free_all (headerlist);
> > printf("slist freed\n\n\n");
> > }
> > }
> > }
>
> What do you imagine curl_slist_append() will do with a garbage pointer
> headerlist you end up with after curl_slist_free_all() in the second
> iteration? The same can be said for the formpost variable.
>
> Lars Nilsson
>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html

Oops, I thought the *free commands would return the pointers to a state
where they could be reused.

Thank you for the help.

-Tim

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-09-01