From bf7c6593ae43b3b7c4eccbd0bf7518b657be8f2e Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Thu, 14 May 2026 01:59:11 -0300 Subject: [PATCH] fix(oracle): preallocate price account serialization Use borsh::object_length to reserve the exact serialized size for OraclePriceAccount data without relying on size_of_val. Add coverage for string-backed source identifiers. --- oracle/core/src/lib.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/oracle/core/src/lib.rs b/oracle/core/src/lib.rs index 13478cf..4daa02c 100644 --- a/oracle/core/src/lib.rs +++ b/oracle/core/src/lib.rs @@ -173,7 +173,9 @@ impl TryFrom<&Data> for OraclePriceAccount { impl From<&OraclePriceAccount> for Data { fn from(price_account: &OraclePriceAccount) -> Self { - let mut data = Vec::new(); + let serialized_len = + borsh::object_length(price_account).expect("Oracle price account length must be known"); + let mut data = Vec::with_capacity(serialized_len); BorshSerialize::serialize(price_account, &mut data) .expect("Serialization to Vec should not fail"); Self::try_from(data).expect("Oracle price account encoded data should fit into Data") @@ -292,6 +294,19 @@ mod tests { ); } + #[test] + fn oracle_price_account_data_matches_borsh_object_length() { + let mut oracle_account = price_account(1_050_000, 1_000); + oracle_account.source_identifier = "mock-oracle-with-long-source-name".to_owned(); + + let data = Data::from(&oracle_account); + + assert_eq!( + data.as_ref().len(), + borsh::object_length(&oracle_account).expect("object length should be known") + ); + } + #[test] fn account_price_feed_rejects_wrong_feed_account() { let oracle_account = price_account(1_050_000, 1_000);