3646: AVRO-4228: [c++] Fix BinaryDecoder::arrayNext() to handle negative block counts#33
3646: AVRO-4228: [c++] Fix BinaryDecoder::arrayNext() to handle negative block counts#33martin-augment wants to merge 2 commits intomainfrom
Conversation
Summary of ChangesHello @martin-augment, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical bug in the C++ Avro Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
WalkthroughThis change modifies the ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Code Review: AVRO-4228 - Fix BinaryDecoder::arrayNext() to handle negative block countsSummaryThis PR fixes a critical bug where Code Quality ✅Excellent fix! The change is:
Bug Analysis 🐛The Problem:
The Fix:
Test Coverage ✅The test case Strengths:
Suggestions for Enhancement (optional, not blocking):
Performance ✅No performance concerns. The change:
Security ✅Security improvement! This fix addresses:
Consistency Check 🔍Verified that the pattern is now consistent across all similar methods:
Recommendations
Final Verdict: LGTM 🚀This is a textbook example of a good bug fix: minimal change, proper understanding of the specification, comprehensive test coverage, and improved correctness. The PR is ready to merge. Review generated by Claude Code |
🤖 Augment PR SummarySummary: Fixes C++ binary decoding of array blocks when block counts are negative (Avro’s “byte-sized block” encoding). Changes:
Technical Notes: Negative counts per spec indicate 🤖 Was this summary useful? React with 👍 or 👎 |
| // Per the Avro spec, a negative count means: abs(count) items follow, | ||
| // preceded by a long byte-size of the block. | ||
| // | ||
| // Block 1: count=-3, bytesize=3, items: 10, 20, 30 |
There was a problem hiding this comment.
This test puts the negative block count in the first block (handled by arrayStart()), so arrayNext() never encounters a negative count; as written, it likely would have passed before this PR too. Consider adding a later block with a negative count so the test actually exercises the arrayNext() fix.
Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.
There was a problem hiding this comment.
value:useful; category:bug; feedback: The Augment AI reviewer is correct! The new test passes even without the fix, so it does not prevent regressions at all. The test should be improved to use a negative count for the non-first block(s) too.
|
|
||
| size_t BinaryDecoder::arrayNext() { | ||
| return static_cast<size_t>(doDecodeLong()); | ||
| return doDecodeItemCount(); |
There was a problem hiding this comment.
doDecodeItemCount() negates result when it’s negative; if the encoded count is INT64_MIN, -result overflows (undefined behavior). Since arrayNext() now routes through this path, consider explicitly guarding that value and throwing instead of overflowing.
Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.
There was a problem hiding this comment.
value:delightful; category:security; feedback: The Augment AI reviewer is correct! If the count value is size_t's min then the negation will lead to an overflow and even more issues because of this. Prevents a vulnerability if an attacker crafts an Avro file with such min count.
There was a problem hiding this comment.
Code Review
This pull request correctly fixes an issue in BinaryDecoder::arrayNext() to handle negative block counts as specified by the Avro specification. This addresses a bug where negative block counts were not being handled, which could lead to incorrect parsing and potential resource exhaustion. While the fix reuses existing logic and adds a new test case, a critical vulnerability remains in doDecodeItemCount(): negating LONG_MIN results in undefined behavior and a potential denial of service via a near-infinite loop. There is also a suggestion to make the new test case more robust and concise.
|
|
||
| size_t BinaryDecoder::arrayNext() { | ||
| return static_cast<size_t>(doDecodeLong()); | ||
| return doDecodeItemCount(); |
There was a problem hiding this comment.
The call to doDecodeItemCount() on line 173 introduces a potential undefined behavior and denial of service. The doDecodeItemCount() function (defined on line 163) contains an expression -result on line 167 that overflows when result is LONG_MIN (-9223372036854775808). An attacker can trigger this by providing a specially crafted Avro varint (e.g., 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x01). In a 2's complement environment, this typically results in a very large positive value being returned, which can cause the caller to enter a near-infinite loop, leading to a denial of service. It is recommended to handle the LONG_MIN case in doDecodeItemCount() or use a safer negation like static_cast<size_t>(-(result + 1)) + 1.
There was a problem hiding this comment.
value:delightful; category:security; feedback: The Gemini AI reviewer is correct! If the count value is size_t's min then the negation will lead to an overflow and even more issues because of this. Prevents a vulnerability if an attacker crafts an Avro file with such min count.
| BOOST_CHECK_EQUAL(result.size(), 5u); | ||
| BOOST_CHECK_EQUAL(result[0], 10); | ||
| BOOST_CHECK_EQUAL(result[1], 20); | ||
| BOOST_CHECK_EQUAL(result[2], 30); | ||
| BOOST_CHECK_EQUAL(result[3], 40); | ||
| BOOST_CHECK_EQUAL(result[4], 50); |
There was a problem hiding this comment.
This series of checks can be made more robust and concise. If the initial size check fails, the subsequent element access result[i] could lead to out-of-bounds access and a test crash, because BOOST_CHECK does not abort the test. Using BOOST_CHECK_EQUAL_COLLECTIONS is a better approach as it is idiomatic in this file, checks for size equality internally, and compares all elements in a single statement. This makes the test safer and more readable.
const std::vector<int32_t> expected = {10, 20, 30, 40, 50};
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());There was a problem hiding this comment.
value:good-to-have; category:bug; feedback: The Gemini AI reviewer is correct! Using BOOST_CHECK_EQUAL_COLLECTIONS will provide better error messages when asserting the values in a collection, like in this test.
3646: To review by AI