aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2025-05-16 10:21:11 -0700
committerMark Adler <git@madler.net>2025-12-06 17:39:44 -0800
commitd0fc110cc31a30132bf878a86cf530e83d34d974 (patch)
treedb629fd39df7b7e7994795147b04f6bf5cdeaa6d
parenta2b61271a362d4881b9f67fb92455f4fbbab06df (diff)
downloadzlib-d0fc110cc31a30132bf878a86cf530e83d34d974.tar.gz
zlib-d0fc110cc31a30132bf878a86cf530e83d34d974.tar.bz2
zlib-d0fc110cc31a30132bf878a86cf530e83d34d974.zip
Remove redundant frees of point list on error in examples/zran.c.
Also clean out the point list contents when freed, even though the structure itself is freed, in case someone tries to free it again.
-rw-r--r--examples/zran.c32
-rw-r--r--examples/zran.h6
2 files changed, 17 insertions, 21 deletions
diff --git a/examples/zran.c b/examples/zran.c
index 6ce75dd..731d34d 100644
--- a/examples/zran.c
+++ b/examples/zran.c
@@ -1,7 +1,7 @@
1/* zran.c -- example of deflate stream indexing and random access 1/* zran.c -- example of deflate stream indexing and random access
2 * Copyright (C) 2005, 2012, 2018, 2023, 2024 Mark Adler 2 * Copyright (C) 2005, 2012, 2018, 2023, 2024, 2025 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 * Version 1.6 2 Aug 2024 Mark Adler */ 4 * Version 1.7 16 May 2025 Mark Adler */
5 5
6/* Version History: 6/* Version History:
7 1.0 29 May 2005 First version 7 1.0 29 May 2005 First version
@@ -19,6 +19,8 @@
19 Provide a reusable inflate engine in the index 19 Provide a reusable inflate engine in the index
20 Allocate the dictionaries to reduce memory usage 20 Allocate the dictionaries to reduce memory usage
21 1.6 2 Aug 2024 Remove unneeded dependency on limits.h 21 1.6 2 Aug 2024 Remove unneeded dependency on limits.h
22 1.7 16 May 2025 Remove redundant frees of point list on error
23 Clean out point list structure when freed
22 */ 24 */
23 25
24// Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary() 26// Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
@@ -71,19 +73,19 @@
71// See comments in zran.h. 73// See comments in zran.h.
72void deflate_index_free(struct deflate_index *index) { 74void deflate_index_free(struct deflate_index *index) {
73 if (index != NULL) { 75 if (index != NULL) {
74 size_t i = index->have; 76 while (index->have)
75 while (i) 77 free(index->list[--index->have].window);
76 free(index->list[--i].window);
77 free(index->list); 78 free(index->list);
79 index->list = NULL;
78 inflateEnd(&index->strm); 80 inflateEnd(&index->strm);
79 free(index); 81 free(index);
80 } 82 }
81} 83}
82 84
83// Add an access point to the list. If out of memory, deallocate the existing 85// Add an access point to the list. If out of memory, return NULL. index->mode
84// list and return NULL. index->mode is temporarily the allocated number of 86// is temporarily the allocated number of access points, until it is time for
85// access points, until it is time for deflate_index_build() to return. Then 87// deflate_index_build() to return. Then index->mode is set to the mode of
86// index->mode is set to the mode of inflation. 88// inflation.
87static struct deflate_index *add_point(struct deflate_index *index, off_t in, 89static struct deflate_index *add_point(struct deflate_index *index, off_t in,
88 off_t out, off_t beg, 90 off_t out, off_t beg,
89 unsigned char *window) { 91 unsigned char *window) {
@@ -91,29 +93,23 @@ static struct deflate_index *add_point(struct deflate_index *index, off_t in,
91 // The list is full. Make it bigger. 93 // The list is full. Make it bigger.
92 index->mode = index->mode ? index->mode << 1 : 8; 94 index->mode = index->mode ? index->mode << 1 : 8;
93 point_t *next = realloc(index->list, sizeof(point_t) * index->mode); 95 point_t *next = realloc(index->list, sizeof(point_t) * index->mode);
94 if (next == NULL) { 96 if (next == NULL)
95 deflate_index_free(index);
96 return NULL; 97 return NULL;
97 }
98 index->list = next; 98 index->list = next;
99 } 99 }
100 100
101 // Fill in the access point and increment how many we have. 101 // Fill in the access point and increment how many we have.
102 point_t *next = (point_t *)(index->list) + index->have++; 102 point_t *next = (point_t *)(index->list) + index->have++;
103 if (index->have < 0) { 103 if (index->have < 0)
104 // Overflowed the int! 104 // Overflowed the int!
105 deflate_index_free(index);
106 return NULL; 105 return NULL;
107 }
108 next->out = out; 106 next->out = out;
109 next->in = in; 107 next->in = in;
110 next->bits = index->strm.data_type & 7; 108 next->bits = index->strm.data_type & 7;
111 next->dict = out - beg > WINSIZE ? WINSIZE : (unsigned)(out - beg); 109 next->dict = out - beg > WINSIZE ? WINSIZE : (unsigned)(out - beg);
112 next->window = malloc(next->dict); 110 next->window = malloc(next->dict);
113 if (next->window == NULL) { 111 if (next->window == NULL)
114 deflate_index_free(index);
115 return NULL; 112 return NULL;
116 }
117 unsigned recent = WINSIZE - index->strm.avail_out; 113 unsigned recent = WINSIZE - index->strm.avail_out;
118 unsigned copy = recent > next->dict ? next->dict : recent; 114 unsigned copy = recent > next->dict ? next->dict : recent;
119 memcpy(next->window + next->dict - copy, window + recent - copy, copy); 115 memcpy(next->window + next->dict - copy, window + recent - copy, copy);
diff --git a/examples/zran.h b/examples/zran.h
index 5c6e643..16a84fe 100644
--- a/examples/zran.h
+++ b/examples/zran.h
@@ -1,7 +1,7 @@
1/* zran.h -- example of deflated stream indexing and random access 1/* zran.h -- example of deflate stream indexing and random access
2 * Copyright (C) 2005, 2012, 2018, 2023, 2024 Mark Adler 2 * Copyright (C) 2005, 2012, 2018, 2023, 2024, 2025 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 * Version 1.5 4 Feb 2024 Mark Adler */ 4 * Version 1.7 16 May 2025 Mark Adler */
5 5
6#include <stdio.h> 6#include <stdio.h>
7#include "zlib.h" 7#include "zlib.h"