Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmentation fault (core dumped)

Reply
Thread Tools

segmentation fault (core dumped)

 
 
Keith Thompson
Guest
Posts: n/a
 
      12-21-2003
Pieter Droogendijk <> writes:
> on Sat, 20 Dec 2003 22:42:10 GMT, Keith Thompson <kst-> wrote:
> > Pieter Droogendijk <> 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


If you're going to assume that the argument is already a valid IP
address, an even simpler solution is:

int isIP(const char *ip) { return 1; }

If you're not willing to make that assumption, I'm afraid you're just
going to have to deal with the possibility that the string is longer
than 15 characters.

I've always found calling free() to be much less painful than nasal
demons.

--
Keith Thompson (The_Other_Keith) kst- <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)
 
Reply With Quote
 
 
 
 
no_name
Guest
Posts: n/a
 
      01-04-2004
On Sat, 20 Dec 2003 13:29:15 +0200, "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)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define uc unsigned char

/* if a ip address= (space-'\n')+number+(space-'\n').
(space-'\n')+number+(space-'\n').(space-'\n')+number+(space-'\n').
(space-'\n')+number+(space-'\n') */
int is_ip(char* a, int* v)
{char i, *b;
unsigned long ak;

for(i=0; i<4; ++i, ++a) /* 0.1.2. 3*/
{while(isspace( (uc) *a) && *a!='\n') ++a;
if(!isdigit(*a))
return 0;
ak=strtoul(a, &b, 10);
if(ak>255)
return 0;
if(i==3)
{a=b;
if(!isspace( (uc) *a ) && *a!='\0' )
return 0;
v[i]=ak;
return 1;
}
for( a=b; isspace( (uc) *a ) && *a!='\n'; ++a);
if(*a!='.')
return 0;
v[i]= ak;
}
}

int main(void)
{char a[] ="255 . 255 . 255. 255";
int v[4]={0,0,0,0}, r;
while(*a!='n')
{r=is_ip(a, v);
printf("%d: %d.%d.%d.%d\n", r, v[0], v[1], v[2], v[3] );
printf("continuare? number.number.number.number/n ");
fflush(stdout);
if(fgets(a, sizeof(a), stdin)==NULL) return 0;
}
return 0;
}

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      01-05-2004
On Sun, 04 Jan 2004 21:30:37 GMT, no_name <> wrote:

>On Sat, 20 Dec 2003 13:29:15 +0200, "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)

>#include <stdio.h>
>#include <stdlib.h>
>#include <ctype.h>
>#define uc unsigned char
>
>/* if a ip address= (space-'\n')+number+(space-'\n').
> (space-'\n')+number+(space-'\n').(space-'\n')+number+(space-'\n').
> (space-'\n')+number+(space-'\n') */
>int is_ip(char* a, int* v)
>{char i, *b;
> unsigned long ak;
>
> for(i=0; i<4; ++i, ++a) /* 0.1.2. 3*/
> {while(isspace( (uc) *a) && *a!='\n') ++a;
> if(!isdigit(*a))
> return 0;
> ak=strtoul(a, &b, 10);
> if(ak>255)
> return 0;
> if(i==3)
> {a=b;
> if(!isspace( (uc) *a ) && *a!='\0' )


If,during the fgets call, the user enters 1.1.1.1 and then presses
enter, a will contain '1', '.', '1', '.', '1', '.', '1', '\n', and
'\0'. This will cause the second expression to be true and lead you
to reject a valid address.

> return 0;
> v[i]=ak;
> return 1;


If the user enters 1.1.1.1 followed by a space and another 1, you will
accept this invalid IP address as valid.

> }
> for( a=b; isspace( (uc) *a ) && *a!='\n'; ++a);
> if(*a!='.')
> return 0;
> v[i]= ak;
> }
>}
>
>int main(void)
>{char a[] ="255 . 255 . 255. 255";
> int v[4]={0,0,0,0}, r;
> while(*a!='n')
> {r=is_ip(a, v);
> printf("%d: %d.%d.%d.%d\n", r, v[0], v[1], v[2], v[3] );
> printf("continuare? number.number.number.number/n ");
> fflush(stdout);
> if(fgets(a, sizeof(a), stdin)==NULL) return 0;
> }
> return 0;
>}




<<Remove the del for email>>
 
Reply With Quote
 
no_name
Guest
Posts: n/a
 
      01-05-2004
On 5 Jan 2004 00:07:56 GMT, Barry Schwarz <> wrote:


>>>FreeBSD prints:
>>>Segmentation fault (core dumped)

>>#include <stdio.h>
>>#include <stdlib.h>
>>#include <ctype.h>
>>#define uc unsigned char
>>
>>/* if a ip address= (space-'\n')+number+(space-'\n').
>> (space-'\n')+number+(space-'\n').(space-'\n')+number+(space-'\n').
>> (space-'\n')+number+(space-'\n') */

^^^^^^^^^^NO space+'\0'

>>int is_ip(char* a, int* v)
>>{char i, *b;
>> unsigned long ak;
>>
>> for(i=0; i<4; ++i, ++a) /* 0.1.2. 3*/
>> {while(isspace( (uc) *a) && *a!='\n') ++a;
>> if(!isdigit(*a))
>> return 0;
>> ak=strtoul(a, &b, 10);
>> if(ak>255)
>> return 0;
>> if(i==3)
>> {a=b;
>> if(!isspace( (uc) *a ) && *a!='\0' )

>
>If,during the fgets call, the user enters 1.1.1.1 and then presses
>enter, a will contain '1', '.', '1', '.', '1', '.', '1', '\n', and
>'\0'. This will cause the second expression to be true and lead you
>to reject a valid address.

NO
1: 255.255.255.255
|>[this ends with ,255,'\0', seems ok]
continuare? number.number.number.number/n 1.1.1.1
1: 1.1.1.1
|>[seems ok this ends with '\n\0' ?]
continuare? number.number.number.number/n 23.33 . 45 . 1 q
1: 23.33.45.1
continuare? number.number.number.number/n 23.33 . 45 . 1q
0: 23.33.45.1
continuare? number.number.number.number/n 23.33 . 45 .
19999999922222222
0: 23.33.45.1
continuare? number.number.number.number/n 0: 23.33.45.1
|> here the problem?
continuare? number.number.number.number/n
8888888888888888888888888888.99999
0: 23.33.45.1
continuare? number.number.number.number/n 0: 23.33.45.1
continuare? number.number.number.number/n

Are there problems with big numbers?
>> return 0;
>> v[i]=ak;


Thank you and bye
 
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
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57