lib/getchar: Oversized CSI parameters overflow signed int
On trunk 95c91326ad8b9f698f2c7b7ae8b65bdb1437207a, both BIOS and UEFI versions of input_sequence() accumulate an arbitrary-length decimal CSI parameter in a signed int without checking the multiply and addition.
For example, the BIOS parser built normally on a platform with 32-bit int decodes 4294967299~ as Delete because the overflowed value becomes 3. UBSan stops at the multiplication with:
runtime error: signed integer overflow: 429496729 * 10 cannot be represented in type 'int'
The exact result is undefined and may vary with the compiler. Unsupported parameters should be consumed and ignored rather than becoming another supported key.
Expected behaviour:
3~,5~,6~, and21~keep producing Delete, Page Up, Page Down, and F10.- Leading zeroes remain accepted.
- Parameters that exceed
INT_MAXconsume their terminator and return no key. - BIOS serial input and UEFI console input follow the same rule.
Prepared fix: 8c3afc4f. I am opening the corresponding PR immediately.
Validation of the prepared fix:
- Before the fix, an actual-source BIOS harness reproduced the Delete alias and UBSan diagnostic.
- After the fix, 11 parser cases passed in optimised and UBSan builds, including all supported numeric keys, arrows, leading zeroes,
INT_MAX,INT_MAX + 1, the wrap-to-Delete value, a 40-digit value, and terminator consumption. - Full
--enable-all --enable-werrorbuilds passed with GCC and Clang.
Fixed by #679