Strcpy on SysV vs. BSD.

Conor P. Cahill cpcahil at virtech.uucp
Mon Sep 3 00:15:36 AEST 1990


In article <24351 at adm.BRL.MIL> hsw at sparta.com (Howard Weiss) writes:
>main(){
>  char *TTx = "/dev/";
>  char tty[10]; /* works on both SysV and BSD */
>/*  char *tty;	/* works only on BSD */

While you might get away with the *tty stuff for small strings on BSD systems,
it in very bad code.  What is hapening is that *tty is getting a default 
value (probably Zero in this case) and you are then writing data to that
location (which has not been allocated to you and which *should* result in
a core dump). 

The fact that you didn't get a core dump probably means that the startup 
code on your BSD system happens to leave some non-zero data on the stack
that just happens to point to an area that you can write to.  This is not
expected behavior and will probably not work in all cases (even under your
BSD system).  

Remember, you can NEVER NEVER NEVER write to an area pointed to by a pointer
that you have not yet initialized (set to point to a data area that you 
can write to) and expect the code to work.

>I've worked on UNIX systems since V6 (in 1976) and I've never seen
>this before.

I don't know what systems you have tried this on before, but It should
fail on almost every system (or at least it should clobber some other
variables).

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.unix.questions mailing list