Skip to content

Fix initialization from prefilled capture buffers - #63

Open
omrzk wants to merge 2 commits into
rusticata:masterfrom
omrzk:fix/prefilled-reader-57
Open

omrzk wants to merge 2 commits into
rusticata:masterfrom
omrzk:fix/prefilled-reader-57

Conversation

@omrzk

@omrzk omrzk commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

create_reader buffers a complete header before calling from_buffer. Reading again there can fail on a full streaming-decoder buffer or wait for more input while the producer waits for the header to be processed. Reuse the buffered header in both PCAP and PCAPNG readers.

Fixes #57.

The review follow-up reuses the parsed legacy header and validates PCAPNG again only after a refill. It preserves the existing refill/error behavior for malformed prefills. Added controls cover partial legacy headers and variable-size, option-bearing section headers in both byte orders.

Validation of f5f8fc0: all 66 unit/integration tests with all features and all 52 without default features pass; formatting and nightly documentation pass. Independent public-reader controls pass on both the prior submitted version and this follow-up; current upstream still reproduces the redundant-read defect. The original four regressions failed on base a5ca3c6.

Local full test commands still fail two existing doctests. Strict stable check reports nine existing warnings as errors; nightly Clippy reports eleven across each feature configuration. Clean-base comparisons retain those findings. The local Rust 1.65 check stops in dependency resolution (web-sys/js-sys); local semver/external-type checks were not run. The reader fixtures model streaming behavior; no real zstd process or device testing is claimed.

Current-head hosted CI: all three test configurations, formatting, documentation and semver pass. Strict nightly check and Clippy fail on the same existing diagnostics recorded above; the external-type checker fails on a rustdoc JSON format mismatch (55 produced, 57 required). The stable check job was canceled after its check step reported failure; Rust 1.65 was canceled during Cargo update. These results do not establish full green CI.

@chifflier chifflier self-assigned this Nov 20, 2026
Comment thread src/pcap/reader.rs Outdated
Comment on lines +100 to +103
if parse_pcap_header(buffer.data()).is_err() {
let sz = reader.read(buffer.space()).or(Err(PcapError::ReadError))?;
buffer.fill(sz);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I understand the problem, however I am unsure of the solution.
Instead of parsing twice, can't we just check if buffer has PcapHeader::size() (24) bytes for the first call?
I don't think there are cases that could fail on that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in f5f8fc0: from_buffer now keeps the successfully parsed PcapHeader, avoiding the second parse. I kept the parser-based decision rather than a length-only gate so malformed prefills retain the existing refill and read-error precedence. The partial-header/read-count controls and all 66 all-feature unit/integration tests pass; the two existing doctest failures remain.

Comment thread src/pcapng/reader.rs
Comment on lines +126 to +129
if parse_sectionheaderblock(buffer.data()).is_err() {
let sz = reader.read(buffer.space()).or(Err(PcapError::ReadError))?;
buffer.fill(sz);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This double call is hard to avoid, because unlike pcap, the SHB has a variable size here.
Just thinking:
I'm just wondering if there is a way to keep the result and avoid calling the parser twice if it worked.
Or maybe I am over-optimizing ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in f5f8fc0: the second SHB validation now runs only after a refill. A successful initial parse needs no second validation in from_buffer; the block remains buffered for next(). Added controls cover option-bearing variable-size SHBs in both byte orders, including partial prefills and exact read counts. Independent reader/error/lifecycle controls pass on the previous and final versions.

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.

create_reader always returns ReadError if read operation of the underlying reader gives large output

2 participants