Inexplicable System call failure - HELP - expert needed 
Author Message
 Inexplicable System call failure - HELP - expert needed

Howdy folks,

I'm working on a sun solaris machine here at school working on my big
fourth-year comp-sci project in c, and I've just come across possibly
the most confusing and inexplicable behavior I've ever seen.  I've
managed to narrow down the problem to what appears to be the
following:

I have a call to fscanf, and later a system call is made.  For some
reason the system call is not executed.  HOWEVER, if I comment out the
fscanf call, then the system call is executed COMPLETELY NORMALLY.
WHY?  argh.  My narrowed down program I've written for the purose of
this posting exhibits exactly this behavior:

#include <stdlib.h>
#include <stdio.h>

int main(){
  char *s;
  FILE *in = fopen("good.txt", "r");
  fscanf(in, "%s", s);
  system("echo hello");

Quote:
}

That's it, and if I compile this by typing "gcc -o out stuff.c" at the
command line, and then I run the out executable, there will be no
hello echo'd to the terminal, however, if I were to comment out the
fscanf, the hello would be echo'd as would be expected.

Here's some details that might be useful I guess:  I'm using the gcc
v2.95.3 compiler on our Sparc Sun-Solaris 5.6 OS.  My name is Erik,
and I'm at a complete and utter loss.  If you have any suggestions on
other places I might post this cry for help, or on an explanation of
what is going on, or more importantly, how to fix or avert this
problem (other than just not using one of the calls) it would be
greatly appreciated.
--



Thu, 28 Jul 2005 02:23:33 GMT  
 Inexplicable System call failure - HELP - expert needed

Quote:

> I'm working on a sun solaris machine here at school working on my big
> fourth-year comp-sci project in c, and I've just come across possibly
> the most confusing and inexplicable behavior I've ever seen.  I've
> managed to narrow down the problem to what appears to be the
> following:

Please follow good Usenet etiquette by not multi-posting.  We've
already responded to you in comp.lang.c.  Don't waste everyone's
time by making us read your article twice.
--
"I consider that the golden rule requires that if I like a program
 I must share it with other people who like it."
--Richard Stallman
--



Thu, 28 Jul 2005 11:47:12 GMT  
 Inexplicable System call failure - HELP - expert needed

Quote:
> I'm working on a sun solaris machine here at school working on my big
> fourth-year comp-sci project in c, and I've just come across possibly

It seems to me that you should have progressed beyond this point by the
fourth year.

Quote:
> the most confusing and inexplicable behavior I've ever seen.  I've

Let's not exagerate

Quote:
> I have a call to fscanf, and later a system call is made.  For some
> reason the system call is not executed.  HOWEVER, if I comment out the
> fscanf call, then the system call is executed COMPLETELY NORMALLY.
> WHY?  argh.  My narrowed down program I've written for the purose of
> this posting exhibits exactly this behavior:

> #include <stdlib.h>
> #include <stdio.h>

> int main(){
>   char *s;

s points to la-la land.

Quote:
>   FILE *in = fopen("good.txt", "r");

You don't check if fopen succeeded.

Quote:
>   fscanf(in, "%s", s);

fscanf reads some characters and stores them at the location pointed to
by s. as s does not point to valid storage, anything is possible from
this point on.

Quote:
>   system("echo hello");
> }

--
A. Sinan Unur

Remove dashes for address

--



Thu, 28 Jul 2005 11:47:17 GMT  
 Inexplicable System call failure - HELP - expert needed

Quote:

> I'm working on a sun solaris machine here at school working on my big
> fourth-year comp-sci project in c, and I've just come across possibly
> the most confusing and inexplicable behavior I've ever seen.  I've
> managed to narrow down the problem to what appears to be the
> following:

> I have a call to fscanf, and later a system call is made.  For some
> reason the system call is not executed.  HOWEVER, if I comment out the
> fscanf call, then the system call is executed COMPLETELY NORMALLY.
> WHY?  argh.  My narrowed down program I've written for the purose of
> this posting exhibits exactly this behavior:

> #include <stdlib.h>
> #include <stdio.h>

> int main(){
>   char *s;
>   FILE *in = fopen("good.txt", "r");
>   fscanf(in, "%s", s);

Since the pointer s hasn't been initialized, fscanf() will store
the string it reads at whetever arbitrary location s points to.
This is probably corrupting something.

Quote:
>   system("echo hello");
> }

> That's it, and if I compile this by typing "gcc -o out stuff.c" at the
> command line, and then I run the out executable, there will be no
> hello echo'd to the terminal, however, if I were to comment out the
> fscanf, the hello would be echo'd as would be expected.

The pointer s needs to point to a known location that's big enough
to hold the string that fscanf() reads.  Fix that and your program
should work.  I'd also recommend reading the string in a manner
that will prevent a buffer overflow -- as Henry Spencer said, "for
surely where thou typest 'foo' someone someday shall type
'supercalifragilisticexpialidocious'."

