bzip2: Add missing checks for malloc returning NULL

Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
LKML-Reference: <4b26b1ef.ln20bM9Mn4gzB21L%phillip@lougher.demon.co.uk>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
Phillip Lougher 2009-12-14 21:45:19 +00:00 committed by H. Peter Anvin
parent c1e7c3ae59
commit d4529862ca

View file

@ -637,6 +637,8 @@ static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
/* Allocate bunzip_data. Most fields initialize to zero. */ /* Allocate bunzip_data. Most fields initialize to zero. */
bd = *bdp = malloc(i); bd = *bdp = malloc(i);
if (!bd)
return RETVAL_OUT_OF_MEMORY;
memset(bd, 0, sizeof(struct bunzip_data)); memset(bd, 0, sizeof(struct bunzip_data));
/* Setup input buffer */ /* Setup input buffer */
bd->inbuf = inbuf; bd->inbuf = inbuf;
@ -664,6 +666,8 @@ static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
bd->dbufSize = 100000*(i-BZh0); bd->dbufSize = 100000*(i-BZh0);
bd->dbuf = large_malloc(bd->dbufSize * sizeof(int)); bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
if (!bd->dbuf)
return RETVAL_OUT_OF_MEMORY;
return RETVAL_OK; return RETVAL_OK;
} }
@ -686,7 +690,7 @@ STATIC int INIT bunzip2(unsigned char *buf, int len,
if (!outbuf) { if (!outbuf) {
error("Could not allocate output bufer"); error("Could not allocate output bufer");
return -1; return RETVAL_OUT_OF_MEMORY;
} }
if (buf) if (buf)
inbuf = buf; inbuf = buf;
@ -694,6 +698,7 @@ STATIC int INIT bunzip2(unsigned char *buf, int len,
inbuf = malloc(BZIP2_IOBUF_SIZE); inbuf = malloc(BZIP2_IOBUF_SIZE);
if (!inbuf) { if (!inbuf) {
error("Could not allocate input bufer"); error("Could not allocate input bufer");
i = RETVAL_OUT_OF_MEMORY;
goto exit_0; goto exit_0;
} }
i = start_bunzip(&bd, inbuf, len, fill); i = start_bunzip(&bd, inbuf, len, fill);
@ -720,11 +725,14 @@ STATIC int INIT bunzip2(unsigned char *buf, int len,
} else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) { } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
error("Compressed file ends unexpectedly"); error("Compressed file ends unexpectedly");
} }
if (!bd)
goto exit_1;
if (bd->dbuf) if (bd->dbuf)
large_free(bd->dbuf); large_free(bd->dbuf);
if (pos) if (pos)
*pos = bd->inbufPos; *pos = bd->inbufPos;
free(bd); free(bd);
exit_1:
if (!buf) if (!buf)
free(inbuf); free(inbuf);
exit_0: exit_0: