Skip to content

Commit 03813c2

Browse files
authored
fix off-by-one (#267)
* fix off-by-one * Added tests * MSRV fix * Fix linter by writing tests
1 parent d6dadc6 commit 03813c2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/tag.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Tag {
8383
/// Returns the tag's value as a `u8` if the `value` component fits in a
8484
/// short form (value < 31) or `None` if this is a long-form tag.
8585
pub fn as_u8(&self) -> Option<u8> {
86-
if self.value > 0x1f {
86+
if self.value >= 0x1f {
8787
return None;
8888
}
8989
Some(
@@ -104,7 +104,7 @@ impl Tag {
104104
} else {
105105
0
106106
};
107-
if self.value > 0x1f {
107+
if self.value >= 0x1f {
108108
b |= 0x1f;
109109
dest.push(b);
110110
let len = base128::base128_length(self.value);
@@ -121,3 +121,18 @@ impl Tag {
121121
self.constructed
122122
}
123123
}
124+
125+
#[cfg(test)]
126+
mod tests {
127+
use super::{Tag, TagClass};
128+
129+
#[test]
130+
fn test_as_u8() {
131+
for (t, expected) in &[
132+
(Tag::new(5, TagClass::Application, true), Some(0x65)),
133+
(Tag::new(0x1f, TagClass::Universal, false), None),
134+
] {
135+
assert_eq!(&t.as_u8(), expected);
136+
}
137+
}
138+
}

src/writer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ mod tests {
589589
parse_single::<Tlv>(b"\x1f\x81\x80\x01\x00").unwrap(),
590590
b"\x1f\x81\x80\x01\x00",
591591
),
592+
(
593+
parse_single::<Tlv>(b"\x1f\x1f\x00").unwrap(),
594+
b"\x1f\x1f\x00",
595+
),
592596
]);
593597
}
594598
}

0 commit comments

Comments
 (0)