Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   segmentation fault (core dumped) (http://www.velocityreviews.com/forums/t316651-segmentation-fault-core-dumped.html)

Pieter Droogendijk 12-20-2003 11:26 AM

Re: segmentation fault (core dumped)
 
begin on Sat, 20 Dec 2003 13:29:15 +0200, "N.S. du Toit" <ferdyn@mweb.co.za>
wrote:
> Just having a bit of trouble programming with C under FreeBSD 5.1 using the
> gcc compiler. I'm a bit new to C so my apologies if the answer to my
> question appear obvious :)
>
> Basically I've written a function that will check whether a string is an ip
> address (see the function isIP below). During my attempt at debugging this
> problem I inserted a printf statement before the return command from the
> statement, and also a printf statement after the function's call.
> Strangely, the last printf in the function prints, but not the printf after
> the function has been called. So somewhere something causes a problem
> during the attempt at exiting the function.
>
> FreeBSD prints:
> Segmentation fault (core dumped)
> which from what I've read basically means that I'm mayhaps addressing memory
> outside the current segment. I've got no idea what's wrong, but also more
> importantly, I don't know how to go about debugging the problem? So any
> insights into debugging segmentation faults (or core dumps) would be
> appreciated. I tried using lint but that doesn't really give me any useful
> information...or not any information useful to me ;)
>
> Thanks for your time, much appreciated :)
>
> /*
> * checks whether a string is an IP address
> * (it basically splits the string, checks that it has 4 segments and that
> each segment is a number between 0 and 255)
> */



#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int isNumber(char* chars);


> int isIP(const char *ip) {
>
> char* segment;
> int segmentcount = 0;

char iptemp[3+1+3+1+3+1+3+1]; /* Ip address won't get longer than this */
>
> // we mustn't change the IP pointer (strtok changes content)
> memcpy(iptemp, ip, strlen(ip)+1 );
>
> segment = strtok(iptemp, ".");
> while (segment != NULL) {
> // increment our number of segments
> segmentcount++;
>
> // check whether the retrieved segment is a digit
> if (!isNumber( segment )) {
> return 0;
> }
>
> // check whether the number is between 0 and 255
> if ( atoi(segment) > 255 ) {
> return 0;
> }
>
> // retrieve the next segment
> segment = strtok(NULL, ".");
> }
>
> // an IP address consists of 4 segments
> if (segmentcount != 4) {
> return 0;
> }
>
> return 1;
> }
>
>
>
> The function isNumber is just:
>
> int isNumber(char* chars) {
>
> char* ptr;
> int asciicode;
>

ptr = chars;
> while (*ptr != '\0') {

/* You knew about isascii(), but not about isdigit()? */
if (!isdigit (*ptr))
return 0;
> ptr++;
> }
>
> return 1;
> }
>
>



--
begin No outlook shall percieve:
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}

N.S. du Toit 12-20-2003 11:29 AM

segmentation fault (core dumped)
 
Just having a bit of trouble programming with C under FreeBSD 5.1 using the
gcc compiler. I'm a bit new to C so my apologies if the answer to my
question appear obvious :)

Basically I've written a function that will check whether a string is an ip
address (see the function isIP below). During my attempt at debugging this
problem I inserted a printf statement before the return command from the
statement, and also a printf statement after the function's call.
Strangely, the last printf in the function prints, but not the printf after
the function has been called. So somewhere something causes a problem
during the attempt at exiting the function.

FreeBSD prints:
Segmentation fault (core dumped)
which from what I've read basically means that I'm mayhaps addressing memory
outside the current segment. I've got no idea what's wrong, but also more
importantly, I don't know how to go about debugging the problem? So any
insights into debugging segmentation faults (or core dumps) would be
appreciated. I tried using lint but that doesn't really give me any useful
information...or not any information useful to me ;)

Thanks for your time, much appreciated :)

/*
* checks whether a string is an IP address
* (it basically splits the string, checks that it has 4 segments and that
each segment is a number between 0 and 255)
*/
int isIP(const char *ip) {

char* segment;
int segmentcount = 0;

//--------------------------------------------------------------------------
---
// we mustn't change the IP pointer (strtok changes content)
char *iptemp;
memcpy(iptemp, ip, strlen(ip)+1 );
//--------------------------------------------------------------------------
---

segment = strtok(iptemp, ".");
while (segment != NULL) {
// increment our number of segments
segmentcount++;

// check whether the retrieved segment is a digit
if (!isNumber( segment )) {
return 0;
}

// check whether the number is between 0 and 255
if ( atoi(segment) > 255 ) {
return 0;
}

// retrieve the next segment
segment = strtok(NULL, ".");
}

// an IP address consists of 4 segments
if (segmentcount != 4) {
return 0;
}

return 1;
}



