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

  • Home
  • SEARCH
  • 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 7906561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:02:10+00:00 2026-06-03T11:02:10+00:00

In process_query_str on line 4 I call splitline and get back a string array

  • 0

In process_query_str on line 4 I call splitline and get back a string array of 3 elements, the last of which is a NULL. The array is malloced and so are the individual strings. Then I try to free each of the elements and then the array itself (towards the end of the first function). The first two strings are freed but when it goes to free the third element, which is NULL, I get the error at the bottom regarding invalid pointer. Apparently it’s not getting set to NULL and should be. I tried placing breakpoints but the program won’t stop.

char * process_query_str(char *rq, char *cmd) {
    logg("In process_query_str.\n");
    int has_query = 0;
    char **tokenized_args = splitline(rq, &has_query); //Tokenize the string based on '?'
    char * query_str = rq;
    logg("has_query:");
    fprintf(stderr, "%d", has_query);
    if (has_query == 1) //Check for Query String
        if (query_str != NULL) {
            query_str = retrieve_query_str(tokenized_args);
            logg("Query String: ");
            logg(query_str);
            logg("\n");

            char* key = "REQUEST_METHOD"; //Process REQUEST_METHOD envir variable
            if (VLstore(key, cmd) == 0)
                if (VLexport(key) == 0) {
                    logg("Successfully exported ");
                    logg(cmd);
                    logg(": ");
                    logg(key);
                    logg("\n");

                    key = "QUERY_STRING"; //Process QUERY_STRING envir variable
                    if (VLstore(key, query_str) != 1) //1 signals a problem
                        if (VLexport(key) != 0) //0 signals a problem
                                {
                            logg("Successfully exported ");
                            logg(cmd);
                            logg(": ");
                            logg(key);
                            logg("\n");
                        }
                }
        }
#ifdef LOGGING //Print out environment variables
    VLlist();
#endif
    char *resource_str = newstr(tokenized_args[0], strlen(tokenized_args[0]));
    freelist(tokenized_args);
    logg("resource_str=");
    logg(resource_str);
    logg("\n");
    return resource_str;
}

char ** splitline(char *line, int*has_query)
/*
 * purpose: split a line into array of white-space separated tokens
 * returns: a NULL-terminated array of pointers to copies of the tokens
 *          or NULL if line if no tokens on the line
 *  action: traverse the array, locate strings, make copies
 *    note: strtok() could work, but we may want to add quotes later
 */
{
    //char *newstr();
    logg("In splitline\n");
    char **args;
    int spots = 0; /* spots in table    */
    int bufspace = 0; /* bytes in table */
    int argnum = 0; /* slots used       */
    char *cp = line; /* pos in string   */
    char *start;
    int len;

    if (line == NULL) /* handle special case    */
        return NULL;

    args = emalloc(BUFSIZ); /* initialize array */
    bufspace = BUFSIZ;
    spots = BUFSIZ / sizeof(char *);

    while (*cp != '\0') {
        logg("*cp=");
        fprintf(stderr, "%c", *cp);
        while (*cp == ' ') /* skip leading spaces   */
            cp++;
        if (*cp == '\0') /* quit at end-o-string    */
            break;
        /* make sure the array has room (+1 for NULL) */
        if (argnum + 1 >= spots) {
            args = erealloc(args, bufspace + BUFSIZ);
            bufspace += BUFSIZ;
            spots += (BUFSIZ / sizeof(char *));
        }

        /* mark start, then find end of word */
        start = cp;
        len = 1;
        if (*cp == '?') {
            logg("query reached.\n");
            *has_query = 1;
        }

        while (*++cp != '\0' && !(is_delim(*cp,*has_query)))
            len++;

        args[argnum++] = newstr(start, len);
    }
    logg("arg[0] =");
    logg(args[0]);
    logg("\n");
    if (argnum == 2) {
        logg("arg[1] =");
        logg(args[1]);
        logg("\n");
    }
    args[argnum] = NULL;
    fprintf(stderr, "last element is NULL.  argnum=%d", argnum);

    return args;
}

