]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Don’t try a higher zstd level than supported
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 29 Jun 2019 18:25:31 +0000 (20:25 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 29 Jun 2019 18:27:16 +0000 (20:27 +0200)
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.

src/compr_zstd.c

index c6a11d7a1034c7c2dbb911955cb11112eee7cd59..90c8f7adaff3749c12d46be3fa8291f855b5bb01 100644 (file)
@@ -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);