From 7192c7237152d8dcdf2f60137b1dc25f1890646d Mon Sep 17 00:00:00 2001 From: melvin <1748826185@qq.com> Date: Fri, 8 Mar 2019 05:42:42 +0800 Subject: [PATCH] Modified __safe_add and __safe_sub function in src/count_min_sketch.c (#11) * modified __safe_add and __safe_sub these two function and test it under Ubuntu14.04 LTS with gcc 4.8.4. --- src/count_min_sketch.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/count_min_sketch.c b/src/count_min_sketch.c index 92dff58..ea26c58 100644 --- a/src/count_min_sketch.c +++ b/src/count_min_sketch.c @@ -310,16 +310,16 @@ int __compare (const void * a, const void * b) { int32_t __safe_add(int a, int b) { /* use the gcc macro if compiling with GCC, otherwise, simple overflow check */ int32_t c = 0; - #ifdef __GNUC__ + #if (defined(__GNU__) && __GNUC__ >= 5) int bl = __builtin_add_overflow(a, b, &c); if (bl != 0) { c = INT_MAX; } #else - if (b < INT_MIN + a) { - c = INT_MIN; + if (b > INT_MAX - a) { + c = INT_MAX; } else { - c = a - b; + c = a + b; } #endif @@ -329,16 +329,16 @@ int32_t __safe_add(int a, int b) { int32_t __safe_sub(int32_t a, int32_t b) { /* use the gcc macro if compiling with GCC, otherwise, simple overflow check */ int32_t c = 0; - #ifdef __GNUC__ + #if (defined(__GNU__) && __GNUC__ >= 5) int32_t bl = __builtin_sub_overflow(a, b, &c); if (bl != 0) { c = INT_MAX; } #else - if (b > INT_MAX - a) { + if (b < a - INT_MAX) { c = INT_MAX; } else { - c = a + b; + c = a - b; } #endif