From: Hans Kristian Rosbach Date: Fri, 11 Feb 2022 11:08:43 +0000 (+0100) Subject: Simplify version and struct size checking, and ensure we do it the same way everywhere. X-Git-Tag: 2.1.0-beta1~231 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=28b029c7261e2bff16d66b3cc138241db5e837c2;p=thirdparty%2Fzlib-ng.git Simplify version and struct size checking, and ensure we do it the same way everywhere. --- diff --git a/deflate.c b/deflate.c index 369b17b5b..878033355 100644 --- a/deflate.c +++ b/deflate.c @@ -325,18 +325,16 @@ int32_t Z_EXPORT PREFIX(deflateInit)(PREFIX3(stream) *strm, int32_t level) { /* Function used by zlib.h and zlib-ng version 2.0 macros */ int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) { - if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != sizeof(PREFIX3(stream))) { + if (CHECK_VER_STSIZE(version, stream_size)) return Z_VERSION_ERROR; - } return PREFIX(deflateInit2)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); } /* Function used by zlib.h and zlib-ng version 2.0 macros */ int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits, int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) { - if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != sizeof(PREFIX3(stream))) { + if (CHECK_VER_STSIZE(version, stream_size)) return Z_VERSION_ERROR; - } return PREFIX(deflateInit2)(strm, level, method, windowBits, memLevel, strategy); } diff --git a/infback.c b/infback.c index 800242572..dd549a89f 100644 --- a/infback.c +++ b/infback.c @@ -62,9 +62,8 @@ int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t wi /* Function used by zlib.h and zlib-ng version 2.0 macros */ int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window, const char *version, int32_t stream_size) { - if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream)))) { + if (CHECK_VER_STSIZE(version, stream_size)) return Z_VERSION_ERROR; - } return PREFIX(inflateBackInit)(strm, windowBits, window); } diff --git a/inflate.c b/inflate.c index 68531ab9a..619214823 100644 --- a/inflate.c +++ b/inflate.c @@ -175,14 +175,14 @@ int32_t Z_EXPORT PREFIX(inflateInit)(PREFIX3(stream) *strm) { /* Function used by zlib.h and zlib-ng version 2.0 macros */ int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) { - if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream)))) + if (CHECK_VER_STSIZE(version, stream_size)) return Z_VERSION_ERROR; return PREFIX(inflateInit2)(strm, DEF_WBITS); } /* Function used by zlib.h and zlib-ng version 2.0 macros */ int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) { - if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream)))) + if (CHECK_VER_STSIZE(version, stream_size)) return Z_VERSION_ERROR; return PREFIX(inflateInit2)(strm, windowBits); } diff --git a/zutil.h b/zutil.h index f70eb4bdb..53c7cdf14 100644 --- a/zutil.h +++ b/zutil.h @@ -120,6 +120,10 @@ extern z_const char * const PREFIX(z_errmsg)[10]; /* indexed by 2-zlib_error */ # define OS_CODE 3 /* assume Unix */ #endif + /* macros */ + +#define CHECK_VER_STSIZE(_ver,_stsize) ((_ver) == NULL || (_ver)[0] != PREFIX2(VERSION)[0] || (_stsize) != (int32_t)sizeof(PREFIX3(stream))) + /* memory allocation functions */ void Z_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size);