Bug 15051

Summary: [PATCH] Awk: fix bitwise functions result/arguments truncation on 32bit platforms
Product: Busybox Reporter: Carlos Ibáñez <caribpa>
Component: OtherAssignee: unassigned
Status: NEW ---    
Severity: normal CC: busybox-cvs
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Host: Target:
Build:
Attachments: awk-bitwise-fix-arch32.patch

Description Carlos Ibáñez 2022-10-09 11:54:05 UTC
Created attachment 9386 [details]
awk-bitwise-fix-arch32.patch

Hi!

On platforms that use 32bit longs data is lost when performing bitwise operations if the result and/or arguments are equal-to/greater-than 2^32.

This behavior is unexpected as Busybox Awk numbers are doubles[1] (typically up to 2^53) and this issue is not present on GNU Awk.

Example 1 - Result equal-to/greater-than 2^32:

  awk 'BEGIN{print lshift(1, 32)}'

It returns 1, instead of the expected 4294967296.


Example 2 - Arguments equal-to/greater-than 2^32:

  awk 'BEGIN{print and(2^32, 2^32)}'

It returns 0, instead of the expected 4294967296.


The patch attached simply changes the castings and type of the getvar_i_int function from long to long long.

Note that the patch found in bug 15036[3] adds two functions that operate/return long numbers that shall also be changed into long long when accepted.

Cheers,
Carlos


[1] - https://git.busybox.net/busybox/tree/editors/awk.c?id=c8c1fcdba163f264a503380bc63485aacd09214c#n123
[2] - https://git.busybox.net/busybox/tree/editors/awk.c?id=c8c1fcdba163f264a503380bc63485aacd09214c#n1048
[3] - https://bugs.busybox.net/show_bug.cgi?id=15036