fix(basics/close-account): reserve the 8-byte discriminator in create_user - #669
Open
moviendome wants to merge 1 commit into
Open
Conversation
…_user Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Greptile SummaryThis PR corrects the Anchor
Confidence Score: 5/5The PR appears safe to merge. The allocation now covers both Anchor's account discriminator and the full generated Important Files Changed
Reviews (1): Last reviewed commit: "fix(basics/close-account): reserve the 8..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
create_user.rssizes the new account withspace = UserState::INIT_SPACE,which does not include Anchor's 8-byte account discriminator.
InitSpacemeasures the struct's own fields and stops there. ForUserState:The account therefore gets 87 bytes, but a serialized
UserStateneeds8 + 4 + name.len() + 33. Names up to 42 bytes fit inside 87. Names of 43 to50 bytes — all of which
#[max_len(50)]promises to support — overflow theallocation and the instruction fails with
AccountDidNotSerialize.The test suite passes because it only ever writes
"John Doe", which is 8 bytes.Every other Anchor example under
basics/already accounts for thediscriminator, so this also brings
close-accountback in line with the restof the repository:
ANCHOR_DISCRIMINATOR_SIZE + AddressInfo::INIT_SPACEANCHOR_DISCRIMINATOR_SIZE + Favorites::INIT_SPACE8 + Counter::INIT_SPACE8 + PageVisits::INIT_SPACEThe change
basics/close-account/anchor/programs/close-account/src/instructions/create_user.rsVerifying
Changing the test to create a user with a 50-byte name fails before this
change and passes after it.
The
nativeimplementation is unaffected: it sizes the account withborsh::to_vec(&data)?.len(), and native accounts carry no discriminator.