Skip to content

fix(basics/close-account): reserve the 8-byte discriminator in create_user - #669

Open
moviendome wants to merge 1 commit into
solana-foundation:mainfrom
moviendome:fix/close-account-discriminator-space
Open

fix(basics/close-account): reserve the 8-byte discriminator in create_user#669
moviendome wants to merge 1 commit into
solana-foundation:mainfrom
moviendome:fix/close-account-discriminator-space

Conversation

@moviendome

Copy link
Copy Markdown

create_user.rs sizes the new account with space = UserState::INIT_SPACE,
which does not include Anchor's 8-byte account discriminator.

InitSpace measures the struct's own fields and stops there. For UserState:

bump: u8                   1
user: Pubkey              32
#[max_len(50)] name       4 + 50
                        ----
                          87

The account therefore gets 87 bytes, but a serialized UserState needs
8 + 4 + name.len() + 33. Names up to 42 bytes fit inside 87. Names of 43 to
50 bytes — all of which #[max_len(50)] promises to support — overflow the
allocation 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 the
discriminator, so this also brings close-account back in line with the rest
of the repository:

  • account-data ANCHOR_DISCRIMINATOR_SIZE + AddressInfo::INIT_SPACE
  • favorites ANCHOR_DISCRIMINATOR_SIZE + Favorites::INIT_SPACE
  • counter 8 + Counter::INIT_SPACE
  • program-derived-addresses 8 + PageVisits::INIT_SPACE

The change

basics/close-account/anchor/programs/close-account/src/instructions/create_user.rs

-        space = UserState::INIT_SPACE,
+        space = 8 + UserState::INIT_SPACE,

Verifying

Changing the test to create a user with a 50-byte name fails before this
change and passes after it.

The native implementation is unaffected: it sizes the account with
borsh::to_vec(&data)?.len(), and native accounts carry no discriminator.

…_user

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@moviendome
moviendome requested a review from dev-jodee as a code owner August 5, 2026 04:57
@greptile-apps

greptile-apps Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR corrects the Anchor UserState account allocation so it includes the required 8-byte account discriminator.

  • Changes create_user to allocate 8 + UserState::INIT_SPACE.
  • Allows names up to the declared 50-byte maximum to serialize successfully.

Confidence Score: 5/5

The PR appears safe to merge.

The allocation now covers both Anchor's account discriminator and the full generated UserState size, with no conflicting behavior introduced.

Important Files Changed

Filename Overview
basics/close-account/anchor/programs/close-account/src/instructions/create_user.rs Correctly reserves Anchor's 8-byte discriminator in addition to the generated state size.

Reviews (1): Last reviewed commit: "fix(basics/close-account): reserve the 8..." | Re-trigger Greptile

@dev-jodee dev-jodee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signed commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants