unlzma: fix memory leak (Pascal Bellard)

This commit is contained in:
Denis Vlasenko 2008-06-27 15:48:45 +00:00
parent ac2b50ebea
commit 2bbdda09df

View File

@ -45,7 +45,7 @@ typedef struct {
/* Called twice: once at startup and once in rc_normalize() */ /* Called twice: once at startup and once in rc_normalize() */
static void rc_read(rc_t * rc) static void rc_read(rc_t *rc)
{ {
int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE); int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
if (buffer_size <= 0) if (buffer_size <= 0)
@ -58,9 +58,9 @@ static void rc_read(rc_t * rc)
static rc_t* rc_init(int fd) /*, int buffer_size) */ static rc_t* rc_init(int fd) /*, int buffer_size) */
{ {
int i; int i;
rc_t* rc; rc_t *rc;
rc = xmalloc(sizeof(rc_t) + RC_BUFFER_SIZE); rc = xmalloc(sizeof(*rc) + RC_BUFFER_SIZE);
rc->fd = fd; rc->fd = fd;
/* rc->buffer_size = buffer_size; */ /* rc->buffer_size = buffer_size; */
@ -78,21 +78,20 @@ static rc_t* rc_init(int fd) /*, int buffer_size) */
} }
/* Called once */ /* Called once */
static ALWAYS_INLINE void rc_free(rc_t * rc) static ALWAYS_INLINE void rc_free(rc_t *rc)
{ {
if (ENABLE_FEATURE_CLEAN_UP) free(rc);
free(rc);
} }
/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */ /* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
static void rc_do_normalize(rc_t * rc) static void rc_do_normalize(rc_t *rc)
{ {
if (rc->ptr >= rc->buffer_end) if (rc->ptr >= rc->buffer_end)
rc_read(rc); rc_read(rc);
rc->range <<= 8; rc->range <<= 8;
rc->code = (rc->code << 8) | *rc->ptr++; rc->code = (rc->code << 8) | *rc->ptr++;
} }
static ALWAYS_INLINE void rc_normalize(rc_t * rc) static ALWAYS_INLINE void rc_normalize(rc_t *rc)
{ {
if (rc->range < (1 << RC_TOP_BITS)) { if (rc->range < (1 << RC_TOP_BITS)) {
rc_do_normalize(rc); rc_do_normalize(rc);
@ -105,25 +104,25 @@ static ALWAYS_INLINE void rc_normalize(rc_t * rc)
* Thus rc_is_bit_0 is always inlined, and rc_is_bit_0_helper is inlined * Thus rc_is_bit_0 is always inlined, and rc_is_bit_0_helper is inlined
* only if we compile for speed. * only if we compile for speed.
*/ */
static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p) static speed_inline uint32_t rc_is_bit_0_helper(rc_t *rc, uint16_t *p)
{ {
rc_normalize(rc); rc_normalize(rc);
rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS); rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
return rc->bound; return rc->bound;
} }
static ALWAYS_INLINE int rc_is_bit_0(rc_t * rc, uint16_t * p) static ALWAYS_INLINE int rc_is_bit_0(rc_t *rc, uint16_t *p)
{ {
uint32_t t = rc_is_bit_0_helper(rc, p); uint32_t t = rc_is_bit_0_helper(rc, p);
return rc->code < t; return rc->code < t;
} }
/* Called ~10 times, but very small, thus inlined */ /* Called ~10 times, but very small, thus inlined */
static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p) static speed_inline void rc_update_bit_0(rc_t *rc, uint16_t *p)
{ {
rc->range = rc->bound; rc->range = rc->bound;
*p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS; *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
} }
static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p) static speed_inline void rc_update_bit_1(rc_t *rc, uint16_t *p)
{ {
rc->range -= rc->bound; rc->range -= rc->bound;
rc->code -= rc->bound; rc->code -= rc->bound;
@ -131,7 +130,7 @@ static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p)
} }
/* Called 4 times in unlzma loop */ /* Called 4 times in unlzma loop */
static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol) static int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
{ {
if (rc_is_bit_0(rc, p)) { if (rc_is_bit_0(rc, p)) {
rc_update_bit_0(rc, p); rc_update_bit_0(rc, p);
@ -145,7 +144,7 @@ static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol)
} }
/* Called once */ /* Called once */
static ALWAYS_INLINE int rc_direct_bit(rc_t * rc) static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
{ {
rc_normalize(rc); rc_normalize(rc);
rc->range >>= 1; rc->range >>= 1;
@ -158,7 +157,7 @@ static ALWAYS_INLINE int rc_direct_bit(rc_t * rc)
/* Called twice */ /* Called twice */
static speed_inline void static speed_inline void
rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol) rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
{ {
int i = num_levels; int i = num_levels;
@ -489,12 +488,16 @@ unpack_lzma_stream(int src_fd, int dst_fd)
} }
} }
if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) { {
SKIP_DESKTOP(int total_written = 0; /* success */)
USE_DESKTOP(total_written += buffer_pos;)
if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) {
bad: bad:
total_written = -1; /* failure */
}
rc_free(rc); rc_free(rc);
return -1; free(p);
free(buffer);
return total_written;
} }
rc_free(rc);
USE_DESKTOP(total_written += buffer_pos;)
return USE_DESKTOP(total_written) + 0;
} }