Skip to content

Commit 8850c6b

Browse files
shouzhiclaude
andcommitted
ORC-2167: [C++] Replace __builtin_add_overflow with portable helper
__builtin_add_overflow is a GCC/Clang built-in and is unavailable on MSVC, breaking the Windows build. Introduce a small portable addOverflow() inline helper based on defined unsigned wrap-around behavior and use it at all call sites in Reader.cc. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 50a2ca0 commit 8850c6b

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

c++/src/Reader.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ namespace orc {
4444
"1.6.0", "1.6.1", "1.6.2", "1.6.3", "1.6.4", "1.6.5", "1.6.6",
4545
"1.6.7", "1.6.8", "1.6.9", "1.6.10", "1.6.11", "1.7.0"};
4646

47+
// Portable unsigned addition overflow check. Returns true on overflow.
48+
// Relies on defined wrap-around behavior of unsigned integer arithmetic.
49+
static inline bool addOverflow(uint64_t a, uint64_t b, uint64_t* result) {
50+
*result = a + b;
51+
return *result < a;
52+
}
53+
4754
ReaderMetrics* getDefaultReaderMetrics() {
4855
static ReaderMetrics internal;
4956
return &internal;
@@ -887,10 +894,9 @@ namespace orc {
887894

888895
// Check for overflow in length calculations
889896
uint64_t totalTail;
890-
if (__builtin_add_overflow(footerLength, metadataSize, &totalTail) ||
891-
__builtin_add_overflow(totalTail, postscriptLength_, &totalTail) ||
892-
__builtin_add_overflow(totalTail, 1ULL, &totalTail) ||
893-
totalTail > fileLength_) {
897+
if (addOverflow(footerLength, metadataSize, &totalTail) ||
898+
addOverflow(totalTail, postscriptLength_, &totalTail) ||
899+
addOverflow(totalTail, 1ULL, &totalTail) || totalTail > fileLength_) {
894900
std::stringstream msg;
895901
msg << "Invalid Metadata length: fileLength=" << fileLength_
896902
<< ", metadataLength=" << metadataSize << ", footerLength=" << footerLength
@@ -1241,9 +1247,9 @@ namespace orc {
12411247

12421248
// Check for overflow and bounds validity
12431249
uint64_t stripeTotalLength;
1244-
if (__builtin_add_overflow(indexLength, dataLength, &stripeTotalLength) ||
1245-
__builtin_add_overflow(stripeTotalLength, footerLength, &stripeTotalLength) ||
1246-
__builtin_add_overflow(stripeOffset, stripeTotalLength, &stripeTotalLength) ||
1250+
if (addOverflow(indexLength, dataLength, &stripeTotalLength) ||
1251+
addOverflow(stripeTotalLength, footerLength, &stripeTotalLength) ||
1252+
addOverflow(stripeOffset, stripeTotalLength, &stripeTotalLength) ||
12471253
stripeTotalLength >= fileLength) {
12481254
std::stringstream msg;
12491255
msg << "Malformed StripeInformation at stripe index " << currentStripe_
@@ -1654,9 +1660,8 @@ namespace orc {
16541660

16551661
// Check for overflow before calculating tailSize
16561662
uint64_t tailSize;
1657-
if (__builtin_add_overflow(1ULL, postscriptLength, &tailSize) ||
1658-
__builtin_add_overflow(tailSize, footerSize, &tailSize) ||
1659-
tailSize >= fileLength) {
1663+
if (addOverflow(1ULL, postscriptLength, &tailSize) ||
1664+
addOverflow(tailSize, footerSize, &tailSize) || tailSize >= fileLength) {
16601665
std::stringstream msg;
16611666
msg << "Invalid tail size: footerSize=" << footerSize
16621667
<< ", postscriptLength=" << postscriptLength

0 commit comments

Comments
 (0)