Repository navigation
fix(data): a .dbf in a declared multibyte code page says how much of its text isn't valid in it (#2614) - #2673
Conversation
…+FFFD (opengeos#2614) A .dbf in Shift_JIS, GBK, EUC-KR, Big5 or another code page encoding_rs decodes was read with U+FFFD in place of bytes that aren't valid in it, with no notice: opengeos#2325 counted only text read as UTF-8. Names and values are now counted with the declared decoder, on the whole read and on the read in place, and the notice names the code page the .cpg or the .dbf's language driver declares.
…os#2614) The streamed-shapefile engine test gains a Big5 case (language driver 0x4F) whose view read, export and conversion notices must name Big5, so Exported.not_utf8_in is tested, and a valid Big5 case that says nothing. A lead byte filling its field is also checked through a standalone .dbf and a read in place. The docs name the right test file.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
| pub(crate) fn malformed(&self, text: &[u8]) -> bool { | ||
| match self { | ||
| Self::Standard(encoding) => *encoding == rs::UTF_8, | ||
| Self::Standard(encoding) if *encoding != rs::UTF_8 => !text.is_ascii() && encoding.decode(text).2, |
There was a problem hiding this comment.
Encoding::decode in encoding_rs sniffs a leading UTF-8 or UTF-16 byte order mark and switches decoders, so a value that starts with EF BB BF, FF FE or FE FF is judged by UTF-8 or UTF-16 rules here. The doc comment on malformed says this asks the decoder dbase calls, and that only matches if dbase's adapter sniffs the BOM the same way. I could not check that from this checkout, so confidence is low. If the adapter does not sniff, the count can differ from the replaced bytes on those rare values.
decode_without_bom_handling returns the same error flag without the sniffing, and it would make the match exact on either side:
| Self::Standard(encoding) if *encoding != rs::UTF_8 => !text.is_ascii() && encoding.decode(text).2, | |
| Self::Standard(encoding) if *encoding != rs::UTF_8 => !text.is_ascii() && encoding.decode_without_bom_handling(text).1, |
Category: bug
There was a problem hiding this comment.
Claude autofix (unclear): Not changed. Whether decode_without_bom_handling is the right call depends on whether dbase's EncodingRs adapter sniffs a BOM, and the dbase source is not in this checkout, so I can't confirm it. If the adapter does not sniff, the suggested change matches it; if it does, the current decode call is right. A maintainer should check the adapter.
Code reviewPR #2673 closes #2614. It makes a Bugs
Security
Performance
Quality
AGENTS.md
Already raised: 0. There were no prior review threads. Change sizeCompared with the base branch (merge base 5fa6a02 to PR head 4536e71): the whole PR against
|
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the |
The note for text values that aren't valid UTF-8 took its encoding from the source handle at the time it was said, not from the handle the counts were kept from. If the source was opened again in between, a Big5 or Shift_JIS layer's counts could be reported as not valid UTF-8. Record the encoding with the counts when they are kept, use it when the note is taken, and clear it in reopen with the counts. Counts from a different encoding replace the pending ones rather than adding to them. Applied by the claude-autofix workflow.
|
Claude autofix
|
| && std::str::from_utf8(padded(b)).is_err() | ||
| { | ||
| super::not_utf8_at(counts, k); | ||
| if matches!(v, Value::Text(_)) && declared.malformed(padded(b)) { |
There was a problem hiding this comment.
In the in-place read, each non-ASCII text value is now transcoded twice. value(c, b, decoder) decodes it (line 739), then declared.malformed(padded(b)) decodes the same bytes again and allocates a second String through encoding_rs's decode. On a national layer with accented or CJK names, that doubles the transcoding work and the allocations for every streamed read, and the whole-file path does the same thing (dbf::not_utf8 decodes each value once, then dbf_attrs decodes it again).
One way to avoid it: have value return the decoder's error flag next to the text (for example by decoding with decode_without_bom_handling and keeping had_errors), so values reads the flag instead of decoding again. The PR body's note of "one extra decode per non-ASCII text value" understates this, because the value is decoded once more on top of the one the read already does.
Category: performance
There was a problem hiding this comment.
Claude autofix (unclear): Not changed here. The second decode only costs an allocation when the declared code page isn't UTF-8: for UTF-8, DbfEncoding::malformed is a from_utf8 check, and for other code pages it runs encoding_rs decode again on each non-ASCII text value (crates/data/src/dbf.rs:34). Removing that means value returns the decoder's error flag, which changes the decoder path, and I can't build or measure it in this environment. A maintainer should decide that, ideally with cargo xtask memory numbers on an accented or CJK layer.
| /// [`SourceLayer::take_not_utf8`] says it, as [`SourceLayer::saw_unread_dates`] does for dates. | ||
| pub fn saw_not_utf8(&mut self, counts: &[usize]) { | ||
| if !self.not_utf8_said && counts.iter().any(|n| *n > 0) { | ||
| let encoding = self.handle.as_ref().map_or("UTF-8", |h| h.text_encoding()); |
There was a problem hiding this comment.
This is a new point, following the earlier thread on this lookup. The fix records the encoding when the counts are kept, but the lookup still falls back to "UTF-8" when self.handle is None, and that fallback is stored with the counts. If counts arrive while the handle is absent, a Big5 or Shift_JIS layer's counts are labelled as UTF-8 and get the "no .cpg names another encoding" clause, which is the mislabel the earlier thread described. The fallback hides the case rather than reporting it.
Confidence is low (about 20%): the callers in crates/map/src/lib.rs (lines 693 to 712) poll the source that holds the handle, so I could not find a path where it is None here. If it can't happen, the fallback is still a silent guess. Carrying the encoding in Batch next to not_utf8 would remove the lookup entirely.
Category: bug
There was a problem hiding this comment.
Claude autofix (declined): Not changed. The "UTF-8" fallback at crates/map/src/source.rs:892 isn't reached here: Polled::Done only comes from a read started with a handle (start returns early without one, source.rs:1822), and every path that clears handle also clears pending (reset_window at source.rs:1786, reached from reset and reopen). So a finished batch always has its handle, and carrying the encoding in Batch would be a data-crate API change for no reachable case.
Code reviewI read the full diff, the earlier review threads, and the callers of every changed signature across the workspace. I could not build or run the tests in this environment, so the findings below come from reading the code. Bugs
Security
Performance
Quality
AGENTS.md
Earlier concerns left alone
Change sizeCompared with
|
|
Claude autofix
|
Code reviewBugs. No new defect found in the changed code. I checked the decode-and-count paths in Security. None found. Field and value bytes are only read through Performance. Nothing new. The double decode of non-ASCII text values is already raised on Quality (not posted inline; for the release quality pass):
AGENTS.md. No rule break found:
Earlier concerns left alone (already raised, so not re-posted): Change sizeCompared: the whole PR against main (merge base 626e080 to PR head e2ec967).
|
Summary
A
.dbfthat declares a multibyte code page (by a.cpg, or by its language driver byte) and holds bytes that aren't valid in it was read with U+FFFD in their place and no notice: #2325 counted only text read as UTF-8.The count now asks the declared decoder (
DbfEncoding::malformed, onencoding_rs) instead offrom_utf8, for field names and values, on the whole read (dbf_reader: a loose or zipped Shapefile, a standalone.dbf, the browser) and on the read in place (ShpSource,Table::values, the chunked read). The notice names the declared code page:Field names say the same (
Shapefile: 1 field name isn't valid Shift_JIS …, in field 1; …). Exports anddata.convertof such a layer name the same page (Exported.not_utf8_in, from the newFeatureSource::text_encoding). UTF-8 notices read exactly as before, clean text in a declared page says nothing, and deleted records still aren't counted (the count runs beforetake_deleted). The DOS pages (437, 850, 852, 861, 865) map every byte to a character, so nothing in them is replaced and nothing is said; the user guide says so.Reaches desktop, CLI, Python, the Assistant and MCP through the existing notice path, and the browser through the whole read. No command, parameter or ribbon change; the way back is reopening the layer with the right
.cpg. Memory: no new copy of a layer; one extra decode per non-ASCII text value in a streamed read.Testing
malformed_multibyte_fields_replace_bytes_and_say_so(it wasmalformed_multibyte_fields_replace_bytes_without_dropping_records, which pinned the gap) andmalformed_values_are_counted_per_value_and_clean_text_says_nothing, incrates/data/tests/integration/shapefile_encoding.rs: names and values in 932, 936, 949 and 950 by language driver and by.cpg, through the ZIP, loose-file, in-place, sampled, complete and chunked reads; a field-filling value ending in a lead byte, also through a standalone.dbfand a read in place; clean multibyte text says nothing. Both failed onmain(no notes); pass now.a_streamed_shapefile_says_text_that_isnt_utf8_once_and_in_its_exports(engine) gains a Big5 case: the view read,share.exportLayeranddata.converteach say "isn't valid Big5" with the declared-page clause, and valid Big5 says nothing. It fails withnot_utf8_inset to "UTF-8".DbfEncoding::malformedand the names note indbf.rs.cargo test -p rustgis-data(lib 1748 passed, integration 70 passed),cargo test -p rustgis-map --lib(452 passed), the engine'sa_streamed_shapefileandhostile_paramsfilters, clippy with-D warningson data, map and engine.origin/main(fb77d77) with this branch and three other branches of mine (fix/2615-window-counts, fix/2447-terrain-block-keys and fix/2620-qgis-style-read-thread) merged: pre-commit on every file,cargo fmt --check, clippy with-D warnings,cargo test --workspace(8721 passed, 0 failed),cargo xtask layers,cargo xtask schema --checkwith the fixtures checked against the schema and test hygiene,cargo xtask rows, and the Python package's tests (maturin + pytest, 44 passed). Not run here: the wasm32 build (no clang on this machine; CI's Web app job checks it), macOS and Windows.UI evidence
No visible UI change.
Related issue
Closes #2614
Summary by CodeRabbit
Bug Fixes
Documentation