need help finding SIGSEGV bug

R

Ralph A. Moritz

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;
}
 
D

David Resnick

Ralph said:
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
 
D

Default User

Ralph said:
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
 
R

Ralph A. Moritz

David said:
Ralph said:
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...

Thanks again,
Ralph
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top