Richard Heathfield wrote:
>
> Tomás Ó hÉilidhe said:
>
> <snip>
>
> > You get down to it. You open up the network drive and navigate to James
> > Weir's source file. Its context is "run on anything". You're looking
> > thru it and you come to the following section of code:
> >
> > typedef union ConfigFlags {
> > unsigned entire; /* Write to all bytes
> > char unsigned bytes[sizeof(unsigned)]; at once or access
> > } ConfigFlags; them individually
> > */
> >
> > int IsRemoteAdminEnabled(ConfigFlags const cf)
> > {
> > return cf.bytes[3] & 0x3u;
> > }
>
> This won't even run on MS-DOS, let alone "anything".
And we're assuming that the issue about the comment is just a typo
by the OP, and not in "James Weir's source file".
> > You look at this code and you think, "Hmm, this chap plans to write to
> > 'entire' and then subsequently read individual bytes by using the
> > 'bytes' member of the structure".
>
> No, I look at this code and I think, "someone has assumed that unsigned
> ints are at least four bytes wide", which isn't true on typical MS-DOS
> systems (yes, they're still used, believe it or not), isn't true on
> various DSPs, and came within a gnat's whisker of being true on at least
> one Cray.
And perhaps unsigned ints are 8 bytes on some new 64-bit systems? (At
least the above then doesn't invoke UB, however.)
> > You have a second suspicion that
> > perhaps James might have made assumptions about the size of "unsigned",
> > but inspecting the code you find that he hasn't.
>
> Yes, he has.
He's also made an assumption about endianness.
> > Now, the question is, in the real world, at 10:13am on a sunny Thursday
> > morning, sitting at your desk with a hot cup of tea, munching away on a
> > fig-roll bar getting small crumbs between the keys on the keyboard, are
> > you really going to reject this code?
>
> Absolutely, yes.
Ditto. Even though every system I happen to work on at the moment
uses 4-byte unsigned ints, some are big-endian and others are little.
[...]
> > What I'm trying to get across is, that, while we may discuss in black
> > and white what the Standard permits and what it forbids... are we really
> > going to be so obtuse as to reject this code in the real world?
>
> No, we're really going to be so acute as to reject this code in the real
> world.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |
www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net |
www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <private.php?do=newpm&u=>