Skip to content

Two SAFETY comments justifying from_utf8_unchecked cite parser.rs:133, which is a Method match arm, not hostname_and_port #1473

Closed

Description

@FlorentinDUBOIS

Two SAFETY: comments justifying from_utf8_unchecked cite a line that is not the function they name, and the citation CI gate cannot see them because it only reads doc/** and **/LIFECYCLE.md.

The two sites

lib/src/http.rs:849 and lib/src/https.rs:1242 carry the same comment:

// SAFETY: `hostname` was just produced by `hostname_and_port` (see
// `lib/src/protocol/kawa_h1/parser.rs:133`), which only accepts
// bytes matching `is_hostname_char` (alphanumeric, `-`, `.`, plus
// `_` under the tolerant-http1-parser feature). All accepted
// bytes are ASCII (≤ 0x7F), so the slice is valid single-byte UTF-8.

On c9dd662a, parser.rs:133 is:

            Self::Head => "HEAD",

— a match arm inside AsRef<str> for Method. hostname_and_port is at parser.rs:184.

Why this is worth fixing rather than leaving

The argument these comments make is the only thing standing between from_utf8_unchecked and undefined behaviour: the claim is that every byte hostname_and_port accepts is ASCII, so the slice is valid UTF-8. A reader auditing that unsafe block follows the citation to verify the claim, lands on an unrelated Method match arm, and has to go find the real function by hand — at which point the comment has cost them confidence rather than given it.

The substantive claim itself is correct at parser.rs:184; is_hostname_char does restrict to ASCII. Only the pointer is wrong. That is the dangerous shape: a true justification with a broken proof path degrades silently instead of failing, because nothing re-reads it until someone is already suspicious.

Why CI did not catch it

.github/scripts/check_doc_citations.py resolves citations under doc/** and **/LIFECYCLE.md. A file:line citation inside a Rust comment is outside its scope entirely — so these two drifted with no signal, and any future line shift in parser.rs moves them further without a gate firing.

That is the general gap, not specific to these two: git grep -nE '(lib|command|bin)/src/[a-z_/]+\.rs:[0-9]+' -- '*.rs' finds every Rust-comment citation in the tree, and none of them is checked by anything.

The fix

Two parts, and the second is the one that matters:

  1. Repoint both comments at parser.rs:184.
  2. Decide whether Rust-comment citations should be in the gate's scope at all. Extending the resolver to **/*.rs would cover them, but it also widens rule 2 (drift) across the whole codebase, where line shifts are constant and legitimate — so the honest options are to extend the gate, or to stop writing line numbers in Rust comments and cite the symbol by name instead. Citing hostname_and_port without a line number would have been immune to this and costs a reader one git grep.

My inclination is (2)-by-symbol: a line number in a comment is a pointer with no owner, and this issue is what that costs.

Found while reviewing an unrelated change to Method::new (#1451); the branch there shifted these citations two further lines without touching them, which is how the staleness surfaced.

Activity

  1. FlorentinDUBOIS commented on Sep 24, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Closed by #1493 (6172929e).

    Part 1 — repointing the two SAFETY comments — was already done by #1480: both now cite lib/src/protocol/kawa_h1/parser.rs by path with hostname_and_port named in prose, and no line number.

    Part 2 was the part that mattered, and this issue's own inclination is what shipped:

    My inclination is (2)-by-symbol: a line number in a comment is a pointer with no owner, and this issue is what that costs.

    check_doc_citations.py now forbids file.rs:NNN written in a Rust comment rather than resolving it, which avoids widening the drift rule across a codebase where line shifts are constant and legitimate. Verified live on main: --root . exit 0, --self-test exit 0, and the deliberately-broken fixture tree still exits 1 as ci.yml asserts.

    The population was 8, not the 4 a prefix grep finds

    Worth recording, because it is this issue's own thesis recurring. Grepping for (lib|command|bin|e2e)/src/…\.rs:[0-9]+ finds four. Driving the script's own PATH regex finds eight — the extra live one being unbackticked and sibling-relative:

    // Mirror of h1.rs:361-368 for the H2-backend → H2-frontend

    A prefix-keyed extractor walks straight past it. That is the #1459 shape, and it is why the new rule fires through rule 1's own resolve_path (sibling-first) rather than a fresh pattern: the rule and the resolver cannot disagree about which file a citation names.

    Three of the four prefix-visible citations were wrong, not merely stale — lib/src/lib.rs:1006-1010 resolved to pub enum RequiredEvents with the real targets ~220 lines away, and lib/src/https.rs:650-655 resolved to an Arc::new(snapshot) block with the real write-up 367 lines away. All are now symbol citations: Readiness::arm_writable / Readiness::signal_pending_write, HttpsSession::close, and ConnectionH1::readable's close-delimited EOF branch.

    Scope

    Every *.rs in the tree, not a directory list — the defect is a property of the form, not of the citing directory, and a directory list would leave sim/, fuzz/, lib/benches, integration tests and every build.rs free to reintroduce it. That it is not narrowed to nothing is proven rather than asserted: restricting it turns --self-test red on four separate assertions.

    Historical in-comment anchors follow NOT_A_TEST's existing precedent — a table keyed on (citing file, exact citation text) with a stated reason — rather than a new mechanism, and an entry without a reason is documented as an unreviewed silencing.

    doc/** and **/LIFECYCLE.md remain free to carry line numbers deliberately: there, a line number is sometimes the only way to point at a span with no symbol. Auditing those is #1466's remaining ask, which is tracked separately and is not closed by this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions