Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Bug in OSF1.32 C compiler on alpha
@ 1996-07-01  4:00 Nigel Goddard
  1996-07-01  4:00 ` Steve Peltz
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Nigel Goddard @ 1996-07-01  4:00 UTC (permalink / raw)
  To: ngoddard


Is this one known?  Is there a fix?

Briefly, code taking the address of a function parameter declared float 
in the old style is not compiled correctly.

Here is a sample program

#include <stdio.h>

static void G(px)
     float *px;
{
  (void) printf("%f\n", *px);
}

static void F(x)
     float x;
{
  (void) printf("%f\n", x);
  G(&x);
}

int main ()
{
  F(0.2);
  return(0);
}

Here is its output:

axpfeb{66} cc -std0 bug.c -o bug
axpfeb{67} bug
0.200000
-0.000000
axpfeb{68} 

Nigel Goddard




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug in OSF1.32 C compiler on alpha
  1996-07-01  4:00 Bug in OSF1.32 C compiler on alpha Nigel Goddard
@ 1996-07-01  4:00 ` Steve Peltz
  1996-07-02  4:00   ` Dan Pop
  1996-07-01  4:00 ` Daryl Clevenger
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Steve Peltz @ 1996-07-01  4:00 UTC (permalink / raw)



In article <4r965l$inj@news.psc.edu>, Nigel Goddard  <ngoddard@psc.edu> wrote:
>static void F(x)
>     float x;
>{
>  (void) printf("%f\n", x);
>  G(&x);
>}

The "float" you have there is really a double, according to the promotion
rules. So you're passing a pointer to a double to a function which expects
a pointer to a float.

Try adding the following line:

	printf("%d %d %d\n", sizeof x, sizeof(float), sizeof(double));

It shows 8 4 8.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug in OSF1.32 C compiler on alpha
  1996-07-01  4:00 Bug in OSF1.32 C compiler on alpha Nigel Goddard
  1996-07-01  4:00 ` Steve Peltz
@ 1996-07-01  4:00 ` Daryl Clevenger
  1996-07-02  4:00 ` Dan Pop
  1996-07-03  4:00 ` Andy V
  3 siblings, 0 replies; 8+ messages in thread
From: Daryl Clevenger @ 1996-07-01  4:00 UTC (permalink / raw)



In article <4r965l$inj@news.psc.edu>, Nigel Goddard  <ngoddard@psc.edu> wrote:
>Is this one known?  Is there a fix?
>
>Briefly, code taking the address of a function parameter declared float 
>in the old style is not compiled correctly.

I am not so sure that this is actually a bug.  I don't have a copy of the
old K&R, but the second addition of K&R, section A7.3.2, Function Calls
has this to say (p 202, the 3rd full paragraph in my copy of the text)

"If the function declaration in scope for a call is old-style, then
default argument promotion is applied to each argument as follows:
integral promotion (sec A6.1) is performed on each argument of integral
type, and each float argument is converted to double.  The effect of
the call is undefined if the number of arguments disagrees with the
number of parameters in the definition of the function, or if the type
of an argument after promotion disagrees with that of the corresponding
parameter.  Type agreement depends on whether the function's definition
is new-style or old-style.  If it is old-style, then the comparison is
between the the promoted type of the argument of the call and the promoted
type of the parameter; ..."

The way I see it, the progam does the right thing until G is called.
Since F promotes x to double, you are really calling G(&x) with a
double *, which is not the same as a float *.  So in G(), *px only
retrieves the first 4 bytes at px and then promotes this to a double.
The are no automatic promotion rules for pointers, so the behavior
may be correct since the type of the argument (double *) disagrees
with the type of the parameter (float *).

>
>Here is a sample program
>
>#include <stdio.h>
>
>static void G(px)
>     float *px;
>{
>  (void) printf("%f\n", *px);
>}
>
>static void F(x)
>     float x;
>{
>  (void) printf("%f\n", x);
>  G(&x);
>}
>
>int main ()
>{
>  F(0.2);
>  return(0);
>}
>
>Here is its output:
>
>axpfeb{66} cc -std0 bug.c -o bug
>axpfeb{67} bug
>0.200000
>-0.000000
>axpfeb{68} 
>
>Nigel Goddard
>


