This tries to keep diffability against upstream, hence a questionable
choice of the size type for zcfree() - but all sizes should fit in 32
bits anyway.

Since all zcfree routines used in the tree cope with NULL arguments
(including the various alloc.c used by the boot blocks), I have
simplified TRY_FREE to compensate for the growth.

Index: lib/libz/deflate.c
===================================================================
RCS file: /OpenBSD/src/sys/lib/libz/deflate.c,v
retrieving revision 1.3
diff -u -p -r1.3 deflate.c
--- lib/libz/deflate.c  14 Mar 2016 23:08:06 -0000      1.3
+++ lib/libz/deflate.c  14 May 2019 13:47:03 -0000
@@ -873,12 +873,12 @@ int ZEXPORT deflateEnd (strm)
     }
 
     /* Deallocate in reverse order of allocations: */
-    TRY_FREE(strm, strm->state->pending_buf);
-    TRY_FREE(strm, strm->state->head);
-    TRY_FREE(strm, strm->state->prev);
-    TRY_FREE(strm, strm->state->window);
+    TRY_FREE(strm, strm->state->pending_buf, strm->state->pending_buf_size);
+    TRY_FREE(strm, strm->state->head, strm->state->hash_size * sizeof(Pos));
+    TRY_FREE(strm, strm->state->prev, strm->state->w_size * sizeof(Pos));
+    TRY_FREE(strm, strm->state->window, strm->state->w_size * 2 * 
sizeof(Byte));
 
-    ZFREE(strm, strm->state);
+    ZFREE(strm, strm->state, sizeof(deflate_state));
     strm->state = Z_NULL;
 
     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
Index: lib/libz/infback.c
===================================================================
RCS file: /OpenBSD/src/sys/lib/libz/infback.c,v
retrieving revision 1.5
diff -u -p -r1.5 infback.c
--- lib/libz/infback.c  20 Jul 2005 15:56:45 -0000      1.5
+++ lib/libz/infback.c  14 May 2019 13:47:03 -0000
@@ -630,7 +630,7 @@ z_streamp strm;
 {
     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
         return Z_STREAM_ERROR;
-    ZFREE(strm, strm->state);
+    ZFREE(strm, strm->state, sizeof(struct inflate_state));
     strm->state = Z_NULL;
     Tracev((stderr, "inflate: end\n"));
     return Z_OK;
Index: lib/libz/inflate.c
===================================================================
RCS file: /OpenBSD/src/sys/lib/libz/inflate.c,v
retrieving revision 1.14
diff -u -p -r1.14 inflate.c
--- lib/libz/inflate.c  23 Mar 2016 19:39:48 -0000      1.14
+++ lib/libz/inflate.c  14 May 2019 13:47:03 -0000
@@ -176,7 +176,7 @@ int stream_size;
 #endif
     }
     if (windowBits < 8 || windowBits > 15) {
-        ZFREE(strm, state);
+        ZFREE(strm, state, sizeof(struct inflate_state));
         strm->state = Z_NULL;
         return Z_STREAM_ERROR;
     }
@@ -1238,8 +1238,8 @@ z_streamp strm;
     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
         return Z_STREAM_ERROR;
     state = (struct inflate_state FAR *)strm->state;
-    if (state->window != Z_NULL) ZFREE(strm, state->window);
-    ZFREE(strm, strm->state);
+    if (state->window != Z_NULL) ZFREE(strm, state->window, 1U << 
state->wbits);
+    ZFREE(strm, strm->state, sizeof(struct inflate_state));
     strm->state = Z_NULL;
     Tracev((stderr, "inflate: end\n"));
     return Z_OK;
@@ -1423,7 +1423,7 @@ z_streamp source;
         window = (unsigned char FAR *)
                  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
         if (window == Z_NULL) {
-            ZFREE(source, copy);
+            ZFREE(source, copy, sizeof(struct inflate_state));
             return Z_MEM_ERROR;
         }
     }
Index: lib/libz/zlib.h
===================================================================
RCS file: /OpenBSD/src/sys/lib/libz/zlib.h,v
retrieving revision 1.11
diff -u -p -r1.11 zlib.h
--- lib/libz/zlib.h     12 May 2019 15:56:56 -0000      1.11
+++ lib/libz/zlib.h     14 May 2019 13:47:04 -0000
@@ -76,7 +76,7 @@ extern "C" {
 */
 
 typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
-typedef void   (*free_func)  OF((voidpf opaque, voidpf address));
+typedef void   (*free_func)  OF((voidpf opaque, voidpf address, uInt size));
 
 struct internal_state;
 
Index: lib/libz/zopenbsd.c
===================================================================
RCS file: /OpenBSD/src/sys/lib/libz/zopenbsd.c,v
retrieving revision 1.6
diff -u -p -r1.6 zopenbsd.c
--- lib/libz/zopenbsd.c 14 Mar 2015 03:38:51 -0000      1.6
+++ lib/libz/zopenbsd.c 14 May 2019 13:47:04 -0000
@@ -5,17 +5,13 @@
  * Space allocation and freeing routines for use by zlib routines.
  */
 void *
-zcalloc(notused, items, size)
-    void *notused;
-    u_int items, size;
+zcalloc(void *notused, u_int items, u_int size)
 {
     return mallocarray(items, size, M_DEVBUF, M_NOWAIT);
 }
 
 void
-zcfree(notused, ptr)
-    void *notused;
-    void *ptr;
+zcfree(void *notused, void *ptr, u_int size)
 {
-    free(ptr, M_DEVBUF, 0);
+    free(ptr, M_DEVBUF, size);
 }
Index: lib/libz/zutil.h
===================================================================
RCS file: /OpenBSD/src/sys/lib/libz/zutil.h,v
retrieving revision 1.18
diff -u -p -r1.18 zutil.h
--- lib/libz/zutil.h    7 Jul 2011 02:57:24 -0000       1.18
+++ lib/libz/zutil.h    14 May 2019 13:47:04 -0000
@@ -271,11 +271,12 @@ extern const char * const z_errmsg[10]; 
 
 
 voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
-void   zcfree  OF((voidpf opaque, voidpf ptr));
+void   zcfree  OF((voidpf opaque, voidpf ptr, unsigned size));
 
 #define ZALLOC(strm, items, size) \
            (*((strm)->zalloc))((strm)->opaque, (items), (size))
-#define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
-#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
+#define ZFREE(strm, addr, size) \
+          (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
+#define TRY_FREE(strm, addr, size) ZFREE(strm, addr, size)
 
 #endif /* ZUTIL_H */
Index: net/ppp-deflate.c
===================================================================
RCS file: /OpenBSD/src/sys/net/ppp-deflate.c,v
retrieving revision 1.15
diff -u -p -r1.15 ppp-deflate.c
--- net/ppp-deflate.c   9 Nov 2018 14:14:31 -0000       1.15
+++ net/ppp-deflate.c   14 May 2019 13:47:04 -0000
@@ -66,7 +66,7 @@ struct deflate_state {
 #define DEFLATE_OVHD   2               /* Deflate overhead/packet */
 
 static void    *zcalloc(void *, u_int items, u_int size);
-static void    zcfree(void *, void *ptr);
+static void    zcfree(void *, void *ptr, u_int size);
 static void    *z_comp_alloc(u_char *options, int opt_len);
 static void    *z_decomp_alloc(u_char *options, int opt_len);
 static void    z_comp_free(void *state);
@@ -124,22 +124,15 @@ struct compressor ppp_deflate_draft = {
  * Space allocation and freeing routines for use by zlib routines.
  */
 void *
-zcalloc(notused, items, size)
-    void *notused;
-    u_int items, size;
+zcalloc(void *notused, u_int items, u_int size)
 {
-    void *ptr;
-
-    ptr = mallocarray(items, size, M_DEVBUF, M_NOWAIT);
-    return ptr;
+    return mallocarray(items, size, M_DEVBUF, M_NOWAIT);
 }
 
 void
-zcfree(notused, ptr)
-    void *notused;
-    void *ptr;
+zcfree(void *notused, void *ptr, u_int size)
 {
-    free(ptr, M_DEVBUF, 0);
+    free(ptr, M_DEVBUF, size);
 }
 
 /*

Reply via email to