Implementation:ClickHouse ClickHouse ArithmeticOverflow
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Arithmetic, Safety |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Safe arithmetic operations that detect and handle integer overflow using compiler builtins.
Description
Provides overflow-safe arithmetic functions using GCC/Clang builtins like `__builtin_add_overflow`. Supports addition, subtraction, and multiplication for all integer types including 128 and 256-bit integers.
Usage
Use when performing arithmetic that might overflow, implementing safe numeric operations, or validating computation results.
Code Reference
Source Location
- Repository: ClickHouse
- File: base/base/arithmeticOverflow.h
- Lines: 1-211
Signature
template <typename T>
bool addOverflow(T x, T y, T & res);
template <typename T>
bool subOverflow(T x, T y, T & res);
template <typename T>
bool mulOverflow(T x, T y, T & res);
template <typename T1, typename T2>
auto mulIgnoreOverflow(T1 x, T2 y);
template <typename T1, typename T2>
auto addIgnoreOverflow(T1 x, T2 y);
template <typename T1, typename T2>
auto subIgnoreOverflow(T1 x, T2 y);
Import
#include <base/arithmeticOverflow.h>
Usage Examples
#include <base/arithmeticOverflow.h>
Int64 result;
if (common::addOverflow(INT64_MAX, 1LL, result)) {
// Overflow occurred
} else {
// result contains sum
}
// Multiply with overflow check
UInt64 product;
if (common::mulOverflow(1000000ULL, 1000000ULL, product)) {
// Overflow
}
// Ignore overflow (for cases where wrapping is acceptable)
auto sum = common::addIgnoreOverflow(INT32_MAX, 1);
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment