solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.5k stars 2.04k forks source link

get_packed_len is dangerously buggy #1626

Closed dzmitry-lahoda closed 3 years ago

dzmitry-lahoda commented 3 years ago
/// Get the worst-case packed length for the given BorshSchema
pub fn get_packed_len<S: BorshSchema>() -> usize {

get_packed_len returns 127, while doing real serde gives 135 for:

#[repr(C)]
#[derive(Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub enum StateVersion {
    Uninitialized,
    V1,
}

impl Default for StateVersion {
    fn default() -> Self {
        StateVersion::Uninitialized
    }
}

#[repr(C)]
#[derive(Debug, BorshDeserialize, BorshSerialize, BorshSchema, Default)]
pub struct StakingPool {
    pub version: StateVersion,
    pub token_account_sos: Pubkey,
    /// Account accumulating staked SOS tokens
    /// Mint issuing pool tokens to the users (xSOS)
    /// TODO: is that xSOS = SOS one to one and we need other just to have them program controllerd?
    pub pool_mint_xsos: Pubkey,
    /// Number of tier users
    pub tier_users: u32,
    pub tier_users1: u32,
    pub tier_users2: u32,
    pub tier_users3: u32,
    /// Balance qualifying to each of the tiers (in ascending order)
    pub tier_balance1: u64, //[u64; 4],
    pub tier_balance2:  u64, //`[u64; 4],
    pub tier_balance3:  u64, //`[u64; 4],
    pub tier_balance4:  u64, //`[u64; 4],

    /// Number of slots SOS tokens are stuck in transit
    pub transit_interval: u64,

    pub dummy:u64,
    pub dummy0:u8,
    pub dummy1:u8,
    pub dummy2:u8,
    pub dummy3:u8,
    pub dummy4:u8,
    pub dummy5:u8,
}
mvines commented 3 years ago

Yikes, thanks!

Pretty sure it's this: https://github.com/solana-labs/solana-program-library/blob/2b3f71ead5b81f4ea4a2fd3e4fe9583a6e39b6a4/feature-proposal/program/src/borsh_utils.rs#L42 If you change that to 4, I expect you'll get the same result

mvines commented 3 years ago

This'll ship in the next Solana 1.6 release. Thanks again for the report