btf: avoid `unreachable!()` and `unwrap()`

reviewable/pr1251/r58
Tamir Duberstein 1 week ago
parent 3ade19b869
commit 122cf17a43
No known key found for this signature in database

@ -703,40 +703,53 @@ impl<'a> AccessSpec<'a> {
bit_offset: 0, bit_offset: 0,
} }
} }
RelocationKind::EnumVariantExists | RelocationKind::EnumVariantValue => match ty { RelocationKind::EnumVariantExists | RelocationKind::EnumVariantValue => {
BtfType::Enum(_) | BtfType::Enum64(_) => { let index = || match parts.as_slice() {
if parts.len() != 1 { [index] => Ok(*index),
return Err(RelocationError::InvalidAccessString { _ => Err(RelocationError::InvalidAccessString {
access_str: spec.to_string(), access_str: spec.to_string(),
}); }),
} };
let index = parts[0];
let (n_variants, name_offset) = match ty { let (n_variants, name_offset, index) = match ty {
BtfType::Enum(en) => ( BtfType::Enum(en) => {
let index = index()?;
(
en.variants.len(), en.variants.len(),
en.variants.get(index).map(|v| v.name_offset), en.variants.get(index).map(|v| v.name_offset),
), index,
BtfType::Enum64(en) => ( )
}
BtfType::Enum64(en) => {
let index = index()?;
(
en.variants.len(), en.variants.len(),
en.variants.get(index).map(|v| v.name_offset), en.variants.get(index).map(|v| v.name_offset),
), index,
_ => unreachable!(), )
}
_ => {
return Err(RelocationError::InvalidRelocationKindForType {
relocation_number: relocation.number,
relocation_kind: format!("{:?}", relocation.kind),
type_kind: format!("{:?}", ty.kind()),
error: "enum relocation on non-enum type",
});
}
}; };
let name_offset =
if name_offset.is_none() { name_offset.ok_or_else(|| RelocationError::InvalidAccessIndex {
return Err(RelocationError::InvalidAccessIndex {
type_name: btf.err_type_name(ty), type_name: btf.err_type_name(ty),
spec: spec.to_string(), spec: spec.to_string(),
index, index,
max_index: n_variants, max_index: n_variants,
error: "tried to access nonexistant enum variant", error: "tried to access nonexistant enum variant",
}); })?;
} let name = btf.string_at(name_offset)?;
let accessors = vec![Accessor { let accessors = vec![Accessor {
type_id, type_id,
index, index,
name: Some(btf.string_at(name_offset.unwrap())?.to_string()), name: Some(name.to_string()),
}]; }];
AccessSpec { AccessSpec {
@ -748,15 +761,6 @@ impl<'a> AccessSpec<'a> {
bit_offset: 0, bit_offset: 0,
} }
} }
_ => {
return Err(RelocationError::InvalidRelocationKindForType {
relocation_number: relocation.number,
relocation_kind: format!("{:?}", relocation.kind),
type_kind: format!("{:?}", ty.kind()),
error: "enum relocation on non-enum type",
});
}
},
RelocationKind::FieldByteOffset RelocationKind::FieldByteOffset
| RelocationKind::FieldByteSize | RelocationKind::FieldByteSize

Loading…
Cancel
Save