Skip to content

fix: a truncated element no longer spins the reader forever - #78

Closed
PlenoraETL wants to merge 1 commit into
georust:mainfrom
PlenoraETL:fix/truncated-elements-terminate
Closed

PlenoraETL wants to merge 1 commit into
georust:mainfrom
PlenoraETL:fix/truncated-elements-terminate

Conversation

@PlenoraETL

Copy link
Copy Markdown

Six elements hang on main when their end tag never arrives: Point, LineString, LinearRing, Placemark, Data and SchemaData. Each waits for a specific Event::End and ignores everything else, so once the document runs out next_event hands back Event::Eof forever and the loop keeps asking. A 314-byte input found by a fuzzer ran for over a hundred seconds before I stopped it.

// hangs on main
let _ = "<Point>".parse::<Kml>();

Only these six were measured to hang. Other loops in the file have the same shape and no Eof arm either, and they return — I checked all thirty-one elements with a three-second budget each, against both the published crate and main. The missing arm is not by itself the defect, so the others are left alone. read_geom_props covers three of the six, which is why four loops are touched and not six.

The fix rejects with Error::InvalidInput, which already exists — no new variant, no change to the enum. Recovering what was read so far would be a different contract, and that is your choice rather than mine; I have gone for the smaller change and would be glad to switch if you prefer recovery.

One road not taken, since it looks tempting: a flag in next_event that errors on the second Eof is a single-place fix and wrong. read_elements is re-entrant, and a legitimate document consumes more than one Eof — four tests in this suite go red.

Tests

Three. The first asserts the six elements are rejected: if the defect came back it would not fail, it would hang, and CI would time out instead — which is a property of the defect and the reason the list is asserted rather than described in a comment. The second covers the shape the fuzzer produced. The third is the counter-proof that well formed documents still parse, without which a change that rejected everything would pass the other two.

Verified on main: 48 + 4 + 12 tests green, cargo clippy --all-targets -- -D warnings clean, cargo fmt --check clean.

Found by the fuzzing CI of https://github.com/PlenoraETL/plenora-IO-tools

Six elements hang on `main` when their end tag never arrives: `Point`,
`LineString`, `LinearRing`, `Placemark`, `Data` and `SchemaData`. Each waits for
a specific `Event::End` and ignores everything else, so once the document runs
out `next_event` hands back `Event::Eof` forever and the loop keeps asking. A
314-byte input found by a fuzzer ran for over a hundred seconds before I stopped
it.

Only these six were measured to hang. Other loops in the file have the same
shape and no `Eof` arm either, and they return: the missing arm is not by itself
the defect, so they are left alone. `read_geom_props` covers three of the six,
which is why four loops are touched and not six.

The fix rejects with `Error::InvalidInput`, which already exists - no new
variant, no change to the enum. Recovering what was read so far would be a
different contract, and that is the maintainer's choice rather than mine; I have
gone for the smaller change.

A note on one road not taken, since it looks tempting: a flag in `next_event`
that errors on the second `Eof` is a single-place fix and **wrong**.
`read_elements` is re-entrant, and a legitimate document consumes more than one
`Eof` - four tests in this suite go red.

Three tests. The first asserts the six elements are rejected: if the defect came
back it would not fail, it would hang, and CI would time out instead - which is
a property of the defect and the reason the list is asserted rather than
described. The second covers the shape the fuzzer produced. The third is the
counter-proof that well formed documents still parse, without which a change
that rejected everything would pass the other two.

Found by the fuzzing CI of https://github.com/PlenoraETL/plenora-IO-tools
PlenoraETL added a commit to PlenoraETL/plenora-IO-tools that referenced this pull request Sep 10, 2026
Il conteggio diceva «27 allineate su 28» e l'elenco della migrazione ne
descriveva sei. Nessuno dei due era falso da solo, e insieme nascondevano
`shapefile`, indietro di tre minor.

La causa e' strutturale, non una svista: i tre fork governati sono **sia**
dipendenze dirette del workspace **sia** voci della sezione `fork`, e chi
compilava l'elenco ne aveva portata una sola. Un elenco tenuto a mano accanto a
un manifesto diverge; la sola difesa e' che qualcosa li confronti.

`check_dependency_pins.py` lo fa ora, e pretende tre cose senza bisogno di rete:
ogni crate nominato in `fork` compare fra le voci della migrazione; ogni voce e'
una dipendenza diretta vera, perche' l'elenco puo' divergere anche nell'altro
senso; e `allineate + indietro` uguaglia il numero di dipendenze dirette, cosi'
un conteggio non puo' descrivere un insieme diverso da quello che c'e'.

Non pretende invece che **ogni** dipendenza compaia fra le voci: quell'elenco
nomina cio' che ha richiesto lavoro, ed elencarne ventotto lo renderebbe
illeggibile senza dire niente di piu'.

Alla prima corsa ha trovato una seconda lacuna: `dxf`, fork e dipendenza
diretta, fuori dall'elenco per la stessa ragione. Non era indietro -- e' alla
sua ultima release -- quindi non nascondeva niente **oggi**, ma il giorno in cui
pubblichera' una release nuova sarebbe rimasto fuori come lo era `shapefile`.
Una lacuna che non fa danno oggi non e' una lacuna diversa: e' la stessa, in
attesa.

Cinque sonde: le tre direzioni del controllo, il registro reale, e la
controprova che un registro coerente passa -- senza la quale le altre
passerebbero anche se il controllo rifiutasse qualunque cosa.

Registrata anche `georust/kml#78`, inviata il 2026-09-10: sei contributi
inviati, due uniti, quattro aperte.

L1 85/85.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012PRhkFKzTjuqBjaUXCou96

@pjsier pjsier left a comment

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.

Thanks for the PR! And sorry for the delay. I have some small feedback items but otherwise this looks good

Comment thread src/reader.rs

#[cfg(test)]
mod tests {
/// A truncated element must not keep the reader spinning.

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.

Small item, but can we move these to the bottom so that they're not above use super::*?

Comment thread src/reader.rs
Comment on lines +1310 to +1311
/// not go red if the defect came back - it would hang, and CI would time
/// out instead. That is a property of the defect, and the reason the list

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.

To be safe here, can we add a timeout instead so that the failure behavior is a failure and not hanging?

We could add something like this to the tests module and wrap it

    fn rejected_within_timeout(doc: &str) -> Option<bool> {
        let doc = doc.to_owned();
        let (tx, rx) = mpsc::channel();
        thread::spawn(move || {
            // Send only a bool, so Kml/Error don't need to be Send.
            let _ = tx.send(doc.parse::<Kml>().is_err());
        });
        rx.recv_timeout(Duration::from_secs(5)).ok()
    }

@PlenoraETL PlenoraETL closed this Nov 26, 2026
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.

2 participants