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. |
what line is giving you the grief? what does gdb say about the segfault?
|
Not sure which line is bad. when i run it i get the error.
|
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... |
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
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.
|
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 |
Quote:
When you declare a pointer (and only the time you declare it) you may also initialize it. Code:
char dev[]="tap"; |
Re: Why do i get a Segmentation fault
Quote:
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... |
Wait! Maybe i got it.
Code:
#include <stdlib.h> |
Quote:
|
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 |
Quote:
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. |
Quote:
Code:
char s[10]; i think if u do : Code:
char s[10]; I don't remember how memset works but s is still just a pointer. |
well ... it's not memset and it's not strncpy or strcpy(unless ioctl() is altering ifr).
:scratch: |
All times are GMT -5. The time now is 12:07 AM. |