-- 
Daryl Clevenger
Member Technical Staff
Transarc Corporation
dlc@cs.cmu.edu



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug in OSF1.32 C compiler on alpha
  1996-07-01  4:00 Bug in OSF1.32 C compiler on alpha Nigel Goddard
  1996-07-01  4:00 ` Steve Peltz
  1996-07-01  4:00 ` Daryl Clevenger
@ 1996-07-02  4:00 ` Dan Pop
  1996-07-03  4:00 ` Andy V
  3 siblings, 0 replies; 8+ messages in thread
From: Dan Pop @ 1996-07-02  4:00 UTC (permalink / raw)



In <4r965l$inj@news.psc.edu> Nigel Goddard <ngoddard@psc.edu> writes:

>Is this one known?  Is there a fix?

Yes and yes.

>Briefly, code taking the address of a function parameter declared float 
>in the old style is not compiled correctly.

A function parameter declared float in an old style function definition
has the type double.

>Here is a sample program
>
>#include <stdio.h>
>
>static void G(px)
>     float *px;
>{
>  (void) printf("%f\n", *px);
>}
>
>static void F(x)
>     float x;

The compiler is required to treat this declaration as

      double x;

>{
>  (void) printf("%f\n", x);
>  G(&x);

Now you pass a pointer to double to a function expecting a pointer to
float.  All the bets are off.

>}
>
>int main ()
>{
>  F(0.2);

The type of the constant 0.2 is double, but this is OK, because the
function F actually expects a double argument.

>  return(0);
>}

Since the bug is in your code, _you_ have to fix it.  Either by switching
to ANSI prototypes or by declaring the argument of the function G as

    double *px;

Dan
--
Dan Pop
CERN, CN Division
Email: Dan.Pop@cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug in OSF1.32 C compiler on alpha
  1996-07-01  4:00 ` Steve Peltz
@ 1996-07-02  4:00   ` Dan Pop
  1996-07-08  4:00     ` Steve Peltz
  0 siblings, 1 reply; 8+ messages in thread
From: Dan Pop @ 1996-07-02  4:00 UTC (permalink / raw)



In <4r9jsg$p3h@vixen.cso.uiuc.edu> peltz@jaka.ece.uiuc.edu (Steve Peltz) writes:

>Try adding the following line:
>
>	printf("%d %d %d\n", sizeof x, sizeof(float), sizeof(double));
>
>It shows 8 4 8.

Which may or may not have any meaning, because your call invokes 
undefined behaviour.  %d expects an int argument, but the sizeof operator
_never_ produces an int value.

Dan
--
Dan Pop
CERN, CN Division
Email: Dan.Pop@cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug in OSF1.32 C compiler on alpha
  1996-07-01  4:00 Bug in OSF1.32 C compiler on alpha Nigel Goddard
                   ` (2 preceding siblings ...)
  1996-07-02  4:00 ` Dan Pop
@ 1996-07-03  4:00 ` Andy V
  3 siblings, 0 replies; 8+ messages in thread
From: Andy V @ 1996-07-03  4:00 UTC (permalink / raw)




Nigel Goddard <ngoddard@psc.edu> writes:

>Briefly, code taking the address of a function parameter declared float 
>in the old style is not compiled correctly.

Actually, the program itself is broken. You are passing the address
of a double (float is re-written to double per old C rules) to a
routine that expects the address of a float. This will `work' on
a VAX or ALPHA using F- and D-float formats because the first half
of a D-float format looks `just like' the near-equivalent F-float
format.

Moral -- move to function prototypes as quickly as possible.


>static void G(px)
>     float *px;
>{
>  (void) printf("%f\n", *px);
>}

This says I have a routine G that assumes it will get a pointer to
a float; but don't let the compiler notice when it is called with
anything else.

