From: Mathieu Desnoyers Date: Wed, 28 Jul 2021 20:19:56 +0000 (-0400) Subject: Truncate aggregation sum to fit map bitness X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=4f8afc535e77070f1ef00434674f0417c6f9ef69;p=lttng-ust.git Truncate aggregation sum to fit map bitness Signed-off-by: Mathieu Desnoyers Change-Id: I89a77d9df897f2b1cc7bfc62dcc9e12f31e4a12b --- diff --git a/src/common/counter/counter.c b/src/common/counter/counter.c index 99a46af6..4f2ac4c8 100644 --- a/src/common/counter/counter.c +++ b/src/common/counter/counter.c @@ -453,6 +453,35 @@ int lttng_counter_aggregate(const struct lib_counter_config *config, default: return -EINVAL; } + switch (config->counter_size) { + case COUNTER_SIZE_8_BIT: + if (sum > INT8_MAX) + *overflow = true; + if (sum < INT8_MIN) + *underflow = true; + sum = (int8_t) sum; /* Truncate sum. */ + break; + case COUNTER_SIZE_16_BIT: + if (sum > INT16_MAX) + *overflow = true; + if (sum < INT16_MIN) + *underflow = true; + sum = (int16_t) sum; /* Truncate sum. */ + break; + case COUNTER_SIZE_32_BIT: + if (sum > INT32_MAX) + *overflow = true; + if (sum < INT32_MIN) + *underflow = true; + sum = (int32_t) sum; /* Truncate sum. */ + break; +#if CAA_BITS_PER_LONG == 64 + case COUNTER_SIZE_64_BIT: + break; +#endif + default: + return -EINVAL; + } *value = sum; return 0; }