From: Michal Jaegermann <michal@ellpspace.math.ualberta.ca>
Subject: Re: Compiler Warnings
Date: Sat, 18 Jan 1997 13:47:18 -0700 (MST) [thread overview]
Message-ID: <199701182047.NAA00278@ellpspace.math.ualberta.ca> (raw)
In-Reply-To: <199701181907.OAA07989@sable.cc.vt.edu>
>
> Yes I 100% agree with the point that conversion from pointer to interger
> will lose half value of pointer and we should not write any code that way.
> BUT this is a general rule that applies to normal circumstance. I believe a
> rule can be bent under special/different circumstances.
Possibly. But you have to investigate that "special circumstances"
do apply and that this propagate correctly through all your code.
I have seen too many broken programs because of that to believe that
this is easy (people who programmed on Atari ST or Amiga, where
sizeof(int) *could* have been 2 and sizeof(void *) was usually 4,
collected quite a bit of battle scars because of that - especially
when porting a code of "all the world is VAX" type). Another "nice"
thing was a use of int where sizeof_t was really meant.
With more complicated source it is often easier to fix buggy code
(yes, these are definite bugs) then trying later to hunt down results
in a debugger. Shared libraries make hunts even more interesting. :-)
> need to worry about them. one of the code that got warning message is:
> int offset = (int)cmd->info;
> And I don't see any problem with that line unless there is something I
> don't know.
That exactly what you do not know may come back later and get you. :-)
This particular case indeed looks like that it **may** be safe. In
particular if a cast was in original sources. But this depends how later
'offset' is used and from what. If values stored there indeed will always
fit into an int then you are OK. But if an original programmer decided
that s/he may later recover an address from 'offset', because everybody
"knows" that sizeof(int) == sizeof(void *), then you are a goner.
> also another warning is about an integer been passed into a function then
> the function passed this integer as pointer into another function. there
> should not be any problem later if the pointer is converted to interger.
This is more "iffy" case. If your function expected pointer, there is
no prototype which would force a conversion, and you passed an int,
then you pushed four bytes on an argument stack and grabbed eight.
Now, if there were no other arguments which would become misaligned,
and if you really needed only four bytes of information, and your
code discarded a garbage and not significant bytes, then you will get
away with this (or maybe you will only see later "unaligned access"
wailings). Otherwise effects are too familiar. :-)
> Other warnings are simliar to the cases I just showed above.
It may be that conversions are harmless in some particular case.
But proving that for non-trivial programs is much harder than it looks.
Another "interesting" gotcha is to access a pointer or long through
a pointer to int. Unfortunately your compiler may be silent on this
one.
The whole point is that if somebody asks "can I safely disregard
compiler warnings about type conversions" then a general answer is NO.
Michal
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
next parent reply other threads:[~1997-01-18 20:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <199701181907.OAA07989@sable.cc.vt.edu>
1997-01-18 20:47 ` Michal Jaegermann [this message]
1997-01-18 21:31 ` Compiler Warnings Richard Henderson
2010-01-18 19:07 Roger
[not found] <199701180109.JAA03936@tapa.admu.edu.ph>
1997-01-18 16:22 ` Richard Henderson
1997-01-20 11:41 ` Jim Nance
-- strict thread matches above, loose matches on Subject: below --
1997-01-18 3:04 Roger
1997-01-18 17:27 ` Michal Jaegermann
1997-01-18 1:09 Pablo Manalastas
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox