Skip to content

packet: set zeroCopy in NewZeroCopyPacketSource - #172

Merged
mosajjal merged 1 commit into
gopacket:masterfrom
hsdfat:zerocopy-packet-source
Oct 1, 2026
Merged

mosajjal merged 1 commit into
gopacket:masterfrom
hsdfat:zerocopy-packet-source

Conversation

@hsdfat

@hsdfat hsdfat commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

NewZeroCopyPacketSource never set the PacketSource zeroCopy field, so the
check in PacketsCtx that rejects a zero copy source combined with the NoCopy
decode option could never fire. This sets the field and adds tests for the
check, using a fake ZeroCopyPacketDataSource.

Behaviour change: a PacketSource created with NewZeroCopyPacketSource and
NoCopy enabled now panics when Packets() or PacketsCtx() is called, as the
check intended. Before, such callers silently got a buffered channel whose
packet data could be overwritten by later reads. NextPacket() is unaffected,
and so are zero copy sources without NoCopy.

Fixes #74

NewZeroCopyPacketSource never set the zeroCopy field, so the check in
PacketsCtx that rejects a zero copy source combined with NoCopy could
never fire. Set the field and add tests for the check.

Fixes gopacket#74
@mosajjal
mosajjal merged commit f8c0188 into gopacket:master Oct 1, 2026
mosajjal added a commit that referenced this pull request Oct 1, 2026
This reverts commit f8c0188.

Setting zeroCopy armed the panic in PacketsCtx, so programs that call
Packets() on a zero copy source with NoCopy set, which ran before,
now crash after upgrading. A patch release must not turn working
code into a panic. The overwrite risk #74 describes stays until a fix
that does not fail at runtime.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mosajjal

mosajjal commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Reverting this. The change is correct and does what the check was meant to do. The problem is what it means for existing users.

A program that creates a source with NewZeroCopyPacketSource, sets NoCopy, and calls Packets() or PacketsCtx() runs today. With this change it panics after a routine upgrade. We don't want an update to turn running code into a crash, even when that code has a latent problem.

The overwrite risk this PR addresses is real, so I've reopened the issue. A fix we'd take is one that never fails at runtime. For example, the channel path could copy each packet's data before decoding when the source is zero copy, so NoCopy can't alias a reused buffer. Thanks for the clear write-up and the tests. They'll be useful for that follow-up.

@hsdfat

hsdfat commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for explaining the compatibility concern. Enabling the panic would break existing channel users during an upgrade. Copying the packet bytes before decoding on the channel path sounds like a better direction, while retaining zero-copy behavior for synchronous reads. A follow-up should test that queued packets remain stable as the source buffer is reused and that existing NoCopy channel callers keep working.

@hsdfat
hsdfat deleted the zerocopy-packet-source branch November 6, 2026 11:28
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.

PacketSource zeroCopy field always false

2 participants