Skip to content

Commit f869967

Browse files
committed
fold in sysvar errors as previously
1 parent 2ce5492 commit f869967

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

sysvar/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ macro_rules! impl_sysvar_get {
183183
};
184184
// Variant for sysvars with padding at the end. Loads bincode-serialized data
185185
// (size - padding bytes) and zeros the padding to avoid undefined behavior.
186-
// Only supports sysvars where padding is at the end of the layout.
186+
// Only supports sysvars where padding is at the end of the layout. Caller
187+
// must supply the correct number of padding bytes.
187188
($sysvar_id:expr, $padding:literal) => {
188189
fn get() -> Result<Self, $crate::__private::ProgramError> {
189190
let mut var = core::mem::MaybeUninit::<Self>::uninit();
@@ -193,12 +194,19 @@ macro_rules! impl_sysvar_get {
193194
// SAFETY: The allocation is valid for `size_of::<Self>()`. We load
194195
// `(size - padding)` bytes from the syscall, which matches bincode
195196
// serialization. The remaining `padding` bytes are then zeroed.
196-
unsafe {
197-
$crate::get_sysvar_unchecked(var_addr, sysvar_id_ptr, 0, length as u64)?;
198-
var_addr.add(length).write_bytes(0, $padding);
199-
// SAFETY: All bytes now initialized: syscall filled data
200-
// bytes and we zeroed padding.
201-
Ok(var.assume_init())
197+
let result =
198+
unsafe { $crate::get_sysvar_unchecked(var_addr, sysvar_id_ptr, 0, length as u64) };
199+
match result {
200+
Ok(()) => {
201+
// SAFETY: All bytes now initialized: syscall filled data
202+
// bytes and we zeroed padding.
203+
unsafe {
204+
var_addr.add(length).write_bytes(0, $padding);
205+
Ok(var.assume_init())
206+
}
207+
}
208+
// Unexpected errors are folded into `UnsupportedSysvar`.
209+
Err(_) => Err($crate::__private::ProgramError::UnsupportedSysvar),
202210
}
203211
}
204212
};

0 commit comments

Comments
 (0)