[Bug 4405] expressions in include/archive.h (XZ magic) are undefined on big-endian architectures

bugzilla at busybox.net bugzilla at busybox.net
Mon Nov 7 11:31:14 UTC 2011


https://bugs.busybox.net/show_bug.cgi?id=4405

--- Comment #2 from Michael Tokarev <mjt+busybox at tls.msk.ru> 2011-11-07 11:31:13 UTC ---
I just checked (finally come to it) -- well, yes, the proposed

  XZ_MAGIC1a  = 256 * (unsigned)(256 * (256 * 0xfd + '7') + 'z') + 'X'

does work on big endian (I tried on mips machine), as in, gcc does not warn
about integer overflow anymore in this place.  But the result is that
XZ_MAGIC1a gets promoted to unsigned long long instead of int or unsigned int,
ie, internally it is interpreted as 64bit number.  So for the following
program:

enum foo {
 XZ_MAGIC1a  = ((0xfd * 256 + '7') * 256 + 'z') * 256 + 'X',
 XZ_MAGIC1b = 256 * (unsigned)(256 * (256 * 0xfd + '7') + 'z') + 'X',
};

#include <stdio.h>
int main() {
 printf("1=%08x 2=%08x\n", XZ_MAGIC1a, XZ_MAGIC1b);
 return 0;
}

it produces this:
test.c:2:49: warning: integer overflow in expression [-Woverflow]
test.c: In function 'main':
test.c:8:2: warning: format '%x' expects argument of type 'unsigned int', but
argument 3 has type 'long long int' [-Wformat]

So I added another cast:

 XZ_MAGIC1b = (unsigned)(256 * (unsigned)(256 * (256 * 0xfd + '7') + 'z') +
'X'),

but the result is still interpreted as long long int -- ie, gcc gives exactly
the same warning.

Note that 0xfd377a58, when used in enum, is also promoted to unsigned long
long.  And note also that the original version, while gcc warns about it,
actually works correctly.

I think the best will be to use a #define with this direct value (0xfd377a58),
which is still much easier to read and verify than any of the expressions used
in this file, -- especially if there's a comment around telling that 0x37 is
'7', 0x7a is 'z' and 0x58 is 'X'.  And it will continue to work even if on a
target platform some other - non-ascii - encoding is used, like ebcdic, since
the (binary) input will still contain the same 0x7a, 0x58 codes, even if 'z'
and 'X' will have different codes on the target platform.

Thanks,

/mjt

-- 
Configure bugmail: https://bugs.busybox.net/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


More information about the busybox-cvs mailing list