tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Casting pointer to short or _Bool (grischka6.1)


From: David A. Wheeler
Subject: Re: [Tinycc-devel] Casting pointer to short or _Bool (grischka6.1)
Date: Wed, 02 May 2007 15:48:06 -0400 (EDT)

I said:
> > Here's another grishka-based patch for tcc.  It applies cleanly to 
> > Landley's 
> fork, and I believe it'll apply cleanly to mainline.
> > Support casting pointer to short or _Bool (grischka-2005-09-25 bugfix 6.1).

Rob asked:
> Hmmm...  Does gcc print a warning?  The typecast is indeed dodgy, but it's 
> also explicit.  The code _asked_ to do something stupid.

gcc 4.1.1 does give a warning for the conversion to short (either via cast or 
assignment), and NOT for _Bool (either cast or assignment). A test program is 
below, along with gcc, tcc, and tcc-patched results.  Without the patch, tcc 
error-fails when it sees a pointer->short cast or assignment. Both gcc and 
patched-tcc give warnings on the cast and assignment to short, both are quiet 
about the assignment to _Bool, and both succeed in generating code (that 
produces the same test case answer).

There are plausible arguments on whether or not a warning makes sense here. I 
can't say I strongly care either way, so I just let it do "what gcc does".  If 
it's to be a warning, it's argument for adding a way to turn off specific 
warnings or having a lint-like "turn off the next warning" convention (my 
"flawfinder" tool does that).  But I'm not worrying that right now; it's the 
"fail on error" of current tcc that is my main problem.

> Or does it do this on a normal assignment, rather than just a typecast?

Unmodified tcc also fails on normal (uncast) assignment from pointer to short, 
same reason.

>  (In which case we should be warning about an assignment from pointer to int 
> as 
> well, since that's nonportable to 64-bit targets, which tcc will need 
> soonish.  (It should be pointer to long, guaranteed portable on both 32 and 
> 64 bit Unix by the LP64 standard.))

I suspect there are still many programs that assume that pointers can be 
losslessly assigned to int, even though that has NEVER been guaranteed and 
there's decades of literature saying "don't do this stupid thing" :-(.   There 
are some gcc flags like "-Wno-pointer-to-int-cast" that might be nice to add 
support for.  But anyway...

> I'm happy to apply it to my fork, but I'd prefer to tweak the warning one way 
> or the other, and would like to hear your opinion on that first...

The patch I posted makes tcc act more-or-less like gcc.  The patched tcc warns 
about the casting and assignment in the same circumstance (cast/assign from 
pointer to short) as gcc does.  It also includes another warning about uncast 
value-changing assignment, which isn't in gcc but is actually a pretty 
reasonable warning message, and isn't something my patch added (it was there in 
the first place).

So I _think_ I've covered the bases.  That said, I think this is an example of 
why the original patchset never went anywhere. It's got some good stuff, but 
it's WAY too large of a patch. Patches should be small and cover only one 
issue, so that they can be reasonably examined and discussed one at a time.

But let me know if something different is important to you.

--- David A. Wheeler



----- End Original Message -----


Test case:
==============================
/* Try casting pointer to short or _Bool (grishka case_6.1). This
 * is lossy, so tcc will print a warning.  This capability is needed
 * to compile gcc 2.95 as well as other programs.  */

#include <stdio.h>
main()
{
    void *p = (void *) 3;
    short s;
    _Bool b;
    printf("Expect 3 1 -> %hd %hhd\n",
        (short) p,
        (_Bool) p); /* Expect warning */
    s = p;
    b = p;
    printf("Expect 3 1 -> %hd %hhd\n", s, b);
}




gcc 4.1.1 output:
================================
,1.c: In function &#8216;main&#8217;:
,1.c:12: warning: cast from pointer to integer of different size
,1.c:13: warning: assignment makes integer from pointer without a cast



Running the gcc-compiled result of the test case produces:
==================================
Expect 3 1 -> 3 1

Unmodified tcc (rl fork 1.0.0) compilation fails, with this message:
===================================
,1.c:12: cannot use pointers here

Patched tcc succeeds, with these messages:
================================================
,1.c:12: warning: nonportable conversion from pointer to char/short
,1.c:14: warning: assignment makes integer from pointer without a cast
,1.c:14: warning: nonportable conversion from pointer to char/short




reply via email to

[Prev in Thread] Current Thread [Next in Thread]