tgetent core dump on sco xenix

Ross Oliver rosso at sco.COM
Sat Jul 2 16:48:21 AEST 1988


In article <54 at libove.UUCP> Jay Libove illistrates the single most
common mistake in large-model 286 programs.  The example he gives,
when compiled using "cc -M2l..." will dump core on execution:

>#include <stdio.h>
>#include <sgtty.h>
>
>main()
>	{
>	char	buffer[1024];
>
>	printf("%d\n",tgetent(buffer,getenv("TERM")));
>	};

Note that the return value of getenv() is not declared, therefore,
the compiler assumes int.  However, tgetent is expecting a char *,
and in large-model 286 programs, int != char *.  So, when main()
calls tgetent, it passes only two of the four bytes returned by
getenv().  tgetent picks up garbage data as the other two bytes,
and (suprise!) a segmentation violation results.  How can this be
fixed?  With the following declaration:

	char *getenv();

Here are a few tips to avoid problems like this in large-model
286 programs:

    - Declare your functions' return values.
    - Don't use "0" to mean NULL ( i.e. (char *)0 ).
    - Don't interchange ints and pointers, or longs and pointers.


Ross Oliver
SCO Technical Support



More information about the Comp.unix.xenix mailing list