Skip to content

Commit 38e08cf

Browse files
committed
Prevent wrap-around in silk_resampler_down2_hp()
Would only happen on pathologically loud HF signals, so this is just to make fuzzers happy.
1 parent d17b5df commit 38e08cf

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

celt/arch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ static OPUS_INLINE int celt_isnan(float x)
315315
#define PSHR32(a,shift) (a)
316316
#define VSHR32(a,shift) (a)
317317

318+
#define SHR64(a,shift) (a)
319+
318320
#define PSHR(a,shift) (a)
319321
#define SHR(a,shift) (a)
320322
#define SHL(a,shift) (a)

celt/fixed_debug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ static OPUS_INLINE int SHL32_(opus_int64 a, int shift, char *file, int line)
241241
#define PSHR32(a,shift) (celt_mips--,SHR32(ADD32((a),(((opus_val32)(1)<<((shift))>>1))),shift))
242242
#define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
243243

244+
#define SHR64(a,shift) (celt_mips++,(a) >> (shift))
245+
244246
#define ROUND16(x,a) (celt_mips--,EXTRACT16(PSHR32((x),(a))))
245247
#define SROUND16(x,a) (celt_mips--,EXTRACT16(SATURATE(PSHR32(x,a), 32767)));
246248

celt/fixed_generic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
/** 32-bit arithmetic shift right where the argument can be negative */
115115
#define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
116116

117+
/** Arithmetic shift-right of a 64-bit value */
118+
#define SHR64(a,shift) ((a) >> (shift))
119+
117120
/** "RAW" macros, should not be used outside of this header file */
118121
#define SHR(a,shift) ((a) >> (shift))
119122
#define SHL(a,shift) SHL32(a,shift)

src/analysis.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,15 @@ static opus_val32 silk_resampler_down2_hp(
149149
out32_hp = ADD32( out32_hp, X );
150150
S[ 2 ] = ADD32( -in32, X );
151151

152-
hp_ener += out32_hp*(opus_val64)out32_hp;
152+
/* len2 can be up to 480, so we shift by 8 to make it fit. */
153+
hp_ener += SHR64(out32_hp*(opus_val64)out32_hp, 8);
153154
/* Add, convert back to int16 and store to output */
154155
out[ k ] = HALF32(out32);
155156
}
156157
#ifdef FIXED_POINT
157-
/* len2 can be up to 480, so we shift by 8 more to make it fit. */
158-
hp_ener = hp_ener >> (2*SIG_SHIFT + 8);
158+
/* Fitting in 32 bits. */
159+
hp_ener = hp_ener >> (2*SIG_SHIFT);
160+
if (hp_ener > 2147483647) hp_ener = 2147483647;
159161
#endif
160162
return (opus_val32)hp_ener;
161163
}

0 commit comments

Comments
 (0)