ORC-2166: [C++] Guard C++ integer arithmetic against overflow#2622
ORC-2166: [C++] Guard C++ integer arithmetic against overflow#2622ffacs wants to merge 3 commits into
Conversation
| hasNegativeLength |= value < 0; | ||
| size_t length = static_cast<size_t>(value); | ||
| size_t nextTotalLength = totalLength + length; | ||
| hasLengthOverflow |= nextTotalLength < totalLength; |
There was a problem hiding this comment.
Like skip() method, shall we throw here instead of waiting for the whole loop is finished?
ffa2af2 to
0c3a0c7
Compare
0c3a0c7 to
266b26b
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces reusable checked integer arithmetic helpers in the C++ codebase and applies them across multiple reader/allocation paths to prevent integer overflow from malformed ORC input (failing fast instead of wrapping into unsafe sizes/offsets).
Changes:
- Add
addWithOverflow/multiplyWithOverflowhelpers and unit tests for overflow behavior. - Harden reader logic (string/list/map/union/dictionary paths) against negative lengths and overflowing length accumulation.
- Add overflow-safe sizing logic for vector offsets,
DataBufferallocation sizing, and block buffer size/capacity calculations.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| c++/test/TestUtils.cc | Adds unit tests for the new overflow helper functions. |
| c++/test/TestColumnReader.cc | Adds regression tests for rejecting negative/overflowing length streams (string/list/map). |
| c++/test/meson.build | Wires the new test source into the Meson test build. |
| c++/test/CMakeLists.txt | Wires the new test source into the CMake test build. |
| c++/src/Vector.cc | Uses checked arithmetic when sizing list/map offset buffers (cap + 1). |
| c++/src/Utils.hh | Introduces reusable checked add/multiply helpers for integral types. |
| c++/src/MemoryPool.cc | Adds checked sizing for DataBuffer allocations and zeroing. |
| c++/src/DictionaryLoader.cc | Guards dictionary blob reads and dictionary offset accumulation against overflow/negative values. |
| c++/src/ColumnReader.cc | Adds overflow/negative-length guards for string direct, list/map length accumulation, union child counts, and double skip sizing. |
| c++/src/BlockBuffer.hh | Avoids potential overflow in getBlockNumber() computation. |
| c++/src/BlockBuffer.cc | Uses checked arithmetic for block buffer size/capacity growth. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| uint64_t nextBlockNumber = currentSize_ / blockSize_ + 1; | ||
| uint64_t nextSize = 0; | ||
| if (multiplyWithOverflow(nextBlockNumber, blockSize_, &nextSize)) { |
| auto addLength = [&](int64_t value) { | ||
| if (value < 0) { | ||
| hasNegativeLength = true; | ||
| return; | ||
| } | ||
| size_t length = static_cast<size_t>(value); | ||
| size_t nextTotalLength = 0; | ||
| bool overflow = addWithOverflow(totalLength, length, &nextTotalLength); | ||
| hasLengthOverflow |= overflow; |
| uint64_t checkedBufferSize(uint64_t count) { | ||
| uint64_t bytes = 0; | ||
| if (multiplyWithOverflow(static_cast<uint64_t>(sizeof(T)), count, &bytes)) { | ||
| throw std::length_error("DataBuffer allocation size overflow"); |
There was a problem hiding this comment.
Do we want to define a orc-specific exception that derives from orc::Exception? arrow-cpp catches all orc::Exception to avoid crashing.
| if (newBlockPtr != nullptr) { | ||
| blocks_.push_back(newBlockPtr); | ||
| currentCapacity_ += blockSize_; | ||
| try { |
There was a problem hiding this comment.
Why do we need this try-catch block?
What changes were proposed in this pull request?
This PR adds reusable checked integer arithmetic helpers for the C++ code:
addWithOverflowandmultiplyWithOverflow.The patch uses these helpers to reject overflow-prone arithmetic in reader and buffer paths, including direct string length accumulation, list/map length accumulation, dictionary offsets, float/double skip byte counts, union child counts,
DataBufferallocation sizes,BlockBuffersize/capacity calculations, and list/map vector offset capacity calculations.Regression tests were added for the overflow helpers and malformed string/list/map length streams.
Why are the changes needed?
Malformed ORC files can provide invalid or extremely large values in length streams. Negative lengths or overflowing length sums can cause unchecked integer wraparound, incorrectly sized allocations, wrapped offsets, or unsafe skip/copy behavior.
Centralizing checked arithmetic makes these cases fail cleanly before unsafe allocation or memory access. Reader-facing malformed input now fails with
ParseError.How was this patch tested?
Ran the full C++ test target:
Result:
1/2 Test #1: orc-test .... Passed
2/2 Test #2: tool-test .... Passed
100% tests passed, 0 tests failed out of 2
Also ran targeted tests during development for the new overflow checks.
Was this patch authored or co-authored using generative AI tooling?
Yes. Generated with OpenAI Codex.