Skip to content

Commit 90d5f2d

Browse files
committed
AVRO-4024: [Rust] Accept only "Nan", "INF", "-INF", "Infinity" and "-Infinity"
This is what the Java SDK (via Jackson library) supports (#3066). This is what the C# SDK also would support (#3070) Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
1 parent 0af4e1b commit 90d5f2d

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

lang/rust/avro/src/types.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,10 @@ impl Value {
933933
/// IEEE 754 NaN and infinities are not valid JSON numbers.
934934
/// So they are represented in JSON as strings.
935935
fn parse_special_float(value: &str) -> Option<f32> {
936-
match value.trim().to_ascii_lowercase().as_str() {
937-
"nan" | "+nan" | "-nan" => Some(f32::NAN),
938-
"inf" | "+inf" | "infinity" | "+infinity" => Some(f32::INFINITY),
939-
"-inf" | "-infinity" => Some(f32::NEG_INFINITY),
936+
match value {
937+
"NaN" => Some(f32::NAN),
938+
"INF" | "Infinity" => Some(f32::INFINITY),
939+
"-INF" | "-Infinity" => Some(f32::NEG_INFINITY),
940940
_ => None,
941941
}
942942
}
@@ -3142,10 +3142,10 @@ Field with name '"b"' is not a member of the map items"#,
31423142
#[test]
31433143
fn avro_4024_resolve_double_from_unknown_string_err() -> TestResult {
31443144
let schema = Schema::parse_str(r#"{"type": "double"}"#)?;
3145-
let value = Value::String("blah".to_owned());
3145+
let value = Value::String("unknown".to_owned());
31463146
match value.resolve(&schema) {
31473147
Err(err @ Error::GetDouble(_)) => {
3148-
assert_eq!(format!("{err:?}"), r#"Double expected, got String("blah")"#);
3148+
assert_eq!(format!("{err:?}"), r#"Double expected, got String("unknown")"#);
31493149
}
31503150
other => {
31513151
panic!("Expected Error::GetDouble, got {other:?}");
@@ -3157,10 +3157,10 @@ Field with name '"b"' is not a member of the map items"#,
31573157
#[test]
31583158
fn avro_4024_resolve_float_from_unknown_string_err() -> TestResult {
31593159
let schema = Schema::parse_str(r#"{"type": "float"}"#)?;
3160-
let value = Value::String("blah".to_owned());
3160+
let value = Value::String("unknown".to_owned());
31613161
match value.resolve(&schema) {
31623162
Err(err @ Error::GetFloat(_)) => {
3163-
assert_eq!(format!("{err:?}"), r#"Float expected, got String("blah")"#);
3163+
assert_eq!(format!("{err:?}"), r#"Float expected, got String("unknown")"#);
31643164
}
31653165
other => {
31663166
panic!("Expected Error::GetFloat, got {other:?}");

lang/rust/avro/tests/io.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,30 @@ fn default_value_examples() -> &'static Vec<(&'static str, &'static str, Value)>
107107
(r#""long""#, "5", Value::Long(5)),
108108
(r#""float""#, "1.1", Value::Float(1.1)),
109109
(r#""double""#, "1.1", Value::Double(1.1)),
110-
(r#""float""#, r#"" +inf ""#, Value::Float(f32::INFINITY)),
110+
(r#""float""#, r#""INF""#, Value::Float(f32::INFINITY)),
111+
(r#""double""#, r#""INF""#, Value::Double(f64::INFINITY)),
112+
(
113+
r#""float""#,
114+
r#""Infinity""#,
115+
Value::Float(f32::INFINITY),
116+
),
117+
(
118+
r#""float""#,
119+
r#""-Infinity""#,
120+
Value::Float(f32::NEG_INFINITY),
121+
),
122+
(
123+
r#""double""#,
124+
r#""Infinity""#,
125+
Value::Double(f64::INFINITY),
126+
),
111127
(
112128
r#""double""#,
113129
r#""-Infinity""#,
114130
Value::Double(f64::NEG_INFINITY),
115131
),
116-
(r#""float""#, r#""-NAN""#, Value::Float(f32::NAN)),
117-
(r#""double""#, r#""-NAN""#, Value::Double(f64::NAN)),
132+
(r#""float""#, r#""NaN""#, Value::Float(f32::NAN)),
133+
(r#""double""#, r#""NaN""#, Value::Double(f64::NAN)),
118134
(
119135
r#"{"type": "fixed", "name": "F", "size": 2}"#,
120136
r#""a""#,

0 commit comments

Comments
 (0)