upstream: Use int64_t for intermediate values in int32_MINMAX to

prevent signed 32-bit integer overflow.

Found by and ok djm@
ok markus@

OpenBSD-Commit-ID: 4f0704768e34cf45fdd792bac4011c6971881bb3
This commit is contained in:
tobhe@openbsd.org 2020-12-30 14:13:28 +00:00 committed by Damien Miller
parent 5c1953bf98
commit 3d999be7b9
1 changed files with 20 additions and 0 deletions

20
int32_minmax.inc Normal file
View File

@ -0,0 +1,20 @@
/* $OpenBSD: int32_minmax.inc,v 1.1 2020/12/30 14:13:28 tobhe Exp $ */
/*
* Public Domain, Authors:
* - Daniel J. Bernstein
* - Chitchanok Chuengsatiansup
* - Tanja Lange
* - Christine van Vredendaal
*/
#define int32_MINMAX(a,b) \
do { \
int64_t ab = (int64_t)b ^ (int64_t)a; \
int64_t c = (int64_t)b - (int64_t)a; \
c ^= ab & (c ^ b); \
c >>= 31; \
c &= ab; \
a ^= c; \
b ^= c; \
} while(0)