diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2021-09-03 15:17:56 +0200 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2021-09-03 15:17:56 +0200 |
| commit | badbd93af92836c7a38ef069d410a829e2575ce2 (patch) | |
| tree | b7463b6f8deb8b578344ed2fef1d678a8aeef8a4 /src/lib/openjp2/opj_intmath.h | |
| parent | 172583ab5b9b8de67bed1eed11bd26a77d94b521 (diff) | |
Avoid integer overflows in DWT. Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11700 and https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30646
Diffstat (limited to 'src/lib/openjp2/opj_intmath.h')
| -rw-r--r-- | src/lib/openjp2/opj_intmath.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib/openjp2/opj_intmath.h b/src/lib/openjp2/opj_intmath.h index afe69d90..f8cc6139 100644 --- a/src/lib/openjp2/opj_intmath.h +++ b/src/lib/openjp2/opj_intmath.h @@ -276,6 +276,44 @@ static INLINE OPJ_INT32 opj_int_fix_mul_t1(OPJ_INT32 a, OPJ_INT32 b) return (OPJ_INT32)(temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) ; } +/** +Addtion two signed integers with a wrap-around behaviour. +Assumes complement-to-two signed integers. +@param a +@param b +@return Returns a + b +*/ +static INLINE OPJ_INT32 opj_int_add_no_overflow(OPJ_INT32 a, OPJ_INT32 b) +{ + void* pa = &a; + void* pb = &b; + OPJ_UINT32* upa = (OPJ_UINT32*)pa; + OPJ_UINT32* upb = (OPJ_UINT32*)pb; + OPJ_UINT32 ures = *upa + *upb; + void* pures = &ures; + OPJ_INT32* ipres = (OPJ_INT32*)pures; + return *ipres; +} + +/** +Subtract two signed integers with a wrap-around behaviour. +Assumes complement-to-two signed integers. +@param a +@param b +@return Returns a - b +*/ +static INLINE OPJ_INT32 opj_int_sub_no_overflow(OPJ_INT32 a, OPJ_INT32 b) +{ + void* pa = &a; + void* pb = &b; + OPJ_UINT32* upa = (OPJ_UINT32*)pa; + OPJ_UINT32* upb = (OPJ_UINT32*)pb; + OPJ_UINT32 ures = *upa - *upb; + void* pures = &ures; + OPJ_INT32* ipres = (OPJ_INT32*)pures; + return *ipres; +} + /* ----------------------------------------------------------------------- */ /*@}*/ |
