Skip to content

fix: Handle files shorter than the Arrow file marker#177

Open
maltzsama wants to merge 1 commit into
apache:mainfrom
maltzsama:fix/validatefiledata
Open

fix: Handle files shorter than the Arrow file marker#177
maltzsama wants to merge 1 commit into
apache:mainfrom
maltzsama:fix/validatefiledata

Conversation

@maltzsama

Copy link
Copy Markdown
Contributor

What's Changed

Fixed a crash in ArrowReader.fromFile() when processing files smaller than the Arrow file marker size (6 bytes). The function now gracefully returns a .failure result instead of crashing with a fatal range error.

Root Cause

The validateFileData() function in ArrowReaderHelper.swift attempted to read the first and last 6 bytes of the input data without checking if the data was large enough. When data.count < 6, subscript operations like data[..<6] would fail with a range error, causing a crash.

Changes Made

  1. Added a bounds check: guard data.count >= markerLength * 2 else { return false }
  2. Replaced direct subscripting with prefix() and suffix() for safer slice operations
  3. Added unit tests to verify the fix handles edge cases:
    • Empty files
    • Files smaller than marker size
    • Files with partial markers

Testing

  • New test testFromFileWithInvalidData() added to IPCFileReaderTests
  • Test verifies that invalid files return .failure instead of crashing
  • All existing tests continue to pass

Impact

This fix improves robustness for applications using ArrowReader.fromFile() with user-supplied or untrusted files, preventing denial of service from truncated or malformed input.

Closes #176

@kou

kou commented Jul 24, 2026

Copy link
Copy Markdown
Member

@willtemperley Could you review this?

@willtemperley willtemperley left a comment

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.

Looks like an improvement in general. I think ideally testing for degenerate cases like this would be done against the fuzz regression files [1] too but that's a fair amount of work.

[1] apache/arrow-testing#120

@kou

kou commented Jul 25, 2026

Copy link
Copy Markdown
Member

OK. Let's work on it as a separated task.

@maltzsama

  1. Could you open an issue for fuzzy testing support?
  2. Could you fix the lint failure?

Added bounds check in validateFileData() to prevent crash when processing
files smaller than the Arrow file marker size (6 bytes). The function now
returns false gracefully instead of panicking.

- Added guard clause to check minimum file size
- Replaced unsafe subscripting with prefix()/suffix()
- Added unit test testFromFileWithInvalidData() covering edge cases

Closes apache#176
@maltzsama
maltzsama force-pushed the fix/validatefiledata branch from 5b21592 to d81084e Compare July 25, 2026 13:53
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.

ArrowReader.fromFile crashes on files smaller than the Arrow file marker

3 participants