Skip to content

Refactor/max length encoding#43

Merged
loks0n merged 3 commits intomainfrom
refactor/max-length-encoding
Apr 18, 2026
Merged

Refactor/max length encoding#43
loks0n merged 3 commits intomainfrom
refactor/max-length-encoding

Conversation

@loks0n
Copy link
Copy Markdown
Contributor

@loks0n loks0n commented Apr 18, 2026

No description provided.

@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 18, 2026

Greptile Summary

This PR replaces the two-pass encode/truncation design (build full packet, then re-encode with encodeWithTruncation) with a single-pass approach: answers are appended one at a time with an inline size guard, authority and additional are tried all-or-nothing, and a header-rebuild path at the end patches counts, TC, and AA only when something was actually dropped. The three issues raised in previous review threads — TC=1 being silently cleared on re-encode, pre-encoded-but-discarded records on the size-check boundary, and the SOA validation firing on truncated inbound messages — are all addressed and backed by new tests.

Confidence Score: 5/5

Safe to merge — logic is correct, edge cases are well-covered by the new tests, and all three previously flagged concerns are resolved.

No P0 or P1 findings. The single-pass approach is cleaner and more efficient than the original two-pass design. TC preservation, AA clearing for NODATA/NXDOMAIN authority drops, and the SOA-validation TC bypass are all implemented correctly and covered by dedicated tests.

No files require special attention.

Important Files Changed

Filename Overview
src/DNS/Message.php Refactors encode() to inline truncation logic: answers loop with per-record size check, all-or-nothing authority/additional blocks, and a header-rebuild path that preserves inbound TC=1 and clears AA on NODATA/NXDOMAIN when authority is dropped. Also adds !$header->truncated guard to SOA validation.
tests/unit/DNS/MessageTest.php Adds eight new test cases covering NODATA AA-clearing, authority-overflow cascading to additional, exact-boundary encoding, TC=1 preservation under maxSize re-encoding, extreme answer truncation AA survival, and NODATA authority drop without TC. All three previously flagged scenarios now have explicit tests.

Reviews (4): Last reviewed commit: "Preserve inbound TC=1 flag when re-encod..." | Re-trigger Greptile

Comment thread src/DNS/Message.php
Comment thread src/DNS/Message.php
Collapse encode() and encodeWithTruncation() into a single linear pass
that streams answers with partial truncation, then attempts authority
and additional all-or-nothing, and only rebuilds the header when counts
or the TC flag actually change. Add tests covering section priority,
answer-truncation clearing populated non-answer sections, TC preservation
on re-encode, the exact maxSize boundary, and NODATA-style drops.
@loks0n loks0n force-pushed the refactor/max-length-encoding branch from f2d932e to 4e72b66 Compare April 18, 2026 20:13
Check the original message's answer set (not the post-encoding count)
when deciding whether to clear AA — a TC=1 response that merely
encoded zero answers isn't semantically NODATA, and TC already signals
the client to retry over TCP for the full authoritative answer. Relax
the Message constructor's SOA-in-authority invariant for truncated
responses so those packets remain decodable.
Comment thread src/DNS/Message.php Outdated
When a source message already has truncated=true (e.g. a forwarded
packet) and encode() drops the additional or authority section under
maxSize, the rebuilt header must keep TC=1 so downstream clients still
know to retry over TCP. OR the original flag into the rebuilt header's
truncated bit instead of always using the locally-computed value.
@loks0n loks0n merged commit fb2a570 into main Apr 18, 2026
5 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.

1 participant