Repository navigation
fs: fix duplicate bytes when Utf8Stream splits a character - #67725
SAMARTHJAISWAL wants to merge 2 commits into
Conversation
When a partial write ended inside a multi-byte UTF-8 character, Utf8Stream backed up to the start of that character and wrote all of it again. The leading bytes of the character were already in the destination, so they ended up there twice. Keep the unwritten bytes of the split character instead and write them before anything else, in the asynchronous, synchronous and flushSync() paths. Fixes: nodejs#67679 Signed-off-by: Samarth Jaiswal <samarthjaiswal30@gmail.com>
|
Review requested:
|
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. Caution AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context. |
|
Could we add two regression cases to the existing test? Both cases can silently lose data. The first drops a queued write, and the second reports a successful flush even though the file ends with an incomplete UTF-8 character.
|
flushSync() dropped the next queued string after it wrote the rest of a split character, and lost the rest of a string when a write threw. flush() and end() did not count the rest of a split character as pending, so they could report success with the character incomplete. Signed-off-by: Samarth Jaiswal <samarthjaiswal30@gmail.com>
When a short write ended inside a multi-byte character,
Utf8Streambacked upto the start of that character and wrote all of it again, although its first
bytes were already in the destination. So those bytes ended up there twice.
This keeps the unwritten bytes of the split character and writes them before
anything else, in the async, sync and
flushSync()paths.The existing partial-write test asserted that the whole character is sent
again, which is the behaviour reported in the issue, so I rewrote it to make
real short writes to a file and compare the file content with the input.
Not changed:
flushSync()called while an async write is in flight can stillreorder output. The code already documents that as not guaranteed.
AI disclosure: I used an AI coding assistant to help write the fix and the
tests. I read through the diff, ran the repro from the issue before and after
the change, and ran the full CI on my fork.
Fixes: #67679