From: Hans Kristian Rosbach Date: Sat, 12 Sep 2020 21:18:13 +0000 (+0200) Subject: Allocate gzlib/gzread/gzwrite structs and in/out buffers using zng_alloc X-Git-Tag: 1.9.9-b1~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a14ec1a53ad28277641b1dec5fe8f4a5568fb040;p=thirdparty%2Fzlib-ng.git Allocate gzlib/gzread/gzwrite structs and in/out buffers using zng_alloc instead of malloc, this also enforces data alignment. --- diff --git a/gzlib.c b/gzlib.c index c965130f..de99174f 100644 --- a/gzlib.c +++ b/gzlib.c @@ -4,6 +4,7 @@ */ #include "zbuild.h" +#include "zutil_p.h" #include "gzguts.h" #if defined(_WIN32) @@ -53,7 +54,7 @@ static gzFile gz_open(const void *path, int fd, const char *mode) { return NULL; /* allocate gzFile structure to return */ - state = (gz_state *)malloc(sizeof(gz_state)); + state = (gz_state *)zng_alloc(sizeof(gz_state)); if (state == NULL) return NULL; state->size = 0; /* no buffers allocated yet */ @@ -82,7 +83,7 @@ static gzFile gz_open(const void *path, int fd, const char *mode) { break; #endif case '+': /* can't read and write at the same time */ - free(state); + zng_free(state); return NULL; case 'b': /* ignore -- will request binary anyway */ break; @@ -120,14 +121,14 @@ static gzFile gz_open(const void *path, int fd, const char *mode) { /* must provide an "r", "w", or "a" */ if (state->mode == GZ_NONE) { - free(state); + zng_free(state); return NULL; } /* can't force transparent read */ if (state->mode == GZ_READ) { if (state->direct) { - free(state); + zng_free(state); return NULL; } state->direct = 1; /* for empty file */ @@ -144,7 +145,7 @@ static gzFile gz_open(const void *path, int fd, const char *mode) { len = strlen((const char *)path); state->path = (char *)malloc(len + 1); if (state->path == NULL) { - free(state); + zng_free(state); return NULL; } #ifdef WIDECHAR @@ -189,7 +190,7 @@ static gzFile gz_open(const void *path, int fd, const char *mode) { open((const char *)path, oflag, 0666)); if (state->fd == -1) { free(state->path); - free(state); + zng_free(state); return NULL; } if (state->mode == GZ_APPEND) { diff --git a/gzread.c b/gzread.c index 7d05f75a..e487d99b 100644 --- a/gzread.c +++ b/gzread.c @@ -4,6 +4,7 @@ */ #include "zbuild.h" +#include "zutil_p.h" #include "gzguts.h" /* Local functions */ @@ -83,11 +84,11 @@ static int gz_look(gz_state *state) { /* allocate read buffers and inflate memory */ if (state->size == 0) { /* allocate buffers */ - state->in = (unsigned char *)malloc(state->want); - state->out = (unsigned char *)malloc(state->want << 1); + state->in = (unsigned char *)zng_alloc(state->want); + state->out = (unsigned char *)zng_alloc(state->want << 1); if (state->in == NULL || state->out == NULL) { - free(state->out); - free(state->in); + zng_free(state->out); + zng_free(state->in); gz_error(state, Z_MEM_ERROR, "out of memory"); return -1; } @@ -100,8 +101,8 @@ static int gz_look(gz_state *state) { state->strm.avail_in = 0; state->strm.next_in = NULL; if (PREFIX(inflateInit2)(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */ - free(state->out); - free(state->in); + zng_free(state->out); + zng_free(state->in); state->size = 0; gz_error(state, Z_MEM_ERROR, "out of memory"); return -1; @@ -589,13 +590,13 @@ int Z_EXPORT PREFIX(gzclose_r)(gzFile file) { /* free memory and close file */ if (state->size) { PREFIX(inflateEnd)(&(state->strm)); - free(state->out); - free(state->in); + zng_free(state->out); + zng_free(state->in); } err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK; gz_error(state, Z_OK, NULL); free(state->path); ret = close(state->fd); - free(state); + zng_free(state); return ret ? Z_ERRNO : err; } diff --git a/gzwrite.c b/gzwrite.c index 1e556878..c4e178f9 100644 --- a/gzwrite.c +++ b/gzwrite.c @@ -4,6 +4,7 @@ */ #include "zbuild.h" +#include "zutil_p.h" #include #include "gzguts.h" @@ -21,7 +22,7 @@ static int gz_init(gz_state *state) { PREFIX3(stream) *strm = &(state->strm); /* allocate input buffer (double size for gzprintf) */ - state->in = (unsigned char *)malloc(state->want << 1); + state->in = (unsigned char *)zng_alloc(state->want << 1); if (state->in == NULL) { gz_error(state, Z_MEM_ERROR, "out of memory"); return -1; @@ -31,9 +32,9 @@ static int gz_init(gz_state *state) { /* only need output buffer and deflate state if compressing */ if (!state->direct) { /* allocate output buffer */ - state->out = (unsigned char *)malloc(state->want); + state->out = (unsigned char *)zng_alloc(state->want); if (state->out == NULL) { - free(state->in); + zng_free(state->in); gz_error(state, Z_MEM_ERROR, "out of memory"); return -1; } @@ -44,8 +45,8 @@ static int gz_init(gz_state *state) { strm->opaque = NULL; ret = PREFIX(deflateInit2)(strm, state->level, Z_DEFLATED, MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); if (ret != Z_OK) { - free(state->out); - free(state->in); + zng_free(state->out); + zng_free(state->in); gz_error(state, Z_MEM_ERROR, "out of memory"); return -1; } @@ -512,14 +513,14 @@ int Z_EXPORT PREFIX(gzclose_w)(gzFile file) { if (state->size) { if (!state->direct) { (void)PREFIX(deflateEnd)(&(state->strm)); - free(state->out); + zng_free(state->out); } - free(state->in); + zng_free(state->in); } gz_error(state, Z_OK, NULL); free(state->path); if (close(state->fd) == -1) ret = Z_ERRNO; - free(state); + zng_free(state); return ret; }