system/mawk: (32-bit big-endian) printf %ld regression from 20200725a to 20200731
Found by the JasPer 2.0.33
and 3.0.6
test suites on 32-bit ppc. Minimal reproducer:
# 32-bit ppc
$ echo 1 | awk '{printf "%d\n%ld\n", $1,$1}'
1
0
# 32-bit x86
$ echo 1 | awk '{printf "%d\n%ld\n", $1,$1}'
1
1
# 32-bit arm
$ echo 1 | awk '{printf "%d\n%ld\n", $1,$1}'
1
1
additionally, 32- and 64- bit SPARC (big-endian) environments were tested:
zv@gcc102:~/mawk-snapshots-t20230404$ echo 1 | ./build64/mawk '{printf "%d\n%ld\n", $1,$1}'
1
1
zv@gcc102:~/mawk-snapshots-t20230404$ echo 1 | ./build32/mawk '{printf "%d\n%ld\n", $1,$1}'
1
0
-
mawk
version20200725a
correctly implements therealtoint()
function below -
mawk
version20200731
seems to return0
for all tested inputs
Original real-world reproducer:
$ . jpcod
$ image_size=16 rate=200 evalexpr "$image_size * $rate + 1" | realtoint
0
Smaller real-world reproducer:
$ . jpcod
$ echo 123.45 | realtoint
0
A tiny derivative of the original jpcod
script is here: jpcod
#!/bin/sh
evalexpr()
{
echo "" | awk 'BEGIN{printf "%f", '"$*"'}'
}
realtoint()
{
awk '
{
for (i = 1; i <= NF; ++i) {
v = $i;
printf "%ld", v + 0.5;
if (i < NF) {
printf " ";
} else {
printf "\n";
}
}
}
'
}
Demonstration:
$ mawk -W version
mawk 1.3.4 20200725
Copyright 2008-2019,2020, Thomas E. Dickey
Copyright 1991-1996,2014, Michael D. Brennan
random-funcs: srandom/random
regex-funcs: internal
compiled limits:
sprintf buffer 8192
maximum-integer 2147483647
$ image_size=16 rate=200 evalexpr "$image_size * $rate + 1" | realtoint
28783
and then
$ mawk -W version
mawk 1.3.4 20200731
Copyright 2008-2019,2020, Thomas E. Dickey
Copyright 1991-1996,2014, Michael D. Brennan
random-funcs: srandom/random
regex-funcs: internal
compiled limits:
sprintf buffer 8192
maximum-integer 9223372036854775808
$ image_size=16 rate=200 evalexpr "$image_size * $rate + 1" | realtoint
0
Note that if we change:
-printf "%ld", v + 0.5;
+printf "%d", v + 0.5;
then we get:
$ . jpcod
$ echo 123.45 | realtoint
123
and
$ . jpcod
$ image_size=16 rate=200 evalexpr "$image_size * $rate + 1" | realtoint
28783
The CHANGELOG
only says (Debian #303825):
20200731
+ amend fix for Debian #303825 to more closely match gawk and BWK.
+ add --help and --version options
20200724
+ modify logic for "{0}" in rexp.c so that a left-parenthesis is not
trimmed when cancelling the previous token.
+ modify RE_cat to add a dummy M_ACCEPT as a workaround for logic in
the new RE_init_it_cnt and RE_set_limit functions.
+ modify regexp_system.c to work with interval-expressions.
+ recognize gawk's --traditional and --posix as aliases for mawk's
-W options, as well as -r and --re-interval to improve scripting
compatibility.
+ add -W compat and -W posix, using the former to replace
-W repetitions and the latter to replace -W posix_space
+ enable interval-expressions by default.