From e2a5c6b6799ddfa9ca03b6f7fd13d0012e7b2977 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 22 Feb 2014 18:02:14 +0000 Subject: [PATCH] Add some assertions in sshzlib.c. gcc 4.8 compiling with -O3 gives a new warning about the access to st->pending at the top of lz77_compress, because for some reason it thinks there's an out-of-bounds array access there (or perhaps just a potential one, I'm not really sure which side -Warray-bounds is erring on). Add an assertion reassuring it that st->npending can't get bigger than the size of st->pending at the site it's complaining about, and a second one at the site where st->npending is increased (just in case my analysis of why it can't happen was wrong!). Also add a comment explaining the assertions. [originally from svn r10144] --- sshzlib.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sshzlib.c b/sshzlib.c index 05fa5772..8a64e356 100644 --- a/sshzlib.c +++ b/sshzlib.c @@ -205,9 +205,16 @@ static void lz77_compress(struct LZ77Context *ctx, struct Match defermatch, matches[MAXMATCH]; int deferchr; + assert(st->npending <= HASHCHARS); + /* * Add any pending characters from last time to the window. (We * might not be able to.) + * + * This leaves st->pending empty in the usual case (when len >= + * HASHCHARS); otherwise it leaves st->pending empty enough that + * adding all the remaining 'len' characters will not push it past + * HASHCHARS in size. */ for (i = 0; i < st->npending; i++) { unsigned char foo[HASHCHARS]; @@ -334,6 +341,7 @@ static void lz77_compress(struct LZ77Context *ctx, if (len >= HASHCHARS) { lz77_advance(st, *data, lz77_hash(data)); } else { + assert(st->npending < HASHCHARS); st->pending[st->npending++] = *data; } data++;