Skip to content

Commit

Permalink
Fix CLEAR_HASH macro to be usable as a single statement.
Browse files Browse the repository at this point in the history
As it is used in deflateParams().
  • Loading branch information
madler committed Feb 16, 2017
1 parent 8ba393e commit 38e8ce3
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@ local const config configuration_table[10] = {
* prev[] will be initialized on the fly.
*/
#define CLEAR_HASH(s) \
s->head[s->hash_size-1] = NIL; \
zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
do { \
s->head[s->hash_size-1] = NIL; \
zmemzero((Bytef *)s->head, \
(unsigned)(s->hash_size-1)*sizeof(*s->head)); \
} while (0)

/* ===========================================================================
* Slide the hash table when sliding the window down (could be avoided with 32
Expand Down

5 comments on commit 38e8ce3

@sam-github
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this fix, on https://github.com/sam-github/zlib/blob/cacf7f1d4e3d44d871b605da3b647f07d718623f/deflate.c#L603, s->head would be zeroed on both branches. Does this have no unintentional side-effects? It seems like it would deflate failure, but we haven't noticed that.

@madler
Copy link
Owner Author

@madler madler commented on 38e8ce3 Mar 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The consequence is relatively benign. It results in potentially less compression when switching from compression (level !=0), to no compression (level 0), and back to compression, when only a small amount of data was compressed at level 0 (less than 32K). What is lost is string hash information from some of the data compressed in the first portion with compression, that is not carried over to the third portion. Earlier versions of zlib threw away that information anyway, so it is only a recent enhancement to try to get some mileage out of the compression before no compression.

@sam-github
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification @madler

@chund
Copy link

@chund chund commented on 38e8ce3 Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess the do ... while(0) is not necessary and I find it confusing.
The curly braces { ... } should already do the trick.

@madler
Copy link
Owner Author

@madler madler commented on 38e8ce3 Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The do while permits following the use of the macro with a semicolon to make it a statement, allowing its use in an if statement. It is a standard construct for macros.

Please sign in to comment.