Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Reject a LeakyRelu alpha that is not a single float instead of reading it as 0 (#1530)

MarekSpodBiedryf865aa28c376
diff --git a/crates/denoise/src/onnx.rs b/crates/denoise/src/onnx.rs
index fc84a36cf..074963695 100644
--- a/crates/denoise/src/onnx.rs
+++ b/crates/denoise/src/onnx.rs
@@ -97,7 +97,12 @@ fn read_bytes(bytes: &[u8]) -> Result<Net, NetError> {
if next.input.len() != 1 || next.output.len() != 1 || next.attribute.iter().any(|a| a.name != "alpha") {
return invalid("malformed LeakyRelu");
}
- let alpha = attr(next, "alpha").map_or(0.01, |a| a.f);
+ // only an omitted alpha gets the ONNX default; one that is not a single float must not read as 0
+ let alpha = match next.attribute.as_slice() {
+ [] => 0.01,
+ [AttributeProto { f: Some(f), kind: 0 | 1, .. }] => *f,
+ _ => return invalid("LeakyRelu alpha is not a single float"),
+ };
i += 1;
let name = next.output.first().cloned().ok_or_else(|| NetError::Invalid("LeakyRelu without an output".into()))?;
return Ok((Some(alpha), name));
@@ -319,6 +324,25 @@ mod tests {
let _ = std::fs::remove_file(p);
}
+ #[test]
+ fn a_leaky_relu_alpha_that_is_not_a_float_is_rejected() {
+ let good = unet_onnx(4, 3, 1, 7);
+ assert!(read_bytes(&good).is_ok());
+ // an alpha attribute: float field 2, then the declared type (field 20) FLOAT
+ let mut alpha = vec![0x15];
+ alpha.extend_from_slice(&0.2f32.to_le_bytes());
+ let at = good.windows(alpha.len()).position(|w| w == alpha.as_slice()).unwrap() + alpha.len();
+ assert_eq!(good[at..at + 3], [0xa0, 0x01, 1]);
+ // declared INT: the float must not be taken as the slope
+ let mut wrong_type = good.clone();
+ wrong_type[at + 2] = 2;
+ assert!(matches!(read_bytes(&wrong_type), Err(NetError::Invalid(_))));
+ // declared FLOAT but no float value (the float field becomes an int field 3 = 0 of the same length): must not read as 0
+ let mut no_float = good.clone();
+ no_float[at - 5..at].copy_from_slice(&[0x18, 0x80, 0x80, 0x80, 0x00]);
+ assert!(matches!(read_bytes(&no_float), Err(NetError::Invalid(_))));
+ }
+
#[test]
fn an_explicit_empty_or_nonvector_bias_is_not_treated_as_missing() {
let w = TensorProto { name: "w".into(), dims: vec![12, 4, 1, 1], data_type: 1, float_data: vec![0.1; 48], ..Default::default() };
diff --git a/crates/denoise/src/onnx_proto.rs b/crates/denoise/src/onnx_proto.rs
index d8bbba411..a331f0fe5 100644
--- a/crates/denoise/src/onnx_proto.rs
+++ b/crates/denoise/src/onnx_proto.rs
@@ -113,7 +113,10 @@ fn ints(w: Wire<'_>, out: &mut Vec<i64>, cap: usize) -> Result<()> {
#[derive(Default)]
pub(super) struct AttributeProto {
pub name: String,
- pub f: f32,
+ /// `None` when the file has no float value for the attribute.
+ pub f: Option<f32>,
+ /// The declared attribute type (1 float, 2 int, 3 string, 7 ints); 0 when the file does not say.
+ pub kind: i64,
pub i: i64,
pub s: Vec<u8>,
pub ints: Vec<i64>,
@@ -151,7 +154,7 @@ fn attribute(b: &[u8]) -> Result<AttributeProto> {
while let Some((n, w)) = fields.next()? {
match (n, w) {
(1, Wire::Bytes(b)) => a.name = string(b)?,
- (2, Wire::F32(v)) => a.f = v,
+ (2, Wire::F32(v)) if a.f.is_none() => a.f = Some(v),
(3, Wire::Int(v)) => a.i = v as i64,
(4, Wire::Bytes(b)) => {
if b.len() > MAX_STRING {
@@ -160,7 +163,7 @@ fn attribute(b: &[u8]) -> Result<AttributeProto> {
a.s = b.to_vec();
}
(8, w) => ints(w, &mut a.ints, 16)?,
- (20, Wire::Int(1 | 2 | 3 | 7)) => {}
+ (20, Wire::Int(v @ (1 | 2 | 3 | 7))) if a.kind == 0 => a.kind = v as i64,
_ => return Err(NetError::Unsupported("unknown attribute value".into())),
}
}