Segmentation fault in other systems C program


 
Thread Tools Search this Thread
Top Forums Programming Segmentation fault in other systems C program
# 1  
Old 09-01-2012
Segmentation fault in other systems C program

Hello everybody,
I've been working on a program on my Linux box, after finished the code, i compile it with gcc -Wall option, so i can see what's wrong or unused.

The Walll output shows nothing, so there are no loose ends on the program.
I run the program on my system, and it works PERFECTLY.

My problem is when i copy the source code of the program and compile it on other operating systems (Also Linux, but live versions). It compiles without any warnings from -Wall, but when i run the program, i get segmentation fault.

Since most live versions doesn't have gdb, i figured out which functions were causing the segmentation fault myself, and it is confusing, because simple lines like:
Code:
strcpy(sockaddr.sa_data, argv[1]);

Are causing the segmentation violation.

I mean, the same code doesn't cause any segmentation fault or any other problem when running, but when i compile the code on other linux system (without warnings) and run it, it fails.

Does anybody know why this happens? Would it be a missing library, package, or something which my host has and the other one does not?

Thanks in advance.
Zykl0n-B
# 2  
Old 09-02-2012
Segmentation faults come from the outside of the program most times, in the sense that somebody enters a nodename with 400 characters. Instead of your expected size.

In a live environment, you have to program defensively. All input is poison. Period.
Something that comes in as argv[1] probably means means it was entered on the command line. Check lengths of string arguments. Check to see that numbers entered as a string are in fact numbers. And are in the range expected. Test input for acceptance, never test it to exclude, because you can never outwit the idiots running your code.

For example, test argv[1] for length. Make sure argc shows the right number of arguments. Absolutely, positively check ALL return codes from any runtime library.
getaddrinfo() will reject garbage node names for you, for example. So a lot of the burden of testing to find good input can be passed off to well-tested code.

Just because a program compiles, does not make it correct in any way. You should actively try to break your code with simulated bad input during unit testing, for example.
Before you test for correct behavior with good input.

If you have identical runtime libraries on your dev and live systems you can copy the core file from over there and may be able to debug it. If it was compiled stripped this may be of no help.
# 3  
Old 09-02-2012
Hello, Jim. Thanks for your reply.

That's what i don't understand, because in this case there are no idiots giving wrong or different arguments than expected to the program. If i run, let's say: ./a.out eth0 in my host it runs perfectly, but in a live environment, and compiled on it, i run it with the same arguments, (./a.out eth0 in our example) and i expect to get the same result, but instead i get segmentation fault.

So, it's not a user mistake, i think it has to be with a libraries factor, a missing package, i don't know. I remember compiling the code on an earlier version of Linux and gcc returning warnings and not producing an executable file. I mean, if the code is working, correct and functionable on 2.6,2 it should be equally correct on 2.6.1 Smilie
Zykl0n-B
# 4  
Old 09-04-2012
Quote:
Originally Posted by Zykl0n-B
simple lines like:
Code:
strcpy(sockaddr.sa_data, argv[1]);

Are causing the segmentation violation.
sockaddr might be null or has been overwritten and is pointing to some odd location.
# 5  
Old 09-04-2012
Quote:
Originally Posted by jlliagre
sockaddr might be null or has been overwritten and is pointing to some odd location.
Won't matter in this case:
Code:
strcpy(sockaddr.sa_data, argv[1]);

The sockaddr isn't an address - it's a structure, and the results of the strcpy() depend on long argv[1] is. It could segv, or not.

Now, the content of sa_data is almost certainly not expected to be in string format, though....
# 6  
Old 09-04-2012
Won't matter indeed, I confused with sockaddr->sa_data
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

3. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

4. Programming

getting Segmentation Fault (core dumped) error but Program runs fine.

i am executing following program int main() { char str; FILE * fp; int i=0; ... (4 Replies)
Discussion started by: bhavesh.sapra
4 Replies

5. Programming

segmentation fault

Hi, I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Discussion started by: mind@work
3 Replies

6. UNIX for Dummies Questions & Answers

Segmentation Fault

Hi, While comparing primary key data of two tables thr bteq script I am getting this Error. This script is a shell script. *** Error: The following error was encountered on the output file. Script.sh: 3043492 Segmentation fault(coredump) Please let me know how to get through it. ... (5 Replies)
Discussion started by: monika
5 Replies

7. Programming

Program received signal SIGSEGV, Segmentation fault.

Dear all, I used debugger from C++ and these are the message I got: Program received signal SIGSEGV, Segmentation fault. 0x00323fc0 in free () from /lib/tls/libc.so.6 (gdb) info s #0 0x00323fc0 in free () from /lib/tls/libc.so.6 #1 0x00794fa1 in operator delete () from... (5 Replies)
Discussion started by: napapanbkk
5 Replies

8. Programming

segmentation fault

sometimes for this code i get a segmentation fault for codes llike this : int main{ int * a= 0; int b; a = (int*)malloc(sizeof(int)); ///some code using these variable but no freeing of a if(a){ free(a); a = 0; } return... (3 Replies)
Discussion started by: wojtyla
3 Replies

9. AIX

Segmentation fault

I am tring to install Lotus Domino/Notes 5.0.5 on a AIX 4.3.3 server. I go to run the cdrom/ibmpow/install and I get the following error. Lotus Notes for Unix Install Program --------------------------------------------- ./install: 10088 Segmentation fault This had Lotus Notes installed... (1 Reply)
Discussion started by: jshaulis
1 Replies

10. Programming

segmentation fault

hi all i'm trying to execute a c program under linux RH and it gives me segmentation fault, this program was running under unix at&t anybody kow what the problem could be? thanx in advance regards (2 Replies)
Discussion started by: omran
2 Replies
Login or Register to Ask a Question