1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

printf: make negative values wrap around with unsigned/hex format

To convert from negative to unsigned with overflow, we get the two's complement
representation of the absolute (unsigned) value.
This commit is contained in:
eduardorittner 2025-04-04 20:23:49 -03:00
parent e6ff6d5c69
commit 36ef5010e3
2 changed files with 69 additions and 13 deletions

View file

@ -2,6 +2,8 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore fffffffffffffffc
use uutests::new_ucmd;
use uutests::util::TestScenario;
use uutests::util_name;
@ -668,6 +670,41 @@ fn partial_integer() {
.stderr_is("printf: '42x23': value not completely converted\n");
}
#[test]
fn unsigned_hex_negative_wraparound() {
new_ucmd!()
.args(&["%x", "-0b100"])
.succeeds()
.stdout_only("fffffffffffffffc");
new_ucmd!()
.args(&["%x", "-0100"])
.succeeds()
.stdout_only("ffffffffffffffc0");
new_ucmd!()
.args(&["%x", "-100"])
.succeeds()
.stdout_only("ffffffffffffff9c");
new_ucmd!()
.args(&["%x", "-0x100"])
.succeeds()
.stdout_only("ffffffffffffff00");
new_ucmd!()
.args(&["%x", "-92233720368547758150"])
.fails_with_code(1)
.stdout_is("ffffffffffffffff")
.stderr_is("printf: '-92233720368547758150': Numerical result out of range\n");
new_ucmd!()
.args(&["%u", "-1002233720368547758150"])
.fails_with_code(1)
.stdout_is("18446744073709551615")
.stderr_is("printf: '-1002233720368547758150': Numerical result out of range\n");
}
#[test]
fn test_overflow() {
new_ucmd!()