>static void F(x)
>     float x;
>{
>  (void) printf("%f\n", x);
>  G(&x);
>}

This says I have a routine F that assumes it will get a double (per
old C argument re-writing rules) and passes its address to routine G.
The compiler isn't allowed to know that G expects the address of a float,
or it would complain.

If you do:

static void G (float *px) { (void) printf ("%f\n", *px);}
static void F (float x) { (void) printf ("%f\n", x); G(&x); }

it will work perfectly (barring typos), or if you do:

static void G (float *px) { (void) printf ("%f\n", *px);}
static void F (double x) { (void) printf ("%f\n", x); G(&x); }

it will complain about the mis-matched arguments.

-- 
Andy V (A Digital spoke, not a Digital Spokesperson)
mailto:andy.vesper@eng.pko.dec.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug in OSF1.32 C compiler on alpha
  1996-07-02  4:00   ` Dan Pop
@ 1996-07-08  4:00     ` Steve Peltz
  1996-07-08  4:00       ` Dan Pop
  0 siblings, 1 reply; 8+ messages in thread
From: Steve Peltz @ 1996-07-08  4:00 UTC (permalink / raw)



In article <danpop.836336015@news.cern.ch>, Dan Pop <Dan.Pop@cern.ch> wrote:
>In <4r9jsg$p3h@vixen.cso.uiuc.edu> peltz@jaka.ece.uiuc.edu (Steve Peltz) writes:
>>	printf("%d %d %d\n", sizeof x, sizeof(float), sizeof(double));
>>
>>It shows 8 4 8.
>
>Which may or may not have any meaning, because your call invokes 
>undefined behaviour.  %d expects an int argument, but the sizeof operator
>_never_ produces an int value.

Since we were talking about a specific compiler on a specific machine, it
seemed unnecessary to specify any excess luggage to demonstrate the point.
In fact, sizeof results in unsigned long in this case.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug in OSF1.32 C compiler on alpha
  1996-07-08  4:00     ` Steve Peltz
@ 1996-07-08  4:00       ` Dan Pop
  0 siblings, 0 replies; 8+ messages in thread
From: Dan Pop @ 1996-07-08  4:00 UTC (permalink / raw)



In <4rq5ud$qe3@vixen.cso.uiuc.edu> peltz@jaka.ece.uiuc.edu (Steve Peltz) writes:

>In article <danpop.836336015@news.cern.ch>, Dan Pop <Dan.Pop@cern.ch> wrote:
>>In <4r9jsg$p3h@vixen.cso.uiuc.edu> peltz@jaka.ece.uiuc.edu (Steve Peltz) writes:
>>>	printf("%d %d %d\n", sizeof x, sizeof(float), sizeof(double));
>>>
>>>It shows 8 4 8.
>>
>>Which may or may not have any meaning, because your call invokes 
>>undefined behaviour.  %d expects an int argument, but the sizeof operator
>>_never_ produces an int value.
>
>Since we were talking about a specific compiler on a specific machine, it
>seemed unnecessary to specify any excess luggage to demonstrate the point.
>In fact, sizeof results in unsigned long in this case.

And %d expects int, a type of a different size.  If the output looked
meaningful at all it is because of an implementation quirk (32-bit int's
are widened to 64 bits when passed as arguments).

But my point was that you can't use broken code to prove anything
(other than your poor understanding of the language :-)

Dan 
--
Dan Pop
CERN, CN Division
Email: Dan.Pop@cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~1996-07-08  4:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-07-01  4:00 Bug in OSF1.32 C compiler on alpha Nigel Goddard
1996-07-01  4:00 ` Steve Peltz
1996-07-02  4:00   ` Dan Pop
1996-07-08  4:00     ` Steve Peltz
1996-07-08  4:00       ` Dan Pop
1996-07-01  4:00 ` Daryl Clevenger
1996-07-02  4:00 ` Dan Pop
1996-07-03  4:00 ` Andy V

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox