Skip to content

fix(data): exports say text that a NUL, a CSV apostrophe or a blank KML field name loses on reading back (#2415) - #2749

Merged
Mavrick-F merged 2 commits into
opengeos:mainfrom
bearinmindcat:fix/2415-export-control-chars-nul
Nov 23, 2026
Merged

Mavrick-F merged 2 commits into
opengeos:mainfrom
bearinmindcat:fix/2415-export-control-chars-nul

Conversation

@bearinmindcat

@bearinmindcat bearinmindcat commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

#2415 lists exports that change text with no notice. Some are already handled on main:

This PR covers the three that remain. Each gets a counted export notice that names its fields, like #1907's notices:

  • NUL in Shapefile text: reading a .dbf keeps only the text before a NUL, so "ab\0cd" comes back as "ab" and "\0x" as null. TextLosses counts text with a NUL in the 254 bytes a Character column keeps, under its dBase read: "t: 2 text values hold a NUL character, and reading the file back keeps only the text before it, read as empty (null) when that is blank, in 'a'". The text kept before a NUL loses its trailing spaces as other dBase text does, which is counted as such.
  • A leading apostrophe in CSV: text such as '=x isn't guarded on write, since it already starts with '. Reading takes the ' for the guard and drops it, so it comes back as =x. TextLosses counts text that starts with ' followed by a formula's first character (the same set unguard checks), under its CSV read. That covers Data Interop's WKT CSV as well: "t: 2 text values start with an apostrophe before =, +, -, @, a tab or a line break, which reading the file back takes for the formula guard and drops, in 'note', 'code'". There's no way to write such text that today's reader takes back exactly, so the loss is reported rather than changed.
  • A KML field with a blank name: a field whose name is blank once trimmed is written as <Data name="">. Reading drops every such entry, so the column and all its values are gone. The new kml_blank_names_note, beside kml_empty_fields_note, counts the fields and their written values: "t: 2 fields with a blank name, and 3 values in them, won't read back: reading KML drops extended data without a name". A blank-named field with no values written is already reported as a field with no values, so it isn't counted again.

A streamed CSV export adds up the new counts a chunk at a time, like the others (TextLosses += …).

Reach every surface

  • Desktop and browser: every export to CSV, WKT CSV, Shapefile, KML and KMZ goes through export_losses_notes or the streamed TextLosses, so the notices appear wherever an export's notices are shown.
  • Command line, Python, Assistant and MCP: the same exports, with the same notices in their results. No parameter changes.
  • Phone, ribbon, command search and plugins: nothing new.
  • The way back: nothing is changed in what's written; the export only says more.

Testing

  • Reproduced on main (9cc7d88), with only the new tests added: all three fail.
    • shapefile_says_text_cut_at_a_nul: no notice (left: []).
    • csv_says_an_apostrophe_reading_takes_for_the_guard: no notice (left: []).
    • kml_says_fields_with_a_blank_name: no notice of the blank names. The only notice is "KML omitted 1 field with no values … (' ')", for the blank-named field that has no values.
  • With the fix:
    • cargo test -p rustgis-data --lib: 1749 passed and 3 failed. The failures are oversized_shapefile_part_reports_whole_dataset_advice, oversized_geojson_publish_gives_vector_advice and oversized_shapefile_dbf_read_for_publish_gives_dataset_advice. Each builds its expected size limit from max_bytes(), which follows the memory free at the moment it's called, and another build was running on the machine. Run alone, cargo test -p rustgis-data --lib -- oversized passed all 11, and this change doesn't touch them.
    • cargo test -p rustgis-data --test integration: 70 passed.
    • cargo fmt --all --check: clean.
    • cargo clippy -p rustgis-data --all-targets -- -D warnings: clean on stable and on 1.100.0, the CI toolchain.
  • New tests. Each writes the file and reads it back, so a test checks that the notice describes what reading actually does:
    • shapefile_says_text_cut_at_a_nul: "ab\0cd" and "\0x" give one notice counting 2 in 'a'. Read back they're "ab" and null. NUL-free text says nothing.
    • csv_says_an_apostrophe_reading_takes_for_the_guard: '=x and '-5 give one notice counting 2 in 'note' and 'code', for CSV and WKT CSV alike. Read back they're =x and -5, while 'kept and the writer-guarded =y come back as they were. Clean text ('kept, =y, it's) says nothing.
    • kml_says_fields_with_a_blank_name: fields "" and " " with 3 values give one notice. Read back, no column has a blank name. A blank-named field with no values gets no notice from this check.

Review round (c666d0a)

  • Text before a NUL with spaces after it (bot review): "ab cd" reads back as "ab", losing both the text after the NUL and the space before it, but only the NUL was counted. The NUL is now counted apart, and the kept text is judged as other dBase text. Its trailing spaces are counted, and a blank one, read as null, is covered by the NUL's notice. shapefile_says_text_cut_at_a_nul now writes "ab cd" and " y" too. On the previous code it failed, giving only the NUL notice. With the fix it passes, reading back "ab" and null.
  • The KML blank-name check: it returns at once without a blank-named field, counts in one pass, and looks at Cancel.
  • Chunks: text_losses_of_chunks_add_up_to_the_whole_layer covers the NUL and apostrophe counts.
  • Checked: the data lib tests, the 70 integration tests, and fmt and clippy (stable and 1.100.0) are clean. The exception is oversized_*, which failed both alone and in the full run while another build ran on this machine. Their limit follows free memory, and below the full limit too_large_held ([Bug]: Undo steps on a large layer each hold a full copy of its rows, so memory grows with every edit #2431) words the error differently, so it doesn't parse as they expect. This change doesn't touch them, and CI ran them.

UI evidence

No UI change. The notices appear in an export's existing list of notices.

Related issue

Part of #2415. Its parts handled before this PR are listed above.

Summary by CodeRabbit

  • New Features
    • Export-loss reporting now flags dBASE text containing a NUL within the first 254 bytes and CSV/WKT text beginning with an apostrophe before a formula character, with notices describing the effects when read back.
    • KML and KMZ export-loss notices now identify blank-named fields with non-null values on features with geometry. Counts for these notices accumulate across streamed data.

…ML field name loses on reading back (opengeos#2415)

Three exports changed text with nothing said:
- Shapefile: reading a .dbf keeps only the text before a NUL, so "ab\0cd"
  reads back as "ab" and "\0x" as null. TextLosses counts text with a NUL
  in the 254 bytes a Character column keeps.
- CSV and WKT CSV: text such as '=x isn't guarded on write, as it already
  starts with an apostrophe, and reading takes the apostrophe for the
  guard and drops it. TextLosses counts text starting with an apostrophe
  before a formula's first character.
- KML: a field whose name is blank is written as <Data name="">, which
  reading drops, column and values. kml_blank_names_note counts such
  fields and their written values.

Each is said with a count and the fields, as opengeos#1907's notices are; a
streamed CSV export adds the counts up a chunk at a time.
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Export loss reporting now identifies additional text and KML values that may not be preserved on read-back. Text loss counts accumulate across streamed chunks. Tests cover the new notices and read-back behavior.

Changes: Export loss notices

Layer / File(s) Summary
Count and report export losses
crates/data/src/io.rs
Text loss tracking and notes cover dBASE text containing NULs and CSV/WKT text with an apostrophe before a formula character. Counts accumulate across chunks. KML and KMZ notes report blank-named fields with non-null values on features with geometry. Tests cover text read-back behavior and KML notices.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Possibly related issues

Possibly related PRs

  • opengeos/RustGIS#2414: This PR deferred Shapefile NUL truncation, blank KML names, and leading CSV apostrophes to this work.
  • opengeos/RustGIS#2413: This PR adds the CSV apostrophe guard whose removal this change counts and reports.

Suggested reviewers: mavrick-f

Merge Risk: Minimal · up to c666d

This adds notices telling users when exported text may change on read-back (NUL characters in Shapefile text, leading apostrophes in CSV, and blank-named KML fields). Exported content is unchanged and no merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the three changes accurately: NUL characters in Shapefile text, leading CSV apostrophes, and blank KML field names, all of which get export notices. It also follows the fix(data) conve…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is thorough and closely follows the template's intent. It covers the summary (the three remaining export-loss cases and why each needs a counted notice), testing (reproduction on main,…
Full details: Title check

Explanation

The title names the three changes accurately: NUL characters in Shapefile text, leading CSV apostrophes, and blank KML field names, all of which get export notices. It also follows the fix(data) convention. It is long (112 characters, 21 words), but it still matches the main change in the changeset.

Full details: Description check

Explanation

The description is thorough and closely follows the template's intent. It covers the summary (the three remaining export-loss cases and why each needs a counted notice), testing (reproduction on main, new tests, fmt/clippy results, and the three unrelated oversized_* failures with the reason), and the related issue (Part of #2415). The UI evidence section states no UI change, which matches the template's 'No visible UI change.' line. The only gap is the template's placeholder comments were replaced with prose, which is acceptable. Nothing required is missing.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the fields at night
And counts the text that won’t read right
A NUL, an apostrophe in view
Blank KML names are counted too
Across the chunks, the tallies stay
Then hops contentedly away

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: b94bdb10-3e68-4b31-b4f8-46dac81283ab
Commits

Reviewing files that changed from the base of the PR and between 0c5ce67 and c666d0a.

Files selected for processing: 1

  • crates/data/src/io.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread crates/data/src/io.rs Outdated
TextRead::Cell if s.is_empty() => &mut loss.blank,
TextRead::Cell | TextRead::Dbf if Value::is_null_marker(s) => &mut loss.markers,
// In the 254 bytes a Character column keeps.
TextRead::Dbf if s.bytes().take(254).any(|b| b == 0) => &mut loss.nul,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The NUL arm sits in the same match as the Dbf padding and blank arms, so a value that has both a NUL and spaces before it is counted only as nul. For example, "ab \0cd" reads back as "ab": the NUL cut and the trailing space drop both happen, but only the NUL is said. The space loss goes unreported, which is the silent-loss case AGENTS.md forbids. This is an edge case and I'm not certain how common it is in real dBase files (confidence about 50%), but the fix is small: count the NUL with its own if before the match, so the padding and blank arms still run for the same value.

Category: bug

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.

Fixed in c666d0a. The NUL is now counted apart, before the dBase arms. The text reading keeps before it is then judged as other dBase text, so its trailing spaces are counted as such. A blank one, which reads as null, is left to the NUL's notice, which now says so: "…keeps only the text before it, read as empty (null) when that is blank". So "ab \0cd" gives both notices.

How it was checked: shapefile_says_text_cut_at_a_nul now also writes "ab \0cd" and " \0y", and reads the file back.

  • On the PR's previous code it fails: only the NUL notice, without "1 text value ends with spaces".
  • With the fix it passes, and reading back gives "ab", null and "ab" in 'a', and null for " \0y".
  • Also clean: the data crate's lib tests (apart from the oversized_* tests, which compare a limit that follows free memory and failed while another build ran on this machine; this change doesn't touch them), its 70 integration tests, and clippy on stable and 1.100.0.

Comment thread crates/data/src/io.rs Outdated
/// (#2415). A field with no value written is said by [`kml_empty_fields_note`].
fn kml_blank_names_note(ds: &Dataset, target: &str) -> Option<String> {
let blank: Vec<usize> = ds.fields.iter().enumerate().filter(|(_, f)| f.name.trim().is_empty()).map(|(k, _)| k).collect();
let values: usize = ds

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does up to three passes over every feature, and the first runs even when there are no blank-named fields. Two costs follow. For a national KML layer with no blank names, it adds a full scan for nothing. Also, with_values is blank.len() × features in the worst case, because the any scans a whole layer for each blank field with no non-null value. Two suggestions: return early when blank is empty, and compute the per-field has_value flags and the total value count in one pass. The new loops also don't call progress::check(); the preceding kml_empty_fields_note pass does, so Cancel still works in practice, but the doc comment should say so.

Category: performance

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.

Fixed in c666d0a:

  • kml_blank_names_note returns at once when no field has a blank name, so a layer without one costs no scan.
  • Otherwise it counts each blank-named field's values, and whether it has any, in one pass over the placemarks written.
  • It calls progress::check() per placemark, as kml_empty_fields_note does, and returns Result, so Cancel stops it too.

kml_says_fields_with_a_blank_name still passes with the same counts: 2 fields and 3 values, with no notice for a blank-named field with no values.

Comment thread crates/data/src/io.rs
a.padded += b.padded;
a.name_padded += b.name_padded;
a.nul += b.nul;
a.apostrophe += b.apostrophe;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new nul and apostrophe += lines are not exercised by the chunked-equality test (text_losses_of_chunks_add_up_to_the_whole_layer, around line 7655), whose data has no NUL or apostrophe values. A missing or wrong += for either counter would pass that test. Adding a '=x value and a NUL-containing Shapefile value to a chunked case would cover them.

Category: quality

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.

Covered in c666d0a. text_losses_of_chunks_add_up_to_the_whole_layer now also adds up two layers in chunks of every size, checking that the sum equals the whole and that its notes say the loss:

  • a CSV layer with '=x and '+z;
  • a Shapefile layer with "a\0b" and "ab \0c".

A missing or wrong += for apostrophe or nul now fails it.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I reviewed the diff against crates/data/src/io.rs, the readers it describes (unguard, the KML writer and reader, the dBase reading path), and the new tests. I could not compile or run the tests here, so the findings come from reading the code.

No prior review threads exist on this PR, so no earlier concerns were left alone.

Bugs

  • Padded text before a NUL is not reported (confidence about 50%). The NUL arm shares a match with the dBase padding arm, so "ab \0cd" is counted only as nul. Reading drops the trailing space as well, and that loss goes unsaid. Counting the NUL separately would fix it. Posted inline.

Security

  • Nothing found.

Performance

  • kml_blank_names_note does extra full passes (confidence about 75%). It scans every feature even when there are no blank-named fields. with_values is O(blank fields × features) in the worst case. The new loops also have no progress::check(), though the preceding kml_empty_fields_note pass does. Posted inline.

Quality

  • The chunked-equality test does not cover the new counters (confidence about 90%). Its data has no NUL or apostrophe values, so a missing += for nul or apostrophe would pass. Posted inline.
  • Checked and fine: the blank-name note matches the writer (to_kml writes fd.name raw) and the reader's trim-and-drop (kml.rs:515). The apostrophe count matches unguard, which reads the raw cell.

AGENTS.md

  • Nothing that breaks a rule. The three loops are not cancellable on their own, but the preceding pass covers Cancel in practice. This is folded into the performance comment.

Change size

Compared with main (merge base 9cc7d88 to PR head 0c5ce67).

Measure Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 486.3 KB
Size after 488.7 KB
Total size change +6.8 KB
Path Before After Change
crates/data/src/io.rs 486.3 KB 488.7 KB +6.8 KB

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Commit c666d0a from a fork built, but isn't published until a maintainer adds the preview label (once per commit).

…file text does, and is said so (opengeos#2415)

Review on opengeos#2749:
- "ab \0cd" reads back as "ab": the NUL cut and the space before it both
  go, but only the NUL was counted. The NUL is now counted apart, and the
  text reading keeps before it is judged as other dBase text: its trailing
  spaces counted, a blank one (read as null) left to the NUL's notice,
  which says so.
- kml_blank_names_note returns at once without a blank-named field, and
  counts the fields and values in one pass, looking at Cancel.
- The chunked TextLosses test covers the NUL and apostrophe counts.
Comment thread crates/data/src/io.rs
}

/// KML fields whose name is blank once trimmed, with values in the placemarks written: each is
/// written as `<Data name="">`, which reading drops, so the column and its values are gone

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The doc says each blank-named field is written as <Data name="">, but the writer uses the field's name as it is (interop.rs, the <Data name=…> line). A name of spaces or a tab is written as <Data name=" "> or <Data name="">. The reader drops all of them because it trims the name, so the behaviour is right and only the example is off. Suggest saying so:

/// KML fields whose name is blank once trimmed, with values in the placemarks written: each is
/// written with its name as it is (`<Data name="">`, or spaces or a tab), which reading drops, so
/// the column and its values are gone (#2415). A field with no value written is said by
/// [`kml_empty_fields_note`].

Category: quality

Comment thread crates/data/src/io.rs
let ds = text_layer(&["note", "code"], &[&[Some("'=x"), Some("a")], &[Some("'kept"), Some("'-5")], &[Some("=y"), Some("b")]]);
let said = "t: 2 text values start with an apostrophe before =, +, -, @, a tab or a line break, which reading the file back takes for the formula guard and drops, in 'note', 'code'";
assert_eq!(text_notes(&ds, "csv"), [said]);
assert_eq!(text_notes(&ds, "wkt"), [said]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The PR says every new test writes the file and reads it back, so the notice describes what reading does. This one checks only the WKT notes. The WKT CSV path goes through the same CSV reader, which applies unguard, so the claim looks right, but nothing here shows it. Suggest reading the WKT file back too and checking that note comes back as =x, 'kept and =y, as the CSV read-back above does.

Category: quality

@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs

None confirmed. I checked the new NUL, apostrophe and KML blank-name counts against the reader and writer code, and each count matches what the read-back does:

  • The NUL cut happens before the dBase padding and blank arms, so "ab \0cd" gets both notices. The dBase reader decodes character values only up to a NUL (dbf.rs).
  • The apostrophe arm matches unguard exactly: it uses the raw cell, not the trimmed one. The ''=x case reads back unchanged and isn't flagged.
  • The KML blank-name count uses the same filter as the writer (geometry.is_some()), and the reader drops names that are blank once trimmed (kml.rs).

Confidence: about 85%. I didn't run the tests; the PR reports them passing.

Security

None found.

Performance

None new. The earlier concern about extra passes is fixed: kml_blank_names_note returns early when no field is blank, and it checks Cancel per placemark.

Quality

  • io.rs:5077 (inline): the doc says blank names are written as <Data name="">, but the writer writes the name as it is, so spaces or a tab appear in the output. The behaviour is right; only the wording is off. Confidence: high.
  • io.rs:7386 (inline): the WKT notice is checked only by its text. There's no read-back, though the PR says every new test reads the file back. The shared CSV reader should make the claim true, so this is a coverage gap. Confidence: medium-high.
  • Not posted, minor: the apostrophe note says "a tab or a line break", but FORMULA_START has only \r (not \n). The wording is loose but harmless.
  • Not posted, minor: the apostrophe arm repeats the unguard predicate. A shared helper would keep the two in step.

AGENTS.md

No breaches found. Each new notice is counted with a test both ways (lossy and clean). No new layer copy is added, and the new KML pass checks Cancel. The rows and layering rules don't apply to this file.

Prior reviews

I left 3 earlier concerns alone, because the author fixed each in c666d0a and I confirmed the fixes in the code:

  • The chunked-sum test not covering nul and apostrophe.
  • The NUL arm hiding the trailing-space loss.
  • The extra scans in the KML note.

I posted 2 new inline comments, both quality.

Change size

Compared: the whole PR against main (merge base 9cc7d88 to PR head c666d0a).

Files changed Added Modified Removed Renamed
1 0 1 0 0
Size before Size after Total change
486.3 KB 495.3 KB +9.0 KB
Path Before After Change
crates/data/src/io.rs 486.3 KB 495.3 KB +9.0 KB

@Mavrick-F
Mavrick-F added this pull request to the merge queue Nov 23, 2026
Merged via the queue into opengeos:main with commit 540735c Nov 23, 2026
29 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.

2 participants