Skip to content

Commit d014a69

Browse files
committed
[ot_certs] Fix integer parsing in substition and unittest
Remove an out of date comment, fix a check that expected exact size match instad of at most. Remove padding of in32 since it's not useful. Also fix the unittest which was quite incomplete since it didn't exercise string parsing at all and didn't detect the above error. Signed-off-by: Amaury Pouly <[email protected]>
1 parent a47ab77 commit d014a69

File tree

1 file changed

+30
-61
lines changed

1 file changed

+30
-61
lines changed

sw/host/ot_certs/src/template/subst.rs

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ impl SubstValue {
112112
Ok(self.clone())
113113
}
114114
SubstValue::String(s) => {
115-
// To be consistent with the template, interpret this as
116-
// an integer and convert it to a big-endian byte array.
117-
// See template::hjson::BigUintVisitor.
118-
119115
// Unless the string starts with '0x', expect a decimal string.
120116
let (radix, s) = s
121117
.strip_prefix("0x")
@@ -124,26 +120,19 @@ impl SubstValue {
124120
.with_context(|| format!("cannot parse {s} as an integer"))?;
125121
let bytes = val.to_bytes_be();
126122
ensure!(
127-
size == 0 || bytes.len() == size,
123+
size == 0 || bytes.len() <= size,
128124
"expected an integer that fits on {size} bytes but it uses {} bytes",
129125
bytes.len()
130126
);
131127
Ok(SubstValue::ByteArray(bytes))
132128
}
133129
SubstValue::Int32(x) => {
134-
// TODO: make this check smarter (ie 0xff could fit on byte but is considered to use 4)
135130
let bigint = x.to_bigint().expect("cannot convert a i32 to BigInt");
136-
let mut bytes = bigint.to_signed_bytes_be();
131+
let bytes = bigint.to_signed_bytes_be();
137132
ensure!(
138133
size == 0 || bytes.len() <= size,
139134
"expected an integer that fits on {size} bytes but it uses 4 bytes"
140135
);
141-
// We need to pad to the right size.
142-
let pad = if *x < 0 { 0xff } else { 0x00 };
143-
while bytes.len() < size {
144-
// Pad to preserve sign.
145-
bytes.insert(0, pad);
146-
}
147136
Ok(SubstValue::ByteArray(bytes))
148137
}
149138
_ => bail!("cannot parse value {self:?} as an integer"),
@@ -587,55 +576,35 @@ mod tests {
587576
fn parse_integers() {
588577
// Big-endian integer.
589578
let byte_array = SubstValue::ByteArray(vec![0x3f, 0x2e, 0x1d, 0x0c]);
590-
// Size 0 means any size.
591-
assert_eq!(
592-
byte_array
593-
.parse(&VariableType::Integer { size: 0 })
594-
.unwrap(),
595-
byte_array
596-
);
597-
assert_eq!(
598-
byte_array
599-
.parse(&VariableType::Integer { size: 4 })
600-
.unwrap(),
601-
byte_array
602-
);
603-
// Size does not need not match exactly.
604-
assert_eq!(
605-
byte_array
606-
.parse(&VariableType::Integer { size: 5 })
607-
.unwrap(),
608-
byte_array
609-
);
610-
assert!(byte_array
611-
.parse(&VariableType::Integer { size: 3 })
612-
.is_err());
613-
579+
// Strings: hexdecimal and decimal.
580+
let byte_array_str_hex = SubstValue::String("0x3f2e1d0c".to_string());
581+
let byte_array_str_dec = SubstValue::String("1059986700".to_string());
582+
// Fixed-size integer.
614583
let byte_array_int = SubstValue::Int32(0x3f2e1d0c);
615-
// Size 0 means any size.
616-
assert_eq!(
617-
byte_array_int
618-
.parse(&VariableType::Integer { size: 0 })
619-
.unwrap(),
620-
byte_array
621-
);
622-
assert_eq!(
623-
byte_array_int
624-
.parse(&VariableType::Integer { size: 4 })
625-
.unwrap(),
626-
byte_array
627-
);
628-
assert!(byte_array_int
629-
.parse(&VariableType::Integer { size: 3 })
630-
.is_err());
631-
// A bigger size is acceptable and it should be padded.
632-
let byte_array_pad = SubstValue::ByteArray(vec![0x00, 0x3f, 0x2e, 0x1d, 0x0c]);
633-
assert_eq!(
634-
byte_array_int
635-
.parse(&VariableType::Integer { size: 5 })
636-
.unwrap(),
637-
byte_array_pad
638-
);
584+
585+
for val in [
586+
&byte_array,
587+
&byte_array_int,
588+
&byte_array_str_hex,
589+
&byte_array_str_dec,
590+
] {
591+
// Size 0 means any size.
592+
assert_eq!(
593+
val.parse(&VariableType::Integer { size: 0 }).unwrap(),
594+
byte_array
595+
);
596+
assert_eq!(
597+
val.parse(&VariableType::Integer { size: 4 }).unwrap(),
598+
byte_array
599+
);
600+
// Size does not need not match exactly.
601+
assert_eq!(
602+
val.parse(&VariableType::Integer { size: 5 }).unwrap(),
603+
byte_array
604+
);
605+
// Too small size in an error.
606+
assert!(val.parse(&VariableType::Integer { size: 3 }).is_err());
607+
}
639608
}
640609

641610
/// Test parsing of strings.

0 commit comments

Comments
 (0)