Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > need help finding SIGSEGV bug

Reply
Thread Tools

need help finding SIGSEGV bug

 
 
Ralph A. Moritz
Guest
Posts: n/a
 
      09-21-2005
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[i] != NULL; i++) {
size_t rlen = strlen(fields[i]) + 1; /* required length */
int quote = needs_quotes(fields[i]);
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[i], strlen(fields[i]));
if (newl == NULL) {
reset();
return NULL;
}

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

line[len] = '\0';
return line;
}

 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      09-21-2005
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

 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      09-21-2005
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.
 
Reply With Quote
 
Ralph A. Moritz
Guest
Posts: n/a
 
      09-22-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help! throw SIGSEGV signal while open shared object from Tomcatserver, But success in JUnit lei.bobby@gmail.com C++ 1 12-20-2007 12:00 PM
ceval.c no such file or dir and SIGSEGV Nancy Python 0 04-05-2006 03:25 PM
SIGSEGV 11* segmentation violation manoj Java 0 06-25-2004 04:47 PM
gdb SIGSEGV Morris Dovey C Programming 3 02-14-2004 11:04 PM
SIGSEGV 11 (*) segmentation violation on AIX 5.1 and Java Frank Java 0 08-05-2003 08:54 PM



Advertisments