need help finding SIGSEGV bug

Discussion in 'C Programming' started by Ralph A. Moritz, Sep 21, 2005.

  1. Hi everyone,

    first of all, sorry for using Google to post. I know
    some people find this offensive, but as I am stuck
    behind a firewall, I don't have a choice.

    I am reading ``The Practice of Programming'' by
    Kernighan & Pike, and I have cannibalized their csv
    functions. I have added another function: csvmkline,
    which constructs a csv format string from a
    NULL-terminated string vector.

    The problem is that I am getting a segmentation fault.
    I've been debugging the function for about an hour
    now, but I just can't see the darn bug(s)! Please
    help!

    Regards,
    Ralph


    static char *line;
    static int maxline;

    char *csvmkline(const char *fields[])
    {
    char *newl;
    int i;
    size_t len = 0;

    maxline = 1;
    line = malloc(maxline);

    if (line == NULL) {
    reset();
    return NULL;
    }

    for (i = 0; fields != NULL; i++) {
    size_t rlen = strlen(fields) + 1; /* required length */
    int quote = needs_quotes(fields);
    if (quote) {
    rlen += 2;
    }

    if (maxline-len <= rlen) { /* grow buffer */
    newl = realloc(line, maxline += rlen);
    if (newl == NULL) {
    reset();
    return NULL;
    }

    line = newl;
    }

    if (len > 0) { /* do we need a comma? */
    line[len++] = ',';
    }

    if (quote) { /* do we need to quote? */
    line[len++] = '"';
    }

    newl = &line[len];
    memmove(newl, fields, strlen(fields));
    if (newl == NULL) {
    reset();
    return NULL;
    }

    len += strlen(fields);
    if (quote) { /* close quotes */
    line[len++] = '"';
    }
    }

    line[len] = '\0';
    return line;
    }
    Ralph A. Moritz, Sep 21, 2005
    #1
    1. Advertising

  2. Ralph A. Moritz wrote:
    > Hi everyone,
    >
    > I am reading ``The Practice of Programming'' by
    > Kernighan & Pike, and I have cannibalized their csv
    > functions. I have added another function: csvmkline,
    > which constructs a csv format string from a
    > NULL-terminated string vector.
    >
    > The problem is that I am getting a segmentation fault.
    > I've been debugging the function for about an hour
    > now, but I just can't see the darn bug(s)! Please
    > help!


    Can you post the code you are using to call this function?
    I gave it a try, seemed to work for me (I didn't have the
    reset or quotes functions, I made ones that did nothing).
    I was driving it like this:
    const char *fields[] = { "abc", "def", NULL };
    const char *line = csvmkline(fields);
    printf("%s\n", line);

    For corruption, it is often useful to use (system specific,
    off topic here) tools if you can't find the problem
    by code inspection. These tools include things like
    valgrind/purify/boundschecker/electric fence/MALLOC_CHECK_=2
    etc. I ran your code with the above to drive it using
    valgrind, it had no complaints.

    Some possible ways to go astray with the above code:
    1) Keeping a pointer to the line (could be realloced later)
    2) forgetting to NULL terminate the input array.

    When I first read the code, I thought you didn't have space
    for the ','. Turns out there is, as the +1 in the malloc is
    either for a ',' or a '\0', but that is slightly subtle.

    -David
    David Resnick, Sep 21, 2005
    #2
    1. Advertising

  3. Ralph A. Moritz

    Default User Guest

    Ralph A. Moritz wrote:

    > Hi everyone,
    >
    > first of all, sorry for using Google to post. I know
    > some people find this offensive, but as I am stuck
    > behind a firewall, I don't have a choice.



    There's nothing inherently wrong with Google, just that it leads to bad
    netiquette due to its broken reply mechanism. Pay attention to the info
    in my sig and you will be fine.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Sep 21, 2005
    #3
  4. David Resnick wrote:
    > Ralph A. Moritz wrote:
    > > Hi everyone,
    > >
    > > I am reading ``The Practice of Programming'' by
    > > Kernighan & Pike, and I have cannibalized their csv
    > > functions. I have added another function: csvmkline,
    > > which constructs a csv format string from a
    > > NULL-terminated string vector.
    > >
    > > The problem is that I am getting a segmentation fault.
    > > I've been debugging the function for about an hour
    > > now, but I just can't see the darn bug(s)! Please
    > > help!

    >
    > Can you post the code you are using to call this function?
    > I gave it a try, seemed to work for me (I didn't have the
    > reset or quotes functions, I made ones that did nothing).
    > I was driving it like this:
    > const char *fields[] = { "abc", "def", NULL };
    > const char *line = csvmkline(fields);
    > printf("%s\n", line);


    Hi David,

    thanks for your analysis. You were right, the function
    works fine, the bug was in the calling code (strange how
    that didn't occur to me yesterday).

    Anyway, using a bunch of static global variables to
    maintain state information seems like bad design to
    me, so I'm going to redesign around a `csv context':
    a struct that will contain the state information.

    > Some possible ways to go astray with the above code:
    > 1) Keeping a pointer to the line (could be realloced later)
    > 2) forgetting to NULL terminate the input array.


    Agreed. Shoddy design. Actually 1) was the source of
    the bug in the calling code...

    > -David


    Thanks again,
    Ralph
    Ralph A. Moritz, Sep 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank
    Replies:
    0
    Views:
    2,106
    Frank
    Aug 5, 2003
  2. manoj
    Replies:
    0
    Views:
    1,177
    manoj
    Jun 25, 2004
  3. Morris Dovey

    gdb SIGSEGV

    Morris Dovey, Feb 12, 2004, in forum: C Programming
    Replies:
    3
    Views:
    1,000
    Mark McIntyre
    Feb 14, 2004
  4. Nancy
    Replies:
    0
    Views:
    329
    Nancy
    Apr 5, 2006
  5. Replies:
    1
    Views:
    599
    Ioannis Gyftos
    Dec 20, 2007
Loading...

Share This Page