diff --git a/pkgs/id/src/tuple.rs b/pkgs/id/src/tuple.rs index c05f878aff..911a5cdbc9 100644 --- a/pkgs/id/src/tuple.rs +++ b/pkgs/id/src/tuple.rs @@ -456,6 +456,11 @@ fn decode_one(data: &[u8], pos: &mut usize, nested: bool) -> Result Result2^63 wraps via the + // wrapping negation below to a zero or positive value — a key + // that sorts and compares as a different number than it claims. + ensure!( + (1..=(1u64 << 63)).contains(&abs), + "negative integer magnitude out of range" + ); + // Non-minimal encodings are rejected, as in the positive branch: + // the encoder's minimal byte count makes the magnitude's top byte + // non-zero, i.e. the first payload byte is never 0xFF. + ensure!(be[8 - n] != 0, "non-minimal negative integer encoding"); // Negate: handles i64::MIN correctly via wrapping // (abs=2^63 → 0u64.wrapping_sub(2^63) = 2^63 as u64 → as i64 = i64::MIN) #[allow(clippy::cast_possible_wrap)] @@ -494,7 +512,13 @@ fn decode_one(data: &[u8], pos: &mut usize, nested: bool) -> Result bail!("unknown tuple type tag: 0x{tag:02X}"), @@ -1132,6 +1156,52 @@ mod tests { assert!(result.is_err()); } + #[test] + fn test_decode_negative_int_magnitude_range() { + // abs == 0 (ones-complement of 0x00): would decode to Int(0). + assert!(decode(&[0x13, 0xFF]).is_err()); + // abs == u64::MAX (payload is ones-complement of 0xFF * 8): would + // wrap via the negation to Int(1). + assert!(decode(&[NEG_INT_8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]).is_err()); + // abs == 2^63 + 1: would wrap to a positive value. + assert!(decode(&[NEG_INT_8, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]).is_err()); + } + + #[test] + fn test_decode_negative_int_min_boundary() { + // abs == 2^63 is i64::MIN and stays decodable. + let vals = decode(&[NEG_INT_8, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap(); + assert_eq!(vals, vec![TupleValue::Int(i64::MIN)]); + // The encoder's own i64::MIN output round-trips. + let key = TupleEncoder::new().int(i64::MIN).build(); + assert_eq!(decode(&key).unwrap(), vec![TupleValue::Int(i64::MIN)]); + } + + #[test] + fn test_decode_non_minimal_int_rejected() { + // Positive 5 spelled in two bytes with a leading zero. + assert!(decode(&[POS_INT_1 + 1, 0x00, 0x05]).is_err()); + // Positive zero under an int tag: zero has its own ZERO tag. + assert!(decode(&[POS_INT_1, 0x00]).is_err()); + // Negative -5 spelled in two bytes (ones-complement of [0x00, 0x05]). + assert!(decode(&[0x12, 0xFF, 0xFA]).is_err()); + } + + #[test] + fn test_decode_nan_float_rejected() { + // Stored form of a positive NaN (the sort transform flips the sign + // bit of a positive float's bytes). + assert!(decode(&[FLOAT64, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]).is_err()); + // Stored form of a negative NaN (the transform flips every bit). + assert!(decode(&[FLOAT64, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).is_err()); + // Infinities are encodable, so they stay decodable. + let key = TupleEncoder::new().float(f64::INFINITY).unwrap().build(); + assert_eq!( + decode(&key).unwrap(), + vec![TupleValue::Float(f64::INFINITY)] + ); + } + #[test] fn test_decode_unterminated_string() { // String tag followed by data but no null terminator