Quote:
> Here's some details that might be useful I guess:  I'm using the gcc
> v2.95.3 compiler on our Sparc Sun-Solaris 5.6 OS.  My name is Erik,
> and I'm at a complete and utter loss.  If you have any suggestions on
> other places I might post this cry for help, or on an explanation of
> what is going on, or more importantly, how to fix or avert this
> problem (other than just not using one of the calls) it would be
> greatly appreciated.

To avoid pointer-related problems, learn how pointers work.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
--



Thu, 28 Jul 2005 11:47:16 GMT  
 Inexplicable System call failure - HELP - expert needed

Quote:

> Howdy folks,

> I'm working on a sun solaris machine here at school working on my big
> fourth-year comp-sci project in c, and I've just come across possibly
> the most confusing and inexplicable behavior I've ever seen.  I've
> managed to narrow down the problem to what appears to be the
> following:

> I have a call to fscanf, and later a system call is made.  For some
> reason the system call is not executed.  HOWEVER, if I comment out the
> fscanf call, then the system call is executed COMPLETELY NORMALLY.
> WHY?  argh.  My narrowed down program I've written for the purose of
> this posting exhibits exactly this behavior:

Why do you keep multi-posting this?  If you at least had the
courtesy to cross-post it people wouldn't be annoyed by seeing it
pop up like spam everywhere.  Didn't you like the answers you
already got on c.l.c?

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Thu, 28 Jul 2005 11:47:20 GMT  
 Inexplicable System call failure - HELP - expert needed

Quote:

> #include <stdlib.h>
> #include <stdio.h>

> int main(){
>   char *s;
>   FILE *in = fopen("good.txt", "r");
>   fscanf(in, "%s", s);
>   system("echo hello");
> }

You forgot to allocate memory into which fscanf() is to write the result
- fscanf() doesn't do that for you. So what happens here is that
fscanf() writes the data to whatever address happens to be in s,
clobbering your memory.

Besides, a safer way of reading arbitrarily long strings is using
fgets() where you can pass the length of the string buffer you provide -
less danger of buffer overflow exploits.
--



Thu, 28 Jul 2005 11:47:21 GMT  
 Inexplicable System call failure - HELP - expert needed

Quote:
>Howdy folks,

>I'm working on a sun solaris machine here at school working on my big
>fourth-year comp-sci project in c, and I've just come across possibly
>the most confusing and inexplicable behavior I've ever seen.  I've
>managed to narrow down the problem to what appears to be the
>following:

>I have a call to fscanf, and later a system call is made.  For some
>reason the system call is not executed.  HOWEVER, if I comment out the
>fscanf call, then the system call is executed COMPLETELY NORMALLY.
>WHY?  argh.  My narrowed down program I've written for the purose of
>this posting exhibits exactly this behavior:

>#include <stdlib.h>
>#include <stdio.h>

>int main(){
>  char *s;
>  FILE *in = fopen("good.txt", "r");
>  fscanf(in, "%s", s);
>  system("echo hello");
>}

>That's it, and if I compile this by typing "gcc -o out stuff.c" at the
>command line, and then I run the out executable, there will be no
>hello echo'd to the terminal, however, if I were to comment out the
>fscanf, the hello would be echo'd as would be expected.

>Here's some details that might be useful I guess:  I'm using the gcc
>v2.95.3 compiler on our Sparc Sun-Solaris 5.6 OS.  My name is Erik,
>and I'm at a complete and utter loss.  If you have any suggestions on
>other places I might post this cry for help, or on an explanation of
>what is going on, or more importantly, how to fix or avert this
>problem (other than just not using one of the calls) it would be
>greatly appreciated.

The undefined behavior in your code was explained at length in
comp.lang.c.

<<Remove the del for email>>
--



Fri, 29 Jul 2005 12:38:24 GMT  
 Inexplicable System call failure - HELP - expert needed


...
# #include <stdlib.h>
# #include <stdio.h>
#
# int main(){
#  char *s;
#  FILE *in = fopen("good.txt", "r");
#  fscanf(in, "%s", s);
#  system("echo hello");
# }
#
# That's it, and if I compile this by typing "gcc -o out stuff.c" at the
# command line, and then I run the out executable, there will be no
# hello echo'd to the terminal

It dumps core on my sun/gcc system here...

# however, if I were to comment out the
# fscanf, the hello would be echo'd as would be expected.

You are using s uninitialized. Where do you think it points to?
Please read http://www.eskimo.com/~scs/C-faq/q7.1.html

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Sat, 30 Jul 2005 14:15:51 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Inexplicable system call failure! HELP-Expert Needed

2. Failure of popen and system calls ......

3. URGENT : Help needed with system()/fork() call

4. system call in Sun OS - need help

5. Help with system calls needed

6. Experts: Help!!! SYSTEM command and grabbing STDOUT

7. HELP:perform system call or call an external program

8. Compile Error=> Need help from expert

9. Needed help from Expert to slove my error

10. Recursive experts -- I need help!

11. RS232 Expert help needed

12. Newbie Need Helps from you C expert !!!

 

 
Powered by phpBB® Forum Software