Skip to content

Commit ab57ce2

Browse files
committed
Validate root_version before using it as index.
- get_type_version() now returns latest version when root_version is out of bounds - Add negative unit test Signed-off-by: Andrei Sandu <[email protected]>
1 parent eb52b6d commit ab57ce2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"coverage_score": 92.9, "exclude_path": "", "crate_features": ""}
1+
{"coverage_score": 93.0, "exclude_path": "", "crate_features": ""}

src/version_map.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ impl VersionMap {
4646
}
4747

4848
/// Returns the version of `type_id` corresponding to the specified `root_version`.
49+
/// If `root_version` is out of range returns the version of `type_id` at latest version.
4950
pub fn get_type_version(&self, root_version: u16, type_id: TypeId) -> u16 {
50-
let version_space = self.versions.split_at(root_version as usize).0;
51+
let version_space = if root_version > self.latest_version() || root_version == 0 {
52+
self.versions.as_slice()
53+
} else {
54+
self.versions.split_at(root_version as usize).0
55+
};
5156

5257
for i in (0..version_space.len()).rev() {
5358
if let Some(version) = version_space[i].get(&type_id) {
@@ -154,4 +159,16 @@ mod tests {
154159
let vm = VersionMap::new();
155160
assert_eq!(vm.get_type_version(1, TypeId::of::<MyType>()), BASE_VERSION);
156161
}
162+
163+
#[test]
164+
fn test_invalid_root_version() {
165+
let mut vm = VersionMap::new();
166+
vm.new_version().set_type_version(TypeId::of::<MyType>(), 2);
167+
168+
assert_eq!(vm.get_type_version(0, TypeId::of::<MyType>()), 2);
169+
170+
assert_eq!(vm.latest_version(), 2);
171+
assert_eq!(vm.get_type_version(129, TypeId::of::<MyType>()), 2);
172+
assert_eq!(vm.get_type_version(1, TypeId::of::<MyType>()), BASE_VERSION);
173+
}
157174
}

0 commit comments

Comments
 (0)