Skip to content

fix(go): Context copies byte-slice parts - #1022

Merged
coderdan merged 1 commit into
feat/go-term-optionsfrom
fix/go-context-owns-byte-parts
Oct 3, 2026
Merged

coderdan merged 1 commit into
feat/go-term-optionsfrom
fix/go-context-owns-byte-parts

Conversation

@coderdan

@coderdan coderdan commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Stacked on #1019. That PR made ExtendContext copy a byte-slice part so an option held across calls cannot follow later changes to the caller's buffer. The Context type itself had the same gap one layer down: NewContext and Context.With stored a []byte part as the caller's slice. A Context is usually built and used in one call, so this rarely bites, but a context kept and reused would silently bind different bytes if the buffer behind it was rewritten.

Both now go through one helper, so there is a single definition of how the SDK holds a part.

Changes

  • context.go: ownPart copies a byte-slice part and passes every other part through. NewContext and With use it, and the Context doc says the type owns its parts.
  • record.go: ExtendContext uses the same helper instead of its own copy loop.
  • Tests: TestContextOwnsItsByteParts builds a context from two byte buffers, overwrites both, and checks the context still holds the original bytes.

Verification

  • With ownPart temporarily made an identity, both TestContextOwnsItsByteParts and TestExtendContextOwnsItsByteParts fail, naming the mutated bytes; with it restored, both pass.
  • go vet, gofmt, golangci-lint (0 issues) and every stackencrypt test package pass with the built guest.

No changeset: the Go module has no release process yet.

Related

@changeset-bot

changeset-bot Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: ab8ca58

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The ownership fix is focused, consistently applied, and adequately tested.

Review effort: Balanced
Findings: None

What changed in this PR

Ensures Go encryption contexts retain immutable byte-slice parts.

Changes:

  • Adds shared byte-slice ownership logic.
  • Applies it to contexts and context extensions.
  • Tests mutation resistance for root and extended parts.
File Description
languages/​golang/​stackencrypt/​context.go Copies byte-slice context parts.
languages/​golang/​stackencrypt/​record.go Reuses the shared ownership helper.
languages/​golang/​stackencrypt/​unit_test.go Verifies contexts resist caller-buffer mutation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@coderdan
coderdan added this pull request to stack #1024 October 2, 2026 22:02
@coderdan
coderdan marked this pull request as ready for review October 2, 2026 22:03
@coderdan
coderdan requested a review from a team as a code owner October 2, 2026 22:03
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T22:05:33.682090Z 43bdf47 Draft marked ready
🔒 Security Review ✅ Completed 2026-10-02T22:07:20.284571Z 43bdf47 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderdan

coderdan commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto the updated feat/go-term-options (b168206), which renames the option types (Option for every call, RecordOption for record calls only) and makes several ExtendContext options join in order; the rebase applied cleanly and ExtendContext still copies its parts through ownPart.

@freshtonic freshtonic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The change is correct and small. I approve it.

  • ownPart copies a []byte part. It passes all other part types through, and those types are values (string and integers). NewContext and With now use it, so a Context does not alias a buffer of the caller. ExtendContext uses the same helper, so the copy rule has one definition.
  • NewContext and Context.With are the only two places that set Context.node. Thus no other path can store an aliased slice.
  • bytes.Clone keeps a nil slice as a nil []byte, and checkPart runs before the copy. Thus the validation and the type the codec gets do not change.
  • TestContextOwnsItsByteParts overwrites both source buffers after the context is built, and it checks each part. It covers the root path and the With path.

No changeset is necessary: the Go module does not have a release process yet, and no skills/* file documents it.

@coderdan
coderdan force-pushed the fix/go-context-owns-byte-parts branch from f1cdbb8 to 887529d Compare October 2, 2026 23:59
NewContext and Context.With stored a []byte part as the caller's slice, the
same aliasing ExtendContext had one layer up: a buffer reused after the
context was built changed what the context bound. One helper, ownPart,
now copies a byte part for both the Context constructors and the option,
so there is a single definition of what a part the SDK holds looks like.
A test mutates the source buffers after NewContext and With and checks the
context still holds the original bytes.
@coderdan
coderdan force-pushed the fix/go-context-owns-byte-parts branch from 887529d to ab8ca58 Compare October 3, 2026 06:32
@coderdan
coderdan merged commit 39a341f into main Oct 3, 2026
31 checks passed
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.

3 participants