@@ -221,6 +221,11 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
221221 if (out == NULL || in == NULL ) {
222222 return BAD_FUNC_ARG ;
223223 }
224+ /* Cap input so the initial doubling and additive growth in the loop
225+ * cannot overflow word32 or the int return type. */
226+ if (inSz > (word32 )(INT_MAX / 2 )) {
227+ return BAD_FUNC_ARG ;
228+ }
224229 i = (maxSz == 1 )? 1 : 2 ; /* start with output buffer twice the size of input
225230 * unless max was set to 1 */
226231
@@ -229,7 +234,7 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
229234 /* Check for source > 64K on 16-bit machine: */
230235 if ((uLong )stream .avail_in != inSz ) return DECOMPRESS_INIT_E ;
231236
232- tmpSz = inSz * i ;
237+ tmpSz = inSz * ( word32 ) i ;
233238 tmp = (byte * )XMALLOC (tmpSz , heap , memoryType );
234239 if (tmp == NULL )
235240 return MEMORY_E ;
@@ -278,6 +283,11 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
278283 }
279284 i ++ ;
280285
286+ if (tmpSz > (word32 )INT_MAX - inSz ) {
287+ WOLFSSL_MSG ("Decompress buffer would exceed INT_MAX" );
288+ result = DECOMPRESS_E ;
289+ break ;
290+ }
281291 newSz = tmpSz + inSz ;
282292 newTmp = (byte * )XMALLOC (newSz , heap , memoryType );
283293 if (newTmp == NULL ) {
@@ -295,13 +305,18 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
295305 } while (result == Z_OK );
296306
297307 if (result == Z_STREAM_END ) {
298- result = (int )stream .total_out ;
299- * out = (byte * )XMALLOC (result , heap , memoryType );
300- if (* out != NULL ) {
301- XMEMCPY (* out , tmp , result );
308+ if (stream .total_out > (uLong )INT_MAX ) {
309+ result = DECOMPRESS_E ;
302310 }
303311 else {
304- result = MEMORY_E ;
312+ result = (int )stream .total_out ;
313+ * out = (byte * )XMALLOC (result , heap , memoryType );
314+ if (* out != NULL ) {
315+ XMEMCPY (* out , tmp , result );
316+ }
317+ else {
318+ result = MEMORY_E ;
319+ }
305320 }
306321 }
307322 else {
0 commit comments