LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-24-2005, 03:52 PM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
segfault prob in C


Hi all,

Having a bit of a problem with fprintf generating a segfault and hoping that someone can straighten me out. The entire code is available here

I have (among other things) the following code:
Code:
int bindips( char *addr, char *mask ) {
int octet1, octet2, octet3, octet4;
int net1, net2, net3, net4;
FILE *fd;
int i;
char *output;
char *threeOctets;
...<snip>...
else { /* Just an IP address */
		strcpy( output, addr );
		/* Trying to get rid of the newline between IP and netmask,
		   but it doesn't seem to be working */
		if( output[ strlen( output ) - 1 ] == '\n' )
			output[ strlen( output ) - 1 ] == ' ';
		strcat( output, ":" );
		strcat( output, mask );
		printf( "%s\n", output );
		fprintf( fd, "%s", output ); /* Generates segfault */
	}
}
I malloc'd memory to output, and it seems to be enough as copying and concatenating to it seems to work.

First, there is a newline at the end of addr that I try to remove before I strcat mask to it. That is not working...ideas?

Secondly, the last line above, fprintf, generates a segfault.

Any help/ideas would be apprciated!
 
Old 01-24-2005, 04:11 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
You didn't open the file....

Here is what I would suggest:

Code:
 /* open /etc/ips for append MOVED OUTSIDE OF IF*/
        if(( fd = fopen( "ips", "a" )) == NULL ) {
            /*code*/
                }

        if( octet4 == 0 ) {
               /* code */
        } else { /* Just an IP address */
                strcpy( output, addr );
                /* Trying to get rid of the newline between IP and netmask,
                   but it doesn't seem to be working */
                while ((tempch = strchr(output,'\n')) != NULL) {
                        *tempch = 0;
                }
                strcat( output, ":" );
                strcat( output, mask );
                while ((tempch = strchr(output,'\n')) != NULL) {
                        *tempch = 0;
                }
                printf( "output:%s\n", output );
                fprintf( fd, "%s", output ); /* Generates segfault */
        }
 
Old 01-24-2005, 04:14 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Actually, I'm sorry, I wasn't paying enough attention... this looks a little better for that if:

Code:
       else { /* Just an IP address */
                strcpy( output, addr );
                /* Trying to get rid of the newline between IP and netmask,
                   but it doesn't seem to be working */
                if ((tempch = strchr(output,'\n')) != NULL) {
                        *tempch = ' ';
                }
                strcat( output, ":" );
                strcat( output, mask );
                if  ((tempch = strchr(output,'\n')) != NULL) {
                        *tempch = ' ';
                }
                printf( "output:%s\n", output );
                fprintf( fd, "%s", output ); /* Generates segfault */
        }
But anyway, your biggest problem was fd wasn't set to anything valid in the else part of the if statement so it crashed when trying to open the file.
 
Old 01-24-2005, 05:21 PM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi Shaw,

Perfect. strchr was a perfect solution, and I am a perfect idiot for using fopen inside an if that was clearly not being executed! At any rate, thanks tons for the speedy and helpful reply.

Watch this space for more of my stupid blunders in the days to come!
 
Old 01-24-2005, 05:58 PM   #5
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hello again,

Updated complete code available here

Getting a segfault at a different spot now:

Code:
	if( octet4 == 0 ) { /* We have been passed a network ID rather than an IP address   */
		/* open /etc/ips for append */
		threeOctets = ( char * ) malloc( 13 );
		if( threeOctets == NULL ) {
			fprintf( stderr, "%s: Out of memory in bindips() on ln 149.", myName );
			return( -1 );
		}
		strcpy( threeOctets, ( char * ) octet1 ); /* Segmentation fault */
                                strcat( threeOctets, "." );
                                strcat( threeOctets, ( char * ) octet2 );
                                strcat( threeOctets, "." );
		strcat( threeOctets, ( char * ) octet3 );
		strcat( threeOctets, "." );

		for( i = 2; i < 255; ++i ) {
			strcpy( output, threeOctets );
			strcat( output, "." );
			strcat( output, ( char * ) i );
			strcat( output, ":" );
			strcat( output, mask );
			printf( "%s", output );
			/*fprintf( fd, "%s", output );*/
		}
               }
I have sscanf'd a char * into 4 ints, each representing one octet of an IP address. If octet4 equals zero, I know I have been passed a subnet to bind all the IP in it, rather than a single IP address. If so, we execute this segment of code. Now I have a char *threeOctets which I want to contain "octet1.octet2.octet3.".

Is the segmentation fault because I am casting the ints as char * but have not malloc'd memory for them? Should I try this:
Code:
char *strOctet1;
char *strOctet2;
char *strOctet3;
...
strOctet1 = (char*) malloc( 3 );
...
strcpy(threeOctets, strOctet1);
...
and so forth, or what would you recommend? Then, in the for loop, I want to create char * that represent all the IP addresses in the subnet from x.x.x.2 to x.x.x.254--am I going to run into the same problem with trying to cast the int i as a char * ?

TIA!
 
Old 01-25-2005, 08:00 AM   #6
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hmm, I tried the above and am still getting a segfault:
Code:
int octet1;
char *strOctet1;
strOctet1 = ( char * ) malloc( 4 );
strOctet1 = ( char * ) octet1;
strcpy( threeOctets,  strOctet1 ); /* Segmentation fault */
but,
Code:
strcopy( threeOctets, "123.123.123." );
works fine. That tells me that it is strOctet1 causing the problem; but I don't understand how it could be in memory that's inaccessible even for a read (that is all that strcpy does here to it, right?) when it has been successfully malloc'd memory.

Any ideas?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
red hat 9, printer prob and connection prob lasttriptotulsa Linux - Newbie 4 01-17-2007 04:39 AM
segfault in mallopt() spuzzzzzzz Programming 6 07-08-2004 07:03 PM
XFree keyboard problem, Quake prob and half-life prob cradlebin Linux - Software 0 09-21-2003 07:57 AM
Man 9.0 printer ? hardware prob ? install prob romcgill Linux - Hardware 2 02-24-2003 05:54 AM
Evolution SegFault granitepoint Linux - Software 0 01-12-2003 12:11 AM


All times are GMT -5. The time now is 07:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration