Back to all reviewers

Validate before type conversions

unionlabs/union
Based on 4 comments
Rust

Always validate input constraints and use safe conversion methods before performing type conversions that could fail or produce undefined behavior. Avoid arbitrary casting with `as` operators, especially when the source value range may not fit the target type.

Null Handling Rust

Reviewer Prompt

Always validate input constraints and use safe conversion methods before performing type conversions that could fail or produce undefined behavior. Avoid arbitrary casting with as operators, especially when the source value range may not fit the target type.

Prefer safe conversion patterns:

  • Use try_into().expect() instead of as casting for fallible conversions
  • Validate input constraints before conversion (e.g., array lengths, value ranges)
  • Consider all possible input values when designing conversions

Example of unsafe vs safe conversion:

// Unsafe - arbitrary casting without validation
let raw = val.to_le_bytes()[0];
let status = Status::try_from(raw as u32)?;

// Safe - validate constraints and use proper error handling
let raw = val.to_le_bytes()[0];
let status = Status::try_from(raw)
    .map_err(|_| ContractError::InvalidClientStatusValue { value: raw })?;

// For address conversions - validate length first
if address_bytes.len() != 20 {
    return Err(eyre!("Invalid address length"));
}
let address = Address::new(address_bytes);

This prevents runtime panics, data corruption, and undefined behavior that can occur when conversions fail silently or when input constraints are violated.

4
Comments Analyzed
Rust
Primary Language
Null Handling
Category

Source Discussions