Application behaving in 3 different ways on 3 different machines


 
Thread Tools Search this Thread
Top Forums Programming Application behaving in 3 different ways on 3 different machines
# 1  
Old 01-07-2013
[solved] Application behaving in 3 different ways on 3 different machines

Hello.
During the holidays I've been developing an application on my desktop computer at home.
I setup a repository on github, so when I got back to work I cloned the repo to my laptop.

It wouldn't work.
The app is comprised of a client and a server, strangely enough the server would segfault at a strcpy at the very beginning, while the client would bug me about not supplying a command line parameter (it's supposed to work anyway).

So I ssh-ed into an office machine we use to test things out, cloned the repo and the problems are inverted!
Now it's the client that would segfault while the server pretends a parameter!

My machines are
desktop - i7 2600k with Ubuntu 12.04 x64 eng
laptop - core2duo with Ubuntu 12.04 x64 eng
test pc - core2duo with Ubuntu 10.04 ita (dunno if x86 or x64)

Now I could bear that an app developed on a single pc would require some tinkering on other pcs, but the same app displaying exactly symmetrical behaviour on two different pcs I can't understand.

Anyway the specific code that seems to be the problems is the following
Code:
struct arguments
{
  int *Z_DEBUG, *M_DEBUG;
  char * interf;
  char * outfile;            /* Argument for -o */
};

int main(int argc, char** argv) {

    struct arguments arguments;
    outstream = stdout;
    arguments.M_DEBUG=&MAIN_DEBUG;
    arguments.Z_DEBUG=&ZMQ_DEBUG;
    strcpy( arguments.interf, "eth0" );
    arguments.outfile = NULL;
    s_catch_signals();

    argp_parse(&argp, argc, argv, 0, 0, &arguments);

either I get a segfault at the strcpy or somehow the argp_parse exits the program.

I'm not an expert enough to understand why declaring the following is correct
Code:
char *mystring="useless phrase";

while this is wrong
Code:
char *mystring;
strcpy(mystring, "useless phrase");

And even more so I can't understand why, if it's wrong, it would work on my desktop computer!

Any help is really appreciated.

Last edited by erupter; 01-07-2013 at 10:16 AM.. Reason: solved
# 2  
Old 01-07-2013
The fact that your code ever worked is an accident. You have what is called undefined behavior

Code:
  char * interf;
  char * outfile;

They are pointers to a character string. They have NO memory allocated to them.

Change them like this (pick a number I chose 256...) :
Code:
  char interf[256];
  char outfile[256];

Or call
Code:
malloc()

to allocate memory for each one in main().

Code:
  interf=malloc(256);
  outfile=malloc(256);
  if(interf==NULL || or outfile==NULL)
  {
      perror("Cannot allocate memory");
      exit(1);
  }

You code did not work, it just happened not to crash. Because those pointers are never initialized they could be pointing to any bit of memory left over from a previous program. If you rebooted one of the PC's you may get different behavior. It is purely random, so it may not be something you can duplicate.

Last edited by jim mcnamara; 01-07-2013 at 08:16 AM..
# 3  
Old 01-07-2013
Thank you for your time.

I begun suspecting as much, but it is still unclear to me how it has been working for more than 10 days without so much as one crash or problem...
# 4  
Old 01-07-2013
Quote:
Originally Posted by erupter
I begun suspecting as much, but it is still unclear to me how it has been working for more than 10 days without so much as one crash or problem...
You are using undefined behavior.

Undefined does not mean 'crash instantly', it means 'what this does depends on the machine'.

So, it's to be expected that it will be unpredictable.
# 5  
Old 01-07-2013
This code ought to work without changing your structures, by the way:

Code:
arguments.interf="eth0";
arguments.outfile=NULL;

...because it's not trying to copy into nonexistent memory. "eth0" itself is already a pointer to valid memory, strcpy not needed.
# 6  
Old 01-07-2013
There is a set of standards for C. They dictate what will or will not happen in the language.

Doing what you did created something that has undefined behavior. I'll make one pass at this.
When you run a C program:

Code:
1 - the OS  creates a stack frame for main.
2 - the os simply overlays that stack on top of existing garbage in memory
3 - it does this for efficiency reasons and because that memory is no longer part of any process.
4 - when your program ran,  those pointers were parked on top of memory that had some existing values in it.
5 - what was in the memory depends on the program that lived in that exact memory before
6 - it could be all 00000000, it could literally be anything.
7 - since it could be anything, it is possible that the memory pointed to (0xfaaa0000) - let's pretend.
8 - 0xfaaa0000 just HAPPENED to be by random chance an OS allocated  location on your existing stack frame.
9 - Now we can use the memory for our program - no crash.
10 Why? because the memory is part of the process so you can what you want to it
11 What if 0xfaaa0000 was NOT part of allocated memory?  Boom, program crash.
12 Therefore there is no known way to predict the behavior of the code, it is undefined

# 7  
Old 01-07-2013
Quote:
Originally Posted by jim mcnamara
There is a set of standards for C. They dictate what will or will not happen in the language.

Doing what you did created something that has undefined behavior. I'll make one pass at this.
When you run a C program:

Code:
1 - the OS  creates a stack frame for main.
2 - the os simply overlays that stack on top of existing garbage in memory
3 - it does this for efficiency reasons and because that memory is no longer part of any process.
4 - when your program ran,  those pointers were parked on top of memory that had some existing values in it.
5 - what was in the memory depends on the program that lived in that exact memory before
6 - it could be all 00000000, it could literally be anything.
7 - since it could be anything, it is possible that the memory pointed to (0xfaaa0000) - let's pretend.
8 - 0xfaaa0000 just HAPPENED to be by random chance an OS allocated  location on your existing stack frame.
9 - Now we can use the memory for our program - no crash.
10 Why? because the memory is part of the process so you can what you want to it
11 What if 0xfaaa0000 was NOT part of allocated memory?  Boom, program crash.
12 Therefore there is no known way to predict the behavior of the code, it is undefined

I will disagree VERY slightly with Jim concerning #3 in the above list. On any UNIX system, data allocated to one process will never be given to another process for use as the stack of a new process. Doing so would create a security hole (covert channel).

Code may be shared between processes (using shared libraries); data may be shared using shared libraries, shared memory segments, mmap()ed files, etc. But address space that will be used as data (including the stack) that is not explicitly shared, will be cleared by the OS before handing it to any user-level process.

There is a lot of code run by a process when a C program starts executing before you get to the first line of code in main(). Shared libraries have to be linked in; the locale has to be initialized; the STDIO stdin, stdout, and stderr streams have to be initialized; etc. Any of these can leave random data in what will eventually become the stack frame allocated to main(), and some of them may leave different things on the stack depending on the time/date when the program was run, the version of the OS or shared libraries being used, etc.

So, the end result is the same. Uninitialized data on the stack of main() can vary from run to run. And if you have other uninitialized pointers being used by argp_parse() or s_catch_signals(), they may also be overwriting anything in your address space and may get segmentation faults on some future execution of your code.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

[Solved] wc behaving weirdly

Can anyone explain why wc is behaving weirdly? Their are only 2 occurrences but wc thinks their are 7 occurrences. I have even manually checked this. $ grep -i base * lit: base xx lit.lst:003- 00103 BASE XX $ grep -i base * | wc -w ... (2 Replies)
Discussion started by: cokedude
2 Replies

2. Shell Programming and Scripting

awk not behaving as expected

Hi, Immediate help on below will be appreciated. I have to read a file (max of 10MB) which will have no new line characters, i.e. data in single line. and have to inster '\n' at every 100 characters. and if record starts with 'BUCA' then need to pick value of length 10 at position 71 and... (7 Replies)
Discussion started by: maks475
7 Replies

3. UNIX and Linux Applications

Linux application upgrade ways

Hello. I need upgrade memcached. This software is installed throuth yum. In official repositories isn`t newest version of memcached, but this one is vulnerable. So looks like I need built it from source, but I dont really want to install c libraries un compilers on system. 1.) So can I compile... (0 Replies)
Discussion started by: jabalv
0 Replies

4. Red Hat

nslookup behaving strangely

I have two servers on same domain. one can nslookup other cannot Psu100 can lookup to psu000, psu010 & psu011 Psu110 can NOT lookup to psu000, psu010 & psu011 I verified resolv.conf entries on both psu000 and psu010 and it contains both name servers (10.200.10.21 & 10.200.11.22).I am... (1 Reply)
Discussion started by: scorohan
1 Replies

5. UNIX for Advanced & Expert Users

FTP behaving erraneous way

Hi Gurus, I tried FTP one file to UNIX which got values like wel^come If I see the content in unix, it shows like wel^Zcome ^ coverted into ^Z (Control + Z ) Can someone please share what is happening here? Thanks, Shahnaz (5 Replies)
Discussion started by: shahnazurs
5 Replies

6. Shell Programming and Scripting

tr command behaving unexpectedly

Im trying to execute the below command on our server to list files and replace the newline in the file list with spaces, but the character 'n' is getting replaced with a space, is there any environment variable that needs to be set in UNIX? sh -c 'ls -trx... (1 Reply)
Discussion started by: rameshrr3
1 Replies

7. Red Hat

application to be run on machines connected in same network

I have a set up of 5 machines which are connected in same network. Now i want to run a small application so that those machines are not ideal. (0 Replies)
Discussion started by: pradeepreddy
0 Replies

8. UNIX for Advanced & Expert Users

csplit not behaving

I have a large file with the first 2 characters of each line determining the type of record. type 03 being a subheader and then it will have multiple 04 records. eg: 03,xxx,xxxx,xxxx 04,xxxxxxxxxxxxxxxxxxxxxxxxxxxx 04,xxxxxxxxxxxxxxxxxxxxxxxxxxxx 03,xxx,xxx,xxx ... (2 Replies)
Discussion started by: badg3r
2 Replies

9. UNIX for Advanced & Expert Users

ftp application behaving erratically

Hi, I am working on a custom made FTP application. The application is behaving erratically for the "ls" command. Wild card character passed to the "ls" command (like "ls *temp") is giving inconsistent results. On debuggin I have found that the "ls" command is implemented as shown below in the... (7 Replies)
Discussion started by: diganta
7 Replies
Login or Register to Ask a Question