From be4db0a79add326bf06ff46b550f564288749f90 Mon Sep 17 00:00:00 2001
From: Mark Adler <madler@alumni.caltech.edu>
Date: Fri, 2 Feb 2024 13:39:10 -0800
Subject: Set returned index to NULL on an index build error in zran.c.

An attempt to use the returned pointer in deflate_index_extract()
will now return cleanly with an error. The returned pointer can
now also be used with deflate_index_free() even on error.
---
 examples/zran.c | 4 ++++
 examples/zran.h | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

(limited to 'examples')

diff --git a/examples/zran.c b/examples/zran.c
index d313595..9c3e5dc 100644
--- a/examples/zran.c
+++ b/examples/zran.c
@@ -130,6 +130,10 @@ static struct deflate_index *add_point(struct deflate_index *index, int bits,
 
 // See comments in zran.h.
 int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
+    // If this returns with an error, any attempt to use the index will cleanly
+    // return an error.
+    *built = NULL;
+
     // Set up inflation state.
     z_stream strm = {0};        // inflate engine (gets fired up later)
     unsigned char buf[CHUNK];   // input buffer
diff --git a/examples/zran.h b/examples/zran.h
index ebf780d..8a332d6 100644
--- a/examples/zran.h
+++ b/examples/zran.h
@@ -30,7 +30,7 @@ struct deflate_index {
 // the number of access points on success (>= 1), Z_MEM_ERROR for out of
 // memory, Z_BUF_ERROR for a premature end of input, Z_DATA_ERROR for a format
 // or verification error in the input file, or Z_ERRNO for a file read error.
-// On success, *built points to the resulting index.
+// On success, *built points to the resulting index, otherwise it's NULL.
 int deflate_index_build(FILE *in, off_t span, struct deflate_index **built);
 
 // Use the index to read len bytes from offset into buf. Return the number of
-- 
cgit v1.2.3-55-g6feb