From: Joel Rosdahl Date: Sat, 29 Jun 2019 18:25:31 +0000 (+0200) Subject: Don’t try a higher zstd level than supported X-Git-Tag: v4.0~929 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=a7badf49d6c07856041942d404c8c495c0d1ca55;p=thirdparty%2Fccache.git Don’t try a higher zstd level than supported If the user tries a higher level than supported by libzstd, initialization will fail. Instead, let’s clamp the level to the highest supported value. Regarding negative levels: They are supported from libzstd 1.3.4, but the query function ZSTD_minCLevel is only supported from 1.4.0 (from 1.3.6 with ZSTD_STATIC_LINKING_ONLY), so let’s not use it for verification of the level. In libzstd 1.3.3 and older, negative levels are silently converted to the zstd’s default level (3), so there’s no major harm done if a user uses a negative level with older libzstd versions. --- diff --git a/src/compr_zstd.c b/src/compr_zstd.c index c6a11d7a1..90c8f7ada 100644 --- a/src/compr_zstd.c +++ b/src/compr_zstd.c @@ -39,7 +39,15 @@ compr_zstd_init(FILE *output, int8_t level) state->stream = ZSTD_createCStream(); state->failed = false; - size_t ret = ZSTD_initCStream(state->stream, level); + int8_t actual_level = MIN(level, ZSTD_maxCLevel()); + if (actual_level != level) { + cc_log( + "Using compression level %d (max libzstd level) instead of %d", + actual_level, + level); + } + + size_t ret = ZSTD_initCStream(state->stream, actual_level); if (ZSTD_isError(ret)) { ZSTD_freeCStream(state->stream); free(state);