Skip to content

lang: Shorten invariant lifetimes during Context creation - #4363

Merged
jamie-osec merged 8 commits into
otter-sec:masterfrom
acheroncrypto:lang-shorten-invariant-lifetimes-during-context-creation
Jun 2, 2026
Merged

lang: Shorten invariant lifetimes during Context creation#4363
jamie-osec merged 8 commits into
otter-sec:masterfrom
acheroncrypto:lang-shorten-invariant-lifetimes-during-context-creation

Conversation

@acheroncrypto

Copy link
Copy Markdown
Collaborator

Problem

As mentioned in #3340 (comment), the helper function __shrink_lifetime actually extends the mutable borrow lifetime instead of shrinking the inner lifetime:

https://github.com/solana-foundation/anchor/blob/62865c636aecc6974fc9cfebfc6cf08ca4f0bb72/lang/syn/src/codegen/program/handlers.rs#L130-L132

This is not the desired behavior, and it also conflicts with its safety comments:

https://github.com/solana-foundation/anchor/blob/62865c636aecc6974fc9cfebfc6cf08ca4f0bb72/lang/syn/src/codegen/program/handlers.rs#L138-L142

Summary of changes

Shorten invariant lifetimes rather than extending the mutable borrow lifetime during Context creation.

@vercel

vercel Bot commented Mar 29, 2026

Copy link
Copy Markdown

@acheroncrypto is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@acheroncrypto acheroncrypto added lang fix Bug fix PR labels Mar 29, 2026
@jamie-osec

Copy link
Copy Markdown
Collaborator

Just bumping on this, are you able to fix the compile errors?

@acheroncrypto

Copy link
Copy Markdown
Collaborator Author

There seems to be only one compile error coming from that cursed generics accounts struct. It should be fixable. I'll take a look later this week.

@acheroncrypto
acheroncrypto marked this pull request as ready for review May 18, 2026 03:35
@acheroncrypto

Copy link
Copy Markdown
Collaborator Author

Sorry for the delay. Should be fixed now.

@acheroncrypto
acheroncrypto requested a review from jamie-osec May 18, 2026 03:37
@acheroncrypto

Copy link
Copy Markdown
Collaborator Author

@jamie-osec bump

0x4ka5h
0x4ka5h previously requested changes Jun 2, 2026

@0x4ka5h 0x4ka5h 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.

Comment thread lang/syn/src/codegen/accounts/__shorten_invariant_lifetime.rs
Comment thread lang/syn/src/codegen/accounts/__shorten_invariant_lifetime.rs
@jamie-osec

Copy link
Copy Markdown
Collaborator

I don't feel strongly about the concerns raised so I think we can merge if you disagree; it's just that we'd be adding unnecessary (if theoretical) breakage

@jamie-osec
jamie-osec dismissed 0x4ka5h’s stale review June 2, 2026 12:39

Addressed in discussion

@acheroncrypto

Copy link
Copy Markdown
Collaborator Author

I think this is an important fix because we're actually extending the borrow lifetime instead of shortening it. This is not only incorrect based on what it says it does, but it might even be bordering the UB territory. I think a theoretical breakage is justified in a situation like this.

However, feel free to cover those other generic cases if you think they could be problematic.

@jamie-osec jamie-osec 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.

We can revisit in the unlikely case this breaks anyone

@jamie-osec
jamie-osec merged commit 31967ec into otter-sec:master Jun 2, 2026
165 of 167 checks passed
@jamie-osec jamie-osec added this to the v1.2.0 milestone Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix PR lang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants