mpi reminded me of this diff.

On Tue, May 14, 2019 at 01:51:05PM +0000, Miod Vallat wrote:
> 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.

Below is a rebased version. Having just gone through the update dance, I
don't think this will make future updates significantly harder (there is
also the question whether there will ever be another one).

The diff is easy to check and works for me.

Index: net/ppp-deflate.c
===================================================================
RCS file: /cvs/src/sys/net/ppp-deflate.c,v
retrieving revision 1.16
diff -u -p -r1.16 ppp-deflate.c
--- net/ppp-deflate.c   5 Mar 2021 09:21:08 -0000       1.16
+++ net/ppp-deflate.c   9 Jul 2021 06:42:33 -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);
@@ -133,9 +133,9 @@ zcalloc(void *notused, u_int items, u_in
 }
 
 void
-zcfree(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/deflate.c
===================================================================
RCS file: /cvs/src/sys/lib/libz/deflate.c,v
retrieving revision 1.4
diff -u -p -r1.4 deflate.c
--- lib/libz/deflate.c  4 Jul 2021 14:24:49 -0000       1.4
+++ lib/libz/deflate.c  9 Jul 2021 06:36:17 -0000
@@ -1080,12 +1080,12 @@ int ZEXPORT deflateEnd (strm)
     status = strm->state->status;
 
     /* 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(Pos));
 
-    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: /cvs/src/sys/lib/libz/infback.c,v
retrieving revision 1.7
diff -u -p -r1.7 infback.c
--- lib/libz/infback.c  4 Jul 2021 14:24:49 -0000       1.7
+++ lib/libz/infback.c  9 Jul 2021 06:36:23 -0000
@@ -684,7 +684,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: /cvs/src/sys/lib/libz/inflate.c,v
retrieving revision 1.16
diff -u -p -r1.16 inflate.c
--- lib/libz/inflate.c  4 Jul 2021 14:24:49 -0000       1.16
+++ lib/libz/inflate.c  9 Jul 2021 06:32:09 -0000
@@ -183,7 +183,7 @@ int windowBits;
     if (windowBits && (windowBits < 8 || windowBits > 15))
         return Z_STREAM_ERROR;
     if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
-        ZFREE(strm, state->window);
+        ZFREE(strm, state->window, 1U << state->wbits);
         state->window = Z_NULL;
     }
 
@@ -231,7 +231,7 @@ int stream_size;
     state->mode = HEAD;     /* to pass state test in inflateReset2() */
     ret = inflateReset2(strm, windowBits);
     if (ret != Z_OK) {
-        ZFREE(strm, state);
+        ZFREE(strm, state, sizeof(struct inflate_state));
         strm->state = Z_NULL;
     }
     return ret;
@@ -1368,8 +1368,8 @@ z_streamp strm;
     if (inflateStateCheck(strm))
         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;
@@ -1568,7 +1568,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: /cvs/src/sys/lib/libz/zlib.h,v
retrieving revision 1.13
diff -u -p -r1.13 zlib.h
--- lib/libz/zlib.h     4 Jul 2021 17:41:23 -0000       1.13
+++ lib/libz/zlib.h     9 Jul 2021 06:45:42 -0000
@@ -81,7 +81,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: /cvs/src/sys/lib/libz/zopenbsd.c,v
retrieving revision 1.9
diff -u -p -r1.9 zopenbsd.c
--- lib/libz/zopenbsd.c 4 Jul 2021 17:51:20 -0000       1.9
+++ lib/libz/zopenbsd.c 9 Jul 2021 06:38:39 -0000
@@ -29,7 +29,7 @@ zcalloc(void *notused, u_int items, u_in
 }
 
 void
-zcfree(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: /cvs/src/sys/lib/libz/zutil.h,v
retrieving revision 1.21
diff -u -p -r1.21 zutil.h
--- lib/libz/zutil.h    8 Jul 2021 20:02:42 -0000       1.21
+++ lib/libz/zutil.h    9 Jul 2021 06:49:58 -0000
@@ -270,13 +270,14 @@ extern z_const char * const z_errmsg[10]
 #ifndef Z_SOLO
    voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
                                     unsigned size));
-   void ZLIB_INTERNAL zcfree  OF((voidpf opaque, voidpf ptr));
+   void ZLIB_INTERNAL zcfree  OF((voidpf opaque, voidpf ptr, unsigned size));
 #endif
 
 #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(s, p, size) ZFREE(s, p, size)
 
 /* Reverse the bytes in a 32-bit value */
 #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \

Reply via email to