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

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

@ -703,60 +703,64 @@ 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),
),
_ => unreachable!(),
};
if name_offset.is_none() {
return Err(RelocationError::InvalidAccessIndex {
type_name: btf.err_type_name(ty),
spec: spec.to_string(),
index, index,
max_index: n_variants, )
error: "tried to access nonexistant enum variant", }
_ => {
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 accessors = vec![Accessor { };
type_id, let name_offset =
name_offset.ok_or_else(|| RelocationError::InvalidAccessIndex {
type_name: btf.err_type_name(ty),
spec: spec.to_string(),
index, index,
name: Some(btf.string_at(name_offset.unwrap())?.to_string()), max_index: n_variants,
}]; error: "tried to access nonexistant enum variant",
})?;
AccessSpec { let name = btf.string_at(name_offset)?;
btf, let accessors = vec![Accessor {
root_type_id, type_id,
relocation, index,
parts, name: Some(name.to_string()),
accessors, }];
bit_offset: 0,
} AccessSpec {
} btf,
_ => { root_type_id,
return Err(RelocationError::InvalidRelocationKindForType { relocation,
relocation_number: relocation.number, parts,
relocation_kind: format!("{:?}", relocation.kind), accessors,
type_kind: format!("{:?}", ty.kind()), bit_offset: 0,
error: "enum relocation on non-enum type",
});
} }
}, }
RelocationKind::FieldByteOffset RelocationKind::FieldByteOffset
| RelocationKind::FieldByteSize | RelocationKind::FieldByteSize

Loading…
Cancel
Save