diff options
| author | Renato Westphal <renato@opensourcerouting.org> | 2020-09-04 22:33:48 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-04 22:33:48 -0300 |
| commit | dcdaabcede9302790c48e18132f202ef3886f635 (patch) | |
| tree | 6ca5b8ff41952921b31c1f10fe0f629f021d8e95 /lib/hash.c | |
| parent | c7b5a0ae3a2195b04a00e6359c14e5033bddf253 (diff) | |
| parent | 763a5d3c2dc7e9061006d56a9a983c2a8be64765 (diff) | |
Merge pull request #7046 from qlyoung/fix-various-integer-issues
Fix various integer signedness / overflow issues
Diffstat (limited to 'lib/hash.c')
| -rw-r--r-- | lib/hash.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/lib/hash.c b/lib/hash.c index 7f8a237047..85982774ac 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -77,9 +77,20 @@ void *hash_alloc_intern(void *arg) return arg; } +/* + * ssq = ssq + (new^2 - old^2) + * = ssq + ((new + old) * (new - old)) + */ #define hash_update_ssq(hz, old, new) \ - atomic_fetch_add_explicit(&hz->stats.ssq, (new + old) * (new - old), \ - memory_order_relaxed); + do { \ + int _adjust = (new + old) * (new - old); \ + if (_adjust < 0) \ + atomic_fetch_sub_explicit(&hz->stats.ssq, -_adjust, \ + memory_order_relaxed); \ + else \ + atomic_fetch_add_explicit(&hz->stats.ssq, _adjust, \ + memory_order_relaxed); \ + } while (0) /* Expand hash if the chain length exceeds the threshold. */ static void hash_expand(struct hash *hash) |
