Skip to content

Stop reading a dBASE III memo at its end-of-memo marker - #350

Merged
chrisrichards merged 1 commit into
mainfrom
fix-dbase3-memo-read
Nov 24, 2026
Merged

chrisrichards merged 1 commit into
mainfrom
fix-dbase3-memo-read

Conversation

@chrisrichards

Copy link
Copy Markdown
Member

Fixes #349

Problem

DbfMemoDbase3.BuildMemo had its stop condition inverted: it stopped when a trimmed block was 512 characters or more and carried on when it was shorter. ReadString trims NULs and spaces, so in a memo file that pads blocks with NULs almost every block comes back short, and the loop read on through the following memos. The 0x1A end-of-memo marker was never checked.

On the file from the issue (1.8 GB DBF, 2.3 GB DBT, four memo columns) the first memo came back as 322 million characters; the real memos average 112 bytes.

Fix

Read raw 512 byte blocks into a reused buffer, stop at the first 0x1A (or NUL, or the end of the file), decode once, then trim surrounding spaces as before.

Reporter's file Before After
Rows read 46 in 242s (stopped) all 1,277,285 in 4.1s
Peak RSS 4.3 GB 65 MB
Longest memo 322M chars 631 chars

Behaviour changes

  • Memos longer than one block are now returned in full; they were cut at 512 characters (record 0 of dbase_83 was affected and had no test).
  • Memo values no longer carry the 0x1A markers or text from following memos.
  • Spaces inside a memo at a block boundary are kept.
  • A memo with no marker and no NUL reads to the end of the file; there is no cap.

Tests

New DbfMemoDbase3Tests (18 cases, 16 fail on main) using synthetic memo streams and the existing dbase_83 fixture: NUL, stale-data and space padding, memos ending either side of a block boundary, a multi-byte character split across blocks, missing marker, and end of file. The reporter's files are not included.

🤖 Generated with Claude Code

DbfMemoDbase3.BuildMemo kept reading blocks while the trimmed block was
shorter than 512 characters, so with NUL padded blocks it ran on through
the following memos. Each memo then pulled in hundreds of megabytes,
making reads extremely slow and memory hungry, and a memo that filled its
first block was cut at 512 characters.

Read raw blocks until the first 0x1A (or NUL, or the end of the file) and
decode the bytes once.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@chrisrichards
chrisrichards merged commit 6a7cebb into main Nov 24, 2026
3 checks passed
@chrisrichards
chrisrichards deleted the fix-dbase3-memo-read branch November 24, 2026 09:48
This was referenced Nov 24, 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.

Slow reading DBF file

1 participant