Skip to content

program: Add const asserts#73

Merged
febo merged 3 commits into
mainfrom
febo/const-asserts
Jun 1, 2026
Merged

program: Add const asserts#73
febo merged 3 commits into
mainfrom
febo/const-asserts

Conversation

@febo
Copy link
Copy Markdown
Contributor

@febo febo commented Jun 1, 2026

Problem

Many of the structs of the program are interpreted directly from bytes assuming 1-byte alignment. In some cases, structs have constraints that their size must match. While these are correct in the implementation, there are no checks to ensure they remain valid in case of future updates.

Solution

Add const asserts on structs.

@febo febo force-pushed the febo/const-asserts branch from 6e1499c to 26ef975 Compare June 1, 2026 14:08
Comment thread program/src/state/data.rs
if bytes.len() < ExternalData::LEN {
return Err(ProgramError::InvalidArgument);
}
if !(bytes.as_ptr() as usize).is_multiple_of(align_of::<ExternalData>()) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExternalData is the only struct that has an alignment different than 1.

}

/// Instruction data expected by the `Write` instruction.
#[repr(C)]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.

@febo febo requested a review from joncinque June 1, 2026 14:15
Copy link
Copy Markdown
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@febo febo merged commit e640717 into main Jun 1, 2026
21 checks passed
@febo febo deleted the febo/const-asserts branch June 1, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants