From edd9e9fe80a08770ad8e66bb81c108b859a60b81 Mon Sep 17 00:00:00 2001 From: tabudz Date: Tue, 25 Feb 2025 14:50:03 +0800 Subject: [PATCH] Remove offset pointer optimization in inftrees.c. inftrees.c was subtracting an offset from a pointer to an array, in order to provide a pointer that allowed indexing starting at the offset. This is not compliant with the C standard, for which the behavior of a pointer decremented before its allocated memory is undefined. Per the recommendation of a security audit of the zlib code by Trail of Bits and TrustInSoft, in support of the Mozilla Foundation, this tiny optimization was removed, in order to avoid the possibility of undefined behavior. --- modules/juce_core/zip/zlib/inftrees.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/modules/juce_core/zip/zlib/inftrees.c b/modules/juce_core/zip/zlib/inftrees.c index dfc0aa73e021..4cb813ec98b6 100644 --- a/modules/juce_core/zip/zlib/inftrees.c +++ b/modules/juce_core/zip/zlib/inftrees.c @@ -53,7 +53,7 @@ int inflate_table (codetype type, code FAR *next; /* next available space in table */ const unsigned short FAR *base; /* base value table to use */ const unsigned short FAR *extra; /* extra bits table to use */ - int end; /* use base and extra for symbol > end */ + unsigned match; /* use base and extra for symbol >= match */ unsigned short count[MAXBITS+1]; /* number of codes of each length */ unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ static const unsigned short lbase[31] = { /* Length codes 257..285 base */ @@ -181,19 +181,17 @@ int inflate_table (codetype type, switch (type) { case CODES: base = extra = work; /* dummy value--not used */ - end = 19; + match = 20; break; case LENS: base = lbase; - base -= 257; extra = lext; - extra -= 257; - end = 256; + match = 257; break; default: /* DISTS */ base = dbase; extra = dext; - end = -1; + match = 0; } /* initialize state for loop */ @@ -215,13 +213,13 @@ int inflate_table (codetype type, for (;;) { /* create table entry */ thisx.bits = (unsigned char)(len - drop); - if ((int)(work[sym]) < end) { + if (work[sym] + 1 < match) { thisx.op = (unsigned char)0; thisx.val = work[sym]; } - else if ((int)(work[sym]) > end) { - thisx.op = (unsigned char)(extra[work[sym]]); - thisx.val = base[work[sym]]; + else if (work[sym] >= match) { + thisx.op = (unsigned char)(extra[work[sym] - match]); + thisx.val = base[work[sym] - match]; } else { thisx.op = (unsigned char)(32 + 64); /* end of block */