samedi 25 avril 2015

Getting a bus error when calling curl_easy_perform() a second time


I'm pretty new to C and Libcurl so please excuse any shoddy coding practices.

So the issue i'm having, when calling the function curl_easy_perform() a second time a I encounter a bus error [EDIT: Specifically: The program received signal SIGBUS] At first I thought it was the way I was setting certain options with curl_easy_setop() However after many hours/days of debugging (hopefully something trivial (non the less way too long)) I found that the culprit was a self written function. the thing that is baffling me is that is does not interact with libcurl in any way, It's only purpose is to strip my api call down to the Barer token. Anyway hopefully my code will make more sense.

Entire code not included however this compiles with no warnings.

int main(int argc, char *argv[]) 
{
    CURL *curl = curl_easy_init();
    curl_socket_t sockfd;
    curl_off_t nread;
    size_t iolen;
    int res;
    long sockextr;
    const char *request;
    struct curl_slist *chunk = NULL;
    struct curl_slist *headers = NULL;
    printf("Setting params\n");

    if(curl) {
        struct string s;
        init_string(&s);
        /* Set URL */
        curl_easy_setopt(curl, CURLOPT_URL, "http://ift.tt/1Jp08l0");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
        /*Add json header*/
        chunk = curl_slist_append(chunk, "Content-type: application/json");
        res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
        /* Use SSL*/
        curl_easy_setopt(curl, CURLOPT_SSLVERSION, 1);
        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
        /* Get AuthToken */
        res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L)
        if (CURLE_OK != res)
        {
            printf("Error: %s\n", strerror(res));
            return 1;
        }

        printf("Getting Token...\n");

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
        curl_easy_perform(curl);
        printf("\nGot response:%s\n", s.ptr);
        printf("Setting Auth headers...\n");
        char *authtoken = s.ptr; // If I was to call   curl_easy_perform(curl) here there is no bus error
        char *tokenkey = strip(authtoken); 
        curl_easy_perform(curl); // When called here I get a Bus error
        return 0;
}

char *strip(char *json)
{
   // returns Auth token
   printf("strip has got json:%s\n", &json[0]);
   const char s[2] = "\"";
   char *token;
   int i = 0;

   token = strtok(json, s);
   while(token != NULL)
   {
     printf("%s\n", token);
     token = strtok(NULL, s);
     if (i == 2)
     {
       printf("Got Token!%s\n", token);
       return token;
       break;
     }
     i++;
   }
 }


Aucun commentaire:

Enregistrer un commentaire