void freelist(char **list)
/*
 * purpose: free the list returned by splitline
 * returns: nothing
 *  action: free all strings in list and then free the list
 */
{
    char **cp = list;
    while (*cp && (*cp)) {
        logg("free: ");logg(*cp);logg("\n");
        free(*cp++);
    }
    logg("Now Free the list:");logg("\n");
    free(list);
    logg("Done Freeing List\n");
}

The stack backtrace information I get from the glibc is:

free: /index.cgifree: key=value*** glibc detected *** ./ws: free(): invalid pointer: 0x0804efa9 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb76d3d05]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb76d7770]
./ws[0x804b36f]
./ws[0x804a1fb]
./ws[0x8049de1]
./ws[0x8049757]
./ws[0x8049660]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb767e460]
./ws[0x8049031]
======= Memory map: ========
08048000-0804d000 r-xp 00000000 00:24 8084895    /nfs/home/j/c/jcalderon/unixlin                                                                                                                     ux/wsng/ws
0804d000-0804e000 rw-p 00004000 00:24 8084895    /nfs/home/j/c/jcalderon/unixlin                                                                                                                     ux/wsng/ws
0804e000-0806f000 rw-p 0804e000 00:00 0          [heap]
b7500000-b7521000 rw-p b7500000 00:00 0
b7521000-b7600000 ---p b7521000 00:00 0
b7644000-b764e000 r-xp 00000000 68:07 1122448    /lib/libgcc_s.so.1
b764e000-b764f000 rw-p 0000a000 68:07 1122448    /lib/libgcc_s.so.1
b765c000-b7665000 r-xp 00000000 68:07 1122335    /lib/tls/i686/cmov/libnss_files                                                                                                                     -2.7.so
b7665000-b7667000 rw-p 00008000 68:07 1122335    /lib/tls/i686/cmov/libnss_files                                                                                                                     -2.7.so
b7667000-b7668000 rw-p b7667000 00:00 0
b7668000-b77b2000 r-xp 00000000 68:07 1122611    /lib/tls/i686/cmov/libc-2.7.so
b77b2000-b77b3000 r--p 0014a000 68:07 1122611    /lib/tls/i686/cmov/libc-2.7.so
b77b3000-b77b5000 rw-p 0014b000 68:07 1122611    /lib/tls/i686/cmov/libc-2.7.so
b77b5000-b77b8000 rw-p b77b5000 00:00 0
b77c2000-b77c7000 rw-p b77c2000 00:00 0
b77c7000-b77c8000 r-xp b77c7000 00:00 0          [vdso]
b77c8000-b77e2000 r-xp 00000000 68:07 1124008    /lib/ld-2.7.so
b77e2000-b77e4000 rw-p 00019000 68:07 1124008    /lib/ld-2.7.so
bfd9f000-bfdb4000 rw-p bffea000 00:00 0          [stack]
  • 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-03T11:02:12+00:00Added an answer on June 3, 2026 at 11:02 am

    It turned out to be a malloc problem where I didn’t allocate enough space in newstr(). The last array element caused the error when I tried to free it.

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

Sidebar

Related Questions

What is the query used to view the topmost process which is having maximum
I'm trying to pull a value out of the url query string however I
I have a program which write logging as Text File. namespace logging { using
Consider the following string: $MRK - Merck - ($AAPL, $MSFT, $F) having day $AA!
I am trying to set security of PROCESS_TERMINATE. This is the code: CreateProcess(C:\\ADP\\SQLBase\\dbntsrv.exe, NULL,
I have a class in which I want to override the get_or_create method. Basically
I built a program to loop through words and get their synonyms from www.dicsin.com.br,
I'm trying to write a program which executes make.exe from MinGW distribution in the
In my query I want to get all users of the same city. This
There is something I don't quite understand it at all which is prepare and

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.