Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions idl/spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,32 @@ impl FromStr for IdlType {
}

if s.starts_with('[') {
fn array_from_str(inner: &str) -> IdlType {
fn array_from_str(inner: &str) -> Result<IdlType, anyhow::Error> {
match inner.strip_suffix(']') {
Some(nested_inner) => array_from_str(&nested_inner[1..]),
None => {
let (raw_type, raw_length) = inner.rsplit_once(';').unwrap();
let ty = IdlType::from_str(raw_type).unwrap();
let (raw_type, raw_length) = inner.rsplit_once(';').ok_or_else(|| {
anyhow!(
"Invalid array type syntax: expected '[type; length]', found '{inner}'"
)
})?;

let ty = IdlType::from_str(raw_type).map_err(|e| {
anyhow!(
"Invalid array element type '{raw_type}' in '{inner}': {e}"
)
})?;

let len = match raw_length.replace('_', "").parse::<usize>() {
Ok(len) => IdlArrayLen::Value(len),
Err(_) => IdlArrayLen::Generic(raw_length.to_owned()),
};
IdlType::Array(Box::new(ty), len)

Ok(IdlType::Array(Box::new(ty), len))
}
}
}
return Ok(array_from_str(&s));
return array_from_str(&s);
}

// Defined
Expand Down Expand Up @@ -472,6 +483,19 @@ mod tests {
);
}

#[test]
fn malformed_array_missing_semicolon_returns_err() {
// Previously this would panic due to `.rsplit_once(';').unwrap()`.
assert!(IdlType::from_str("[u8 32]").is_err());
}

#[test]
fn malformed_array_invalid_element_type_returns_err() {
// Previously this would panic due to `IdlType::from_str(raw_type).unwrap()`.
// Use a type that is *syntactically* invalid (missing '>') so `from_str` returns Err.
assert!(IdlType::from_str("[Option<Pubkey; 32]").is_err());
}

#[test]
fn defined() {
assert_eq!(
Expand Down
Loading