Skip to content

Commit

Permalink
ORC-1834: [C++] Fix undefined behavior
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?
Unaligned reads are UB in C++, memcpy-ing zero bytes is UB either.

### How was this patch tested?
Internal UBsan report was used to detect and fix this bug.

Closes #2112 from georgthegreat/patch-3.

Authored-by: Yuriy Chernyshov <[email protected]>
Signed-off-by: Gang Wu <[email protected]>
(cherry picked from commit ab084b5)
Signed-off-by: Dongjoon Hyun <[email protected]>
  • Loading branch information
georgthegreat authored and dongjoon-hyun committed Jan 17, 2025
1 parent f96d345 commit e0fee1e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions c++/src/ColumnReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ namespace orc {
int64_t bits = 0;
if (bufferEnd_ - bufferPointer_ >= 8) {
if (isLittleEndian) {
bits = *(reinterpret_cast<const int64_t*>(bufferPointer_));
memcpy(&bits, bufferPointer_, sizeof(bits));
} else {
bits = static_cast<int64_t>(static_cast<unsigned char>(bufferPointer_[0]));
bits |= static_cast<int64_t>(static_cast<unsigned char>(bufferPointer_[1])) << 8;
Expand Down Expand Up @@ -509,8 +509,10 @@ namespace orc {
bufferNum = std::min(numValues,
static_cast<size_t>(bufferEnd_ - bufferPointer_) / bytesPerValue_);
uint64_t bufferBytes = bufferNum * bytesPerValue_;
memcpy(outArray, bufferPointer_, bufferBytes);
bufferPointer_ += bufferBytes;
if (bufferBytes > 0) {
memcpy(outArray, bufferPointer_, bufferBytes);
bufferPointer_ += bufferBytes;
}
}
for (size_t i = bufferNum; i < numValues; ++i) {
outArray[i] = readDouble<ValueType>();
Expand Down

1 comment on commit e0fee1e

@github-actions
Copy link

Choose a reason for hiding this comment

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

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-tidy (v12.0.0) reports: 1 concern(s)
  • c++/src/ColumnReader.cc:19:10: error: [clang-diagnostic-error]

    'orc/Int128.hh' file not found

    #include "orc/Int128.hh"
             ^

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.