On 2022-01-31, Mike Scott <
[email protected]d> wrote:
I've hit an interesting problem. This is on fdbsd13/arm64/rpi4, and
there's something amiss with char to int conversions. Or at least, a difference from i386.
On arm64, plain "char" is "unsigned char". On x86 it's "signed char".
char c;
while ( ( c = getopt_long( argc, argv, shortopts, longopts, &idx ) ) !=
-1 ) {
That's broken code. It must be "int c". Only an int will capture
all possible char values and additionally -1.
The prototypical example is this:
while ((c = getchar()) != EOF) { /* EOF is -1 */
...
}
With "char c" this is broken for both platforms:
* On arm64 it's an infinite loop.
* On x86 it will prematurely terminate when it encounters a byte
with the value 0xff.
Any variable that is intended to hold any character plus EOF _must_
be declared int. This is basic C.
Most architectures default to signed char. Notably arm and powerpc,
in their 32 and 64-bit incarnations, default to unsigned.
--
Christian "naddy" Weisgerber
[email protected]
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)