LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 12-09-2004, 12:56 PM   #1
laclac01
Member
 
Registered: Feb 2004
Posts: 36

Rep: Reputation: 15
Why do i get a Segmentation fault


Why do i get a segmentation fault with the following code?
[code]
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <errno.h>

int tun_alloc(char *dev)
{
struct ifreq ifr;
int fd, err;

if( (fd = open("/dev/net/tun", O_RDWR )) < 0 )
//return tun_alloc_old(dev);
printf("error");
memset(&ifr, 0, sizeof(ifr));

/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
*/
ifr.ifr_flags = IFF_TUN;
if( *dev )
strncpy(ifr.ifr_name, dev, IFNAMSIZ);

if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
int main()
{
int tunfd;
char *dev="tap";
printf("testing tun");
tunfd=tun_alloc(dev);
return 0;
}

[code]

On both gentoo and redhat and
On both 2.4 and 2.6 kernels.
 
Old 12-09-2004, 01:25 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,397

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
what line is giving you the grief? what does gdb say about the segfault?
 
Old 12-09-2004, 01:40 PM   #3
laclac01
Member
 
Registered: Feb 2004
Posts: 36

Original Poster
Rep: Reputation: 15
Not sure which line is bad. when i run it i get the error.
 
Old 12-09-2004, 01:43 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,397

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
ever thought about putting in some debug fprintf's then?? if you can't even get it down to a particular line...

i'm a little lost as to why you'd be trying to compile this piece of pretty cryptic code without being able to debug it a little yourself...
 
Old 12-09-2004, 01:45 PM   #5
mcleodnine
Senior Member
 
Registered: May 2001
Location: Left Coast - Canada
Distribution: s l a c k w a r e
Posts: 2,731

Rep: Reputation: 45
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 12-09-2004, 03:01 PM   #6
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
just looking at your code, i think its the way you define the char *. The pointer may be modified to point else where but the results of modifying or lookiing at its contents may be undefined. Define it as char dev[] and reply back if this was it.
 
Old 12-09-2004, 04:48 PM   #7
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
char *dev="tap";

you need to malloc() some space before you can just force a string into a char *, and you need to use strcpy() or something to that effect

Last edited by dave_starsky; 12-09-2004 at 04:53 PM.
 
Old 12-09-2004, 05:04 PM   #8
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Quote:
Originally posted by dave_starsky
char *dev="tap";

you need to malloc() some space before you can just force a string into a char *, and you need to use strcpy() or something to that effect
Thats not true.
When you declare a pointer (and only the time you declare it) you may also initialize it.

Code:
char dev[]="tap";
char dev[]={'t','a','p','\0'};
Both are correct

Last edited by perfect_circle; 12-09-2004 at 05:06 PM.
 
Old 12-09-2004, 05:15 PM   #9
Dextrose
LQ Newbie
 
Registered: Dec 2004
Location: Orange County, CA
Distribution: Fedora Core 3
Posts: 8

Rep: Reputation: 0
Re: Why do i get a Segmentation fault

Quote:
ifr.ifr_flags = IFF_TUN;
if( *dev )
strncpy(ifr.ifr_name, dev, IFNAMSIZ);

if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
I'm willing to be that one or both of those two lines that I bolded for you are causing the problem. You need to ENSURE that the length of dev "strlen(dev)" is less than or equal to the length of ifr.ifr_name and the other way around must hold true as well since you copy back from ifr.ifr_name into dev. I guess I should say it's one or the other lines, but not both since only one buffer can be shorter than the other.

So, you need to find out which one is shorter and fix it. Or code accordingly... if( strlen(dev) <= strlen(ifr.ifr_name) ) { memcpy(); } etc...
 
Old 12-09-2004, 05:17 PM   #10
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Wait! Maybe i got it.

Code:
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <errno.h>

int tun_alloc(char *dev)
{
struct ifreq ifr;
int fd, err;

if( (fd = open("/dev/net/tun", O_RDWR )) < 0 )
//return tun_alloc_old(dev);
printf("error");
memset(&ifr, 0, sizeof(ifr));  <--------ifr_name pointer 
will be set to NULL and wont point to
the space allocated for the string any 
more, the strncpy will cause a segmentation fault then,
 because it will try to write to unallocated space.

/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
*/
ifr.ifr_flags = IFF_TUN;
if( *dev )
strncpy(ifr.ifr_name, dev, IFNAMSIZ);

if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
int main()
{
int tunfd;
char *dev="tap";
printf("testing tun");
tunfd=tun_alloc(dev);
return 0;
}
I think this is it, but it's just a guess. use fprintf(stderr,........) to find were you get the error.
 
Old 12-09-2004, 05:27 PM   #11
Dextrose
LQ Newbie
 
Registered: Dec 2004
Location: Orange County, CA
Distribution: Fedora Core 3
Posts: 8

Rep: Reputation: 0
Quote:
Originally posted by perfect_circle
I think this is it, but it's just a guess. use fprintf(stderr,........) to find were you get the error.
Good call...I missed that one. In fact, I'll GUARENTEE that's it.
 
Old 12-09-2004, 05:27 PM   #12
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
memset() doesn't alter the address of the destination buffer. It would just fill the allocated memory with 0's, not change the pointer to NULL.

EDIT: I see what you're saying now, and that would make sense, but I checked the structure and ifr_name isn't a pointer, it's just an array so it can't be true:
Code:
struct ifreq
{
#define IFHWADDRLEN     6
#define IFNAMSIZ        16
        union
        {
                char    ifrn_name[IFNAMSIZ];
        } ifr_ifrn;

Last edited by itsme86; 12-09-2004 at 05:32 PM.
 
Old 12-09-2004, 05:31 PM   #13
Dextrose
LQ Newbie
 
Registered: Dec 2004
Location: Orange County, CA
Distribution: Fedora Core 3
Posts: 8

Rep: Reputation: 0
Quote:
Originally posted by itsme86
memset() doesn't alter the address of the destination buffer. It would just fill the allocated memory with 0's, not change the pointer to NULL.
Actually, you're right too. Now that I think about it, he's zeroing out the memory for that struct. But, I can only assume that the string contained within that struct is initialize as such char pString[xx] for example.

Memsetting the struct to zero like you said will not cause the address of that string to go to zero...it will simply zero the memory of that string. Hmmm....

I'm going with what I originally said. I bet it's one of the strcpy calls.

Last edited by Dextrose; 12-09-2004 at 05:34 PM.
 
Old 12-09-2004, 05:51 PM   #14
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Quote:
ifr_name isn't a pointer, it's just an array so it can't be true
Any array
Code:
char s[10];
is a pointer pointing to a statically allocated memory.
i think if u do :
Code:
char s[10];
s = NULL;
will make you loose the allocated memory.
I don't remember how memset works
but s is still just a pointer.
 
Old 12-09-2004, 06:18 PM   #15
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
well ... it's not memset and it's not strncpy or strcpy(unless ioctl() is altering ifr).


 
  


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
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM


All times are GMT -5. The time now is 06:45 AM.

Main Menu
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