11cb0ef41Sopenharmony_ciFrom 8304bdda5293ffd5b3efce8e4f54904b387029d6 Mon Sep 17 00:00:00 2001
21cb0ef41Sopenharmony_ciFrom: Hans Wennborg <hans@chromium.org>
31cb0ef41Sopenharmony_ciDate: Wed, 23 Sep 2020 16:36:38 +0200
41cb0ef41Sopenharmony_ciSubject: [PATCH] Avoid crashing in check_match when prev_match == -1
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciprev_match can be set to -1 after sliding the window. In that case, the
71cb0ef41Sopenharmony_ciwindow has slid past the first byte of the last match, which means it
81cb0ef41Sopenharmony_cicannot be compared in check_match.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciThis would cause zlib to crash on some inputs to deflate when built
111cb0ef41Sopenharmony_ciwith ZLIB_DEBUG enabled.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciCheck for this situation and avoid crashing by not trying to compare
141cb0ef41Sopenharmony_cithe first byte.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciBug: 1113142
171cb0ef41Sopenharmony_ci---
181cb0ef41Sopenharmony_ci third_party/zlib/deflate.c | 8 +++++++-
191cb0ef41Sopenharmony_ci 1 file changed, 7 insertions(+), 1 deletion(-)
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cidiff --git a/third_party/zlib/deflate.c b/third_party/zlib/deflate.c
221cb0ef41Sopenharmony_ciindex cfdd2f46b230..d70732ec6fc2 100644
231cb0ef41Sopenharmony_ci--- a/third_party/zlib/deflate.c
241cb0ef41Sopenharmony_ci+++ b/third_party/zlib/deflate.c
251cb0ef41Sopenharmony_ci@@ -2060,7 +2060,13 @@ local block_state deflate_slow(s, flush)
261cb0ef41Sopenharmony_ci             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
271cb0ef41Sopenharmony_ci             /* Do not insert strings in hash table beyond this. */
281cb0ef41Sopenharmony_ci 
291cb0ef41Sopenharmony_ci-            check_match(s, s->strstart-1, s->prev_match, s->prev_length);
301cb0ef41Sopenharmony_ci+            if (s->prev_match == -1) {
311cb0ef41Sopenharmony_ci+                /* The window has slid one byte past the previous match,
321cb0ef41Sopenharmony_ci+                 * so the first byte cannot be compared. */
331cb0ef41Sopenharmony_ci+                check_match(s, s->strstart, s->prev_match+1, s->prev_length-1);
341cb0ef41Sopenharmony_ci+            } else {
351cb0ef41Sopenharmony_ci+                check_match(s, s->strstart-1, s->prev_match, s->prev_length);
361cb0ef41Sopenharmony_ci+            }
371cb0ef41Sopenharmony_ci 
381cb0ef41Sopenharmony_ci             _tr_tally_dist(s, s->strstart -1 - s->prev_match,
391cb0ef41Sopenharmony_ci                            s->prev_length - MIN_MATCH, bflush);
401cb0ef41Sopenharmony_ci-- 
411cb0ef41Sopenharmony_ci2.28.0.681.g6f77f65b4e-goog
421cb0ef41Sopenharmony_ci
43