The function isNumber is just:

int isNumber(char* chars) {

char* ptr;
int asciicode;

ptr = &chars[0];
while (*ptr != '\0') {
// check to see whether the character is ascii
if (!isascii(*ptr)) {
return 0;
}

asciicode = (int) *ptr;
// check that the ASCII code is within 48 -> 57
if ( (asciicode < 48) || (asciicode > 57) ) {
return 0;
}

ptr++;
}

return 1;
}



Richard Heathfield 12-20-2003 12:11 PM

Re: segmentation fault (core dumped)
 
N.S. du Toit wrote:

> Just having a bit of trouble programming with C under FreeBSD 5.1 using
> the
> gcc compiler. I'm a bit new to C so my apologies if the answer to my
> question appear obvious :)
>
> Basically I've written a function that will check whether a string is an
> ip
> address (see the function isIP below). During my attempt at debugging
> this problem I inserted a printf statement before the return command from
> the statement, and also a printf statement after the function's call.
> Strangely, the last printf in the function prints, but not the printf
> after
> the function has been called. So somewhere something causes a problem
> during the attempt at exiting the function.
>
> FreeBSD prints:
> Segmentation fault (core dumped)
> which from what I've read basically means that I'm mayhaps addressing
> memory
> outside the current segment. I've got no idea what's wrong, but also more
> importantly, I don't know how to go about debugging the problem? So any
> insights into debugging segmentation faults (or core dumps) would be
> appreciated. I tried using lint but that doesn't really give me any
> useful information...or not any information useful to me ;)
>
> Thanks for your time, much appreciated :)
>
> /*
> * checks whether a string is an IP address
> * (it basically splits the string, checks that it has 4 segments and that
> each segment is a number between 0 and 255)
> */
> int isIP(const char *ip) {
>
> char* segment;
> int segmentcount = 0;
>
>

//--------------------------------------------------------------------------
> ---
> // we mustn't change the IP pointer (strtok changes content)
> char *iptemp;
> memcpy(iptemp, ip, strlen(ip)+1 );


There's a problem right here. You haven't allocated any storage for iptemp.

char *iptemp = malloc(strlen(ip) + 1);
if(iptemp != NULL)
{
strcpy(iptemp, ip);

Now you can work on iptemp. When you've finished with it, free(iptemp) to
prevent a memory leak.

--
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Martin Ambuhl 12-20-2003 12:55 PM

Re: segmentation fault (core dumped)
 
N.S. du Toit wrote:
> Just having a bit of trouble programming with C under FreeBSD 5.1 using the
> gcc compiler. I'm a bit new to C so my apologies if the answer to my
> question appear obvious :)
>
> Basically I've written a function that will check whether a string is an ip
> address (see the function isIP below). During my attempt at debugging this
> problem I inserted a printf statement before the return command from the
> statement, and also a printf statement after the function's call.
> Strangely, the last printf in the function prints, but not the printf after
> the function has been called. So somewhere something causes a problem
> during the attempt at exiting the function.
>
> FreeBSD prints:
> Segmentation fault (core dumped)
> which from what I've read basically means that I'm mayhaps addressing memory
> outside the current segment.


No ****, Sherlock.
[snip]

> char *iptemp;
> memcpy(iptemp, ip, strlen(ip)+1 );


What space do you think the uninitialized pointer iptemp points to?



--
Martin Ambuhl


N.S. du Toit 12-20-2003 04:43 PM

Re: segmentation fault (core dumped)
 
> char *iptemp = malloc(strlen(ip) + 1);
> if(iptemp != NULL)
> {
> strcpy(iptemp, ip);
>
> Now you can work on iptemp. When you've finished with it, free(iptemp) to
> prevent a memory leak.


excellent. thank you for the help :)



N.S. du Toit 12-20-2003 05:39 PM

Re: segmentation fault (core dumped)
 

"N.S. du Toit" <ferdyn@mweb.co.za> wrote in message
news:3fe47955.0@news1.mweb.co.za...
> > char *iptemp = malloc(strlen(ip) + 1);
> > if(iptemp != NULL)
> > {
> > strcpy(iptemp, ip);
> >
> > Now you can work on iptemp. When you've finished with it, free(iptemp)

to
> > prevent a memory leak.

>
> excellent. thank you for the help :)
>


in the same veign, is it possible to find these types of errors using gdb,
and how would one go about doing that?

thank you



Richard Heathfield 12-20-2003 06:35 PM

Re: segmentation fault (core dumped)
 
N.S. du Toit wrote:

> in the same veign, is it possible to find these types of errors [he
> means seg faults] using gdb, and how would one go about doing that?


man gdb has to be worth a try, doesn't it?

gdb is, however, a specific software tool, and its behaviour is not defined
by the C language. You could profitably raise the question in
comp.os.linux.development.apps or comp.unix.programmer - but in your own
interest, I recommend that you investigate the man page, Google, and lots
of other resources first, so that you can at least show that you've /tried/
to find out on your own.

--
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Keith Thompson 12-20-2003 10:42 PM

Re: segmentation fault (core dumped)
 
Pieter Droogendijk <compile@sig.for.mail> writes:
> > int isIP(const char *ip) {
> >
> > char* segment;
> > int segmentcount = 0;

> char iptemp[3+1+3+1+3+1+3+1]; /* Ip address won't get longer
> than this */
> >
> > // we mustn't change the IP pointer (strtok changes content)
> > memcpy(iptemp, ip, strlen(ip)+1 );


I'm not sure this is a safe assumption, even for valid IP addresses (I
don't know whether extra leading 0s are allowed). In any case, given
the name of the function, it has to allow for strings that *aren't*
valid IP addresses.

Instead, I suggest (as I think someone else has done):

char *iptemp = malloc(strlen(ip) + 1);

with a check that malloc() succeeded and a free() before leaving the
function.

Also, it probably makes more sense to use strcpy() than memcopy().

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)

Alexander Bartolich 12-20-2003 11:11 PM

Re: segmentation fault (core dumped)
 
begin Keith Thompson:
> Instead, I suggest (as I think someone else has done):
>
> char *iptemp = malloc(strlen(ip) + 1);
>
> with a check that malloc() succeeded and a free() before leaving the
> function.
>
> Also, it probably makes more sense to use strcpy() than memcopy().


Why do people use a low-level language like C when they absolutely
don't care about efficiency? What is so seductive about calling strlen
over and over again?

size_t size = strlen(ip) + 1;
char *iptemp = malloc(size);
memcpy(iptemp, ip, size);

Anyway, given the specific problem we can assume an upper limit for
the required buffer size. The input might exceed that limit, but that
is an indication of error.
A buffer on the stack combined with strncpy or snprintf will do.

--
Für Google, Tux und GPL!

Pieter Droogendijk 12-21-2003 05:55 PM

Re: segmentation fault (core dumped)
 
begin on Sat, 20 Dec 2003 22:42:10 GMT, Keith Thompson <kst-u@mib.org> wrote:
> Pieter Droogendijk <compile@sig.for.mail> writes:
> > > int isIP(const char *ip) {
> > >
> > > char* segment;
> > > int segmentcount = 0;

> > char iptemp[3+1+3+1+3+1+3+1]; /* Ip address won't get longer
> > than this */
> > >
> > > // we mustn't change the IP pointer (strtok changes content)
> > > memcpy(iptemp, ip, strlen(ip)+1 );

>
> I'm not sure this is a safe assumption, even for valid IP addresses (I
> don't know whether extra leading 0s are allowed). In any case, given
> the name of the function, it has to allow for strings that *aren't*
> valid IP addresses.


Yes, and valid ip addresses will fit into a buffer of 15, plus a terminator.
If leading zeroes are allowed in an ip address, then a malloc() will be useful,
however that'll induce painful (or drastically different) code later on in the
source, since said buffer should be freed before returning. I merely solved the
problem with a minimum of code change :P

>
> Instead, I suggest (as I think someone else has done):
>
> char *iptemp = malloc(strlen(ip) + 1);
>
> with a check that malloc() succeeded and a free() before leaving the
> function.
>
> Also, it probably makes more sense to use strcpy() than memcopy().
>
> --
> Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
> Schroedinger does Shakespeare: "To be *and* not to be"
> (Note new e-mail address)



--
begin signature:
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}


All times are GMT. The time now is 05:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.