[comp.unix.wizards] Possible Bug in BSD

sefunix%sefe.decnet@nwc-143b.arpa (02/26/87)

We have run into a little problem on both Ultrix-32m (DEC microvax)
and MT. Xinu 4.3BSD on a DEC microvax. Can someone try this program
on their system and see if they also get a segmentation violation
on the fopen of argv[1]. 

And if so, why?

#include <stdio.h>

struct abc
{
	short Red;
	short Green;
	short Blue;
	short Alpha;
};

main (argc, argv)
int argc; 
char **argv;
{
	FILE *f1, *fopen();

	struct abc point[151][451];

	if (argc !=2)
	{
	   puts("\n Usage: InputFileName \n");
	   exit(1);
	}

	if((f1 = fopen(argv[1], "r")) == NULL )
	{
	   printf("I can't open %s\n", argv[1]);
	   exit(1);
	}

	fclose(f1);
}

-------------------------------------------------------------

Thanks,
	Gene Guglielmo
        Naval Weapons Center
	sefunix@nwc-143b
------

chris@mimsy.UUCP (02/26/87)

In article <4685@brl-adm.ARPA> sefunix%sefe.decnet@nwc-143b.arpa
(SEFE::SEFUNIX) writes:
>... try this program on [your] system and see if [you] also get
>a segmentation violation on the fopen of argv[1] [and] if so, why?

>struct abc { short Red; short Green; short Blue; short Alpha; };

>main ...
>	FILE *f1, *fopen();
>	struct abc point[151][451];

Funny how these things come up in bursts.  See recent comp.lang.c
(Info-C digest) articles for details, but you have here 151*451*8
or 544808 bytes of stack.  This is larger than the default stacksize
resource limit, so the next stack access causes a segmentation
fault.  To fix it, raise your stacklimit, or move the data off
the stack.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu

ed@mtxinu.UUCP (02/26/87)

In article <4685@brl-adm.ARPA> sefunix%sefe.decnet@nwc-143b.arpa (SEFE::SEFUNIX) writes:
>We have run into a little problem on both Ultrix-32m (DEC microvax)
>and MT. Xinu 4.3BSD on a DEC microvax. Can someone try this program
>on their system and see if they also get a segmentation violation
>on the fopen of argv[1]. 
>
>And if so, why?
>
>#include <stdio.h>
>
>struct abc
>{
>	short Red;
>	short Green;
>	short Blue;
>	short Alpha;
>};
>
>main (argc, argv)
>int argc; 
>char **argv;
>{
>	FILE *f1, *fopen();
>
>	struct abc point[151][451];
>
>	if (argc !=2)
>	{
>	   puts("\n Usage: InputFileName \n");
>	   exit(1);
>	}
>
>	if((f1 = fopen(argv[1], "r")) == NULL )
>	{
>	   printf("I can't open %s\n", argv[1]);
>	   exit(1);
>	}
>
>	fclose(f1);
>}

A little investigation with adb shows that it crashes while pushing an
argument to fopen() onto the stack.  Notice that there is a *large*
automatic array (point) declared - a total of 544808 bytes (plus
whatever else is on the stack).  The default limit for stack size is
512k.  Increasing the stack limit (with the csh(1) limit command or
with a system call from the program) will solve the problem.  Note,
too, that it crashes just as well pushing the argument to fputs() if it's
called with the wrong number of arguments.

-- 
Ed Gould                    mt Xinu, 2560 Ninth St., Berkeley, CA  94710  USA
{ucbvax,decvax}!mtxinu!ed   +1 415 644 0146

"A man of quality is not threatened by a woman of equality."

rgoguen@mikey.bbn.COM (02/26/87)

change struct abc point[X][Y]
to
       static struct abc point[X][Y]

This is because your stack segement it TOO big. I think the current limit is
512K. adding static will cause the space to be allocated before it starts 
running as opposed to allocating it off of the stack.

/rjg rgoguen@relay.cs.net

madd@bucsb.bu.edu.UUCP (02/27/87)

In article <4685@brl-adm.ARPA> sefunix%sefe.decnet@nwc-143b.arpa (SEFE::SEFUNIX) writes:
>We have run into a little problem on both Ultrix-32m (DEC microvax)
>and MT. Xinu 4.3BSD on a DEC microvax. Can someone try this program
>on their system and see if they also get a segmentation violation
>on the fopen of argv[1]. 
>
>And if so, why?
>
>#include <stdio.h>
>
>struct abc
>{
>	short Red;
>	short Green;
>	short Blue;
>	short Alpha;
>};
>
>main (argc, argv)
>int argc; 
 vvvvvvvvvvvv
>char **argv;
 ^^^^^^^^^^^^
>{
>	FILE *f1, *fopen();
>
>	struct abc point[151][451];
>
>	if (argc !=2)
>	{
>	   puts("\n Usage: InputFileName \n");
>	   exit(1);
>	}
>
>	if((f1 = fopen(argv[1], "r")) == NULL )
>	{
>	   printf("I can't open %s\n", argv[1]);
>	   exit(1);
>	}
>
>	fclose(f1);
>}

You really shouldn't have declared char **argv if you want to treat
argv as an array.  Instead, change that declaration to *argv[], or
reference it as a pointer structure (eg *(argv+1)==argv[1]).  I
suspect that C doesn't treat arrays of pointers exactly as it treats
pointers to pointers, but I'm not really that sharp on the subject.  I
just treat them as if they were two completely different types of
variables, and this keeps things simple.  Some C compilers will give
you "type mismatch" errors when you define a pointer value and then
reference it as an array.  The 4.3BSD compiler on our vax 750 did not,
however.

jim frost
madd@bucsf.bu.edu
..!harvard!bu-cs!bucsf!madd