coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

FR: Throw error if #[max_len] is violated #3063

Open jacksondoherty opened 2 days ago

jacksondoherty commented 2 days ago

Ex. This will not throw an error if name is longer than 32 chars.

#[account]
#[derive(Default, Debug, InitSpace)]
pub struct Data {
    pub admin: Pubkey,
    #[max_len(32)]
    pub name: String,
}

pub fn handle_init(ctx: Context<Init>, data: Data) -> Result<()> {
    Ok(())
}
acheroncrypto commented 2 days ago

Hey, thank you for creating this issue!

#[max_len] only applies to the InitSpace macro, which is pretty much exclusively used for initializing accounts. It's not being used after the account is created, and changing that behavior risks breaking people's existing logic. Thus, I'm not in favor of this feature at this time. However, we might be able to add a new macro/impl that achieves what you described instead.