-
Notifications
You must be signed in to change notification settings - Fork 851
compress plugin - use configured compression levels for gzip and brotli #12924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,6 @@ extern const char *dictionary; | |
|
|
||
| namespace Gzip | ||
| { | ||
| const int ZLIB_COMPRESSION_LEVEL = 6; | ||
| voidpf | ||
| gzip_alloc(voidpf /* opaque ATS_UNUSED */, uInt items, uInt size) | ||
| { | ||
|
|
@@ -51,11 +50,6 @@ gzip_free(voidpf /* opaque ATS_UNUSED */, voidpf address) | |
| void | ||
| data_alloc(Data *data) | ||
| { | ||
| int window_bits = WINDOW_BITS_GZIP; | ||
| if (data->compression_type & COMPRESSION_TYPE_DEFLATE) { | ||
| window_bits = WINDOW_BITS_DEFLATE; | ||
| } | ||
|
|
||
| data->zstrm.next_in = Z_NULL; | ||
| data->zstrm.avail_in = 0; | ||
| data->zstrm.total_in = 0; | ||
|
|
@@ -66,19 +60,36 @@ data_alloc(Data *data) | |
| data->zstrm.zfree = Gzip::gzip_free; | ||
| data->zstrm.opaque = (voidpf) nullptr; | ||
| data->zstrm.data_type = Z_ASCII; | ||
| } | ||
|
|
||
| bool | ||
| transform_init(Data *data) | ||
| { | ||
| int window_bits = WINDOW_BITS_GZIP; | ||
| if (data->compression_type & COMPRESSION_TYPE_DEFLATE) { | ||
| window_bits = WINDOW_BITS_DEFLATE; | ||
| } | ||
|
|
||
| int compression_level = data->hc->zlib_compression_level(); | ||
| debug("gzip compression context initialized with level %d", compression_level); | ||
|
||
|
|
||
| int err = deflateInit2(&data->zstrm, ZLIB_COMPRESSION_LEVEL, Z_DEFLATED, window_bits, ZLIB_MEMLEVEL, Z_DEFAULT_STRATEGY); | ||
| int err = deflateInit2(&data->zstrm, compression_level, Z_DEFLATED, window_bits, ZLIB_MEMLEVEL, Z_DEFAULT_STRATEGY); | ||
|
|
||
| if (err != Z_OK) { | ||
| fatal("gzip-transform: ERROR: deflateInit (%d)!", err); | ||
| error("gzip-transform: deflateInit2 failed (%d)", err); | ||
| return false; | ||
|
Comment on lines
78
to
+80
|
||
| } | ||
|
|
||
| if (Compress::dictionary) { | ||
| err = deflateSetDictionary(&data->zstrm, reinterpret_cast<const Bytef *>(Compress::dictionary), strlen(Compress::dictionary)); | ||
| if (err != Z_OK) { | ||
| fatal("gzip-transform: ERROR: deflateSetDictionary (%d)!", err); | ||
| error("gzip-transform: deflateSetDictionary failed (%d)", err); | ||
| deflateEnd(&data->zstrm); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
Gzip::transform_init(orBrotli::transform_init) fails andcompress_transform_initreturns early,data->statehas already been set totransform_state_output(line 325). This meanscompress_transform_dowon't callcompress_transform_initagain, and will proceed to callcompress_transform_oneandcompress_transform_finish, which in turn callGzip::transform_one/Gzip::transform_finish(or brotli equivalents) on an encoder that was never successfully initialized. This is undefined behavior that can lead to crashes.The fix should either: (a) change the state to a new "error" state that prevents further processing, or (b) use
compress_transform_init's return value to skip subsequent processing incompress_transform_do.