Skip to content

Validate negative OCF block size before conversion#586

Merged
Kriskras99 merged 6 commits into
apache:mainfrom
MitNarodia:validate-ocf-block-metadata
Jul 14, 2026
Merged

Validate negative OCF block size before conversion#586
Kriskras99 merged 6 commits into
apache:mainfrom
MitNarodia:validate-ocf-block-metadata

Conversation

@MitNarodia

Copy link
Copy Markdown
Contributor

Summary

The OCF reader currently converts the block size from i64 to usize using an unchecked cast.

This change validates the block size using usize::try_from() before conversion and returns the existing ConvertI64ToUsize error if a malformed negative block size is encountered.

Motivation

The Java Avro implementation explicitly validates negative OCF block sizes before using them.

In the current Rust implementation, a negative block size is eventually rejected by safe_len(), but only after the unchecked cast wraps the value to usize, resulting in a MemoryAllocation error.

While the malformed input is still rejected, the reported error reflects a failed memory allocation rather than the actual issue: invalid OCF block metadata. This change validates the block size immediately after it is read and returns a more precise error that directly reflects the malformed metadata, while preserving the behavior for valid inputs.

Testing

  • Added a regression test covering a negative OCF block size.
  • Verified that the existing test suite passes with:
cargo test -p apache-avro --lib --tests --features derive

@MitNarodia MitNarodia force-pushed the validate-ocf-block-metadata branch from 1a83911 to 5f86c60 Compare July 13, 2026 20:02
@MitNarodia MitNarodia force-pushed the validate-ocf-block-metadata branch from 5f86c60 to e868d41 Compare July 13, 2026 20:07
Comment thread avro/tests/negative_block_size.rs Outdated
@Kriskras99

Copy link
Copy Markdown
Contributor

I've expanded the check to the object count. As this would have duplicate error logic I've replaced util::read_long with a new util::read_ulong.
I've also moved the test into block.rs.
@martin-g can you do one last check on my modifications?

Comment thread avro/src/util.rs Outdated
@Kriskras99 Kriskras99 merged commit 974efd4 into apache:main Jul 14, 2026
16 checks passed
@Kriskras99

Copy link
Copy Markdown
Contributor

Thank you for your contribution @MitNarodia!

@MitNarodia

Copy link
Copy Markdown
Contributor Author

Thank you! I really appreciate the review and feedback.

@Kriskras99 Kriskras99 mentioned this pull request Jul 14, 2026
@martin-g martin-g added this to the 0.22.0 milestone Jul 14, 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.

3 participants