Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkgs/id/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ fn decode_one(data: &[u8], pos: &mut usize, nested: bool) -> Result<Option<Tuple
// keys decode to floats that compare unequal to everything,
// including themselves. Infinities stay decodable: they encode.
ensure!(!v.is_nan(), "decoded float64 is NaN");
// The encoder canonicalizes either zero sign to +0.0, so the
// corresponding transformed -0.0 payload is not a valid key.
ensure!(
v != 0.0 || !v.is_sign_negative(),
"decoded float64 is non-canonical negative zero"
);
Ok(Some(TupleValue::Float(v)))
}

Expand Down Expand Up @@ -1202,6 +1208,29 @@ mod tests {
);
}

#[test]
fn test_decode_noncanonical_negative_zero_rejected() {
// The encoder canonicalizes -0.0 to +0.0, so this transformed
// negative-zero payload is not a form it can emit.
assert!(decode(&[FLOAT64, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).is_err());

let negative_zero = TupleEncoder::new().float(-0.0).unwrap().build();
let positive_zero = TupleEncoder::new().float(0.0).unwrap().build();
assert_eq!(negative_zero, positive_zero);
assert_eq!(
decode(&negative_zero).unwrap(),
vec![TupleValue::Float(0.0)]
);
}

#[test]
fn test_decode_float_boundaries_stay_decodable() {
for value in [-f64::from_bits(1), -1.5, f64::INFINITY, f64::NEG_INFINITY] {
let key = TupleEncoder::new().float(value).unwrap().build();
assert_eq!(decode(&key).unwrap(), vec![TupleValue::Float(value)]);
}
}

#[test]
fn test_decode_unterminated_string() {
// String tag followed by data but no null terminator
Expand Down