[comp.sys.next] f2c fails to WRITE certain variables -- C LIBRARY BUG

eric@ee.ualberta.ca (Eric Norum) (05/07/91)

In <tennant.673220953@xanth>, tennant@xanth.msfc.nasa.gov (Allyn F.
Tennant - ES65) writes:
> I'm currently using the 'f2c' Fortran compiler on my NextStep 1.0a system.
> Alas, the following the WRITE in the following program will crash with
> a Segmentation violation at run time (all values of X from 1.E6 to
> 9.99...E7 seem to cause the crash).  I dragged the f2c source over to
> a SUN and this problem did not appear.  Also, the C code generated by
> f2c on the NeXT looks OK, so the problem appears to be with the f2c
> run time library as implemented on the NeXT.
> 
> 1) Does anyone else have this problem?
> 
> 2) Will f2c under 2.0/2.1 fix this problem?
> 
> 3) Any C hackers out there willing to fix this?
> 
> The program:
> 
>       REAL X
> C---
>       X=1.E7
>       WRITE(*,*) X
>       END

Allyn asked for replies by email, but I'm posting since this bug arises from
one of my pet peeves with the NeXT C library.  Whoever wrote the `%g' section
of printf apparently didn't understand the concept of significant figures.
I've posted articles about this, pestered my NeXT campus consultant(s) and
managed to get absolutely nowhere.  The bug is in NextStep 2.0, too.

The f2c I/O library calls `sprintf (buf, fmt, n)', where `buf' is a pointer
to an array of 20 characters, `fmt' is " %# .9g", and `n' is the number to be
printed.  The `#' in the format forces the result to contain a decimal point,
and inhibits removal of trailing zeros.  The ` ' format says a space is to
be left before a positive number.  The `9' specifies the `g' format result
should contain 9 significant digits (according to K+R II and ANSI).

On every other machine I could try this on the result was `  10000000.0'
which *does* have 9 digits.
On the NeXT the result was `  10000000.000000000' which means that the
trailing '\0' is written outside the buffer and wrecks the stack frame.

The quick fix is to make the buffer bigger, and live with the awful-looking
output.  The *real* fix would be for NeXT to clean up their library.

So how about it, NeXT??

BTW, while you're fixing printf, make sure that
	printf ("%.3g", 0.0001234);
prints `0.000123' like every other machine does, instead of `0' like the
NeXT does now.

-- 
Eric Norum
Dept. of Electrical Engineering      eric@ee.ualberta.ca
University of Alberta
Edmonton, Canada.                   phone: (403) 492-4626