Skip to content

fix(index): add guard for short files in magic number check#598

Merged
Fdawgs merged 2 commits into
mainfrom
fix/short-files
Jul 5, 2026
Merged

fix(index): add guard for short files in magic number check#598
Fdawgs merged 2 commits into
mainfrom
fix/short-files

Conversation

@Fdawgs

@Fdawgs Fdawgs commented Jul 4, 2026

Copy link
Copy Markdown
Owner

fileHandle.read() may return fewer bytes than requested for files smaller than 6 bytes. Without checking bytesRead, the comparison against the RTF magic number {\rtf1 could produce a false positive on truncated input.

Also switches to Buffer.allocUnsafe as all bytes are immediately overwritten by the read, so the filling of all zeroes in Buffer.alloc was wasted. Teeny tiny micro-optimisation that saves ~2.84µs per call. All adds up when converting millions of files though!

Checklist

Copilot AI review requested due to automatic review settings July 4, 2026 18:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the RTF “magic number” detection in UnRTF.convert() to safely handle inputs shorter than the expected header length, and switches the header buffer allocation to an unsafe variant for a minor performance win while preserving correctness.

Changes:

  • Add a bytesRead guard so files shorter than RTF_MAGIC_NUMBER_LENGTH can’t be misclassified.
  • Switch from Buffer.alloc to Buffer.allocUnsafe for the magic-number read buffer.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/index.js

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 3 changed files in this pull request and generated no new comments.

@Fdawgs Fdawgs merged commit 79dc046 into main Jul 5, 2026
21 checks passed
@Fdawgs Fdawgs deleted the fix/short-files branch July 5, 2026 13:24
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