]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
compress: avoid a bunch of div-by-zeroes
authorLennart Poettering <lennart@poettering.net>
Tue, 3 Jun 2025 14:26:17 +0000 (16:26 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 25 Jun 2025 17:17:42 +0000 (18:17 +0100)
Follow-up for #37706. Implements the same logic for all compression
algorithms we have.

(cherry picked from commit d483ac1d0d26f1df5237d15765a8fa782ed0db08)
(cherry picked from commit f3b35c3e9384e0a33a72e83bcc979415df677f64)

src/basic/compress.c

index b9e3f140f536ac639fe60f9792ce007690591241..9e2a4807188b2e54f3e9ffab787a83b68fa8fdff 100644 (file)
@@ -799,9 +799,12 @@ int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncom
                                 if (ret_uncompressed_size)
                                         *ret_uncompressed_size = s.total_in;
 
-                                log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
-                                          s.total_in, s.total_out,
-                                          (double) s.total_out / s.total_in * 100);
+                                if (s.total_in == 0)
+                                        log_debug("XZ compression finished (no input data)");
+                                else
+                                        log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
+                                                  s.total_in, s.total_out,
+                                                  (double) s.total_out / s.total_in * 100);
 
                                 return 0;
                         }
@@ -898,8 +901,8 @@ int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_unco
                 log_debug("LZ4 compression finished (no input data)");
         else
                 log_debug("LZ4 compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
-                        total_in, total_out,
-                        (double) total_out / total_in * 100);
+                          total_in, total_out,
+                          (double) total_out / total_in * 100);
 
         return 0;
 #else
@@ -972,9 +975,12 @@ int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
                                 return k;
 
                         if (ret == LZMA_STREAM_END) {
-                                log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
-                                          s.total_in, s.total_out,
-                                          (double) s.total_out / s.total_in * 100);
+                                if (s.total_in == 0)
+                                        log_debug("XZ decompression finished (no input data)");
+                                else
+                                        log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
+                                                  s.total_in, s.total_out,
+                                                  (double) s.total_out / s.total_in * 100);
 
                                 return 0;
                         }
@@ -1042,9 +1048,12 @@ int decompress_stream_lz4(int in, int out, uint64_t max_bytes) {
                         goto cleanup;
         }
 
-        log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
-                  total_in, total_out,
-                  total_in > 0 ? (double) total_out / total_in * 100 : 0.0);
+        if (total_in == 0)
+                log_debug("LZ4 decompression finished (no input data)");
+        else
+                log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
+                          total_in, total_out,
+                          (double) total_out / total_in * 100);
         r = 0;
  cleanup:
         munmap(src, st.st_size);
@@ -1152,12 +1161,11 @@ int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_unc
         if (ret_uncompressed_size)
                 *ret_uncompressed_size = in_bytes;
 
-        if (in_bytes > 0)
+        if (in_bytes == 0)
+                log_debug("ZSTD compression finished (no input data)");
+        else
                 log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
                           in_bytes, max_bytes - left, (double) (max_bytes - left) / in_bytes * 100);
-        else
-                log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes)",
-                          in_bytes, max_bytes - left);
 
         return 0;
 #else
@@ -1265,11 +1273,13 @@ int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
                 return zstd_ret_to_errno(last_result);
         }
 
-        log_debug(
-                "ZSTD decompression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
-                in_bytes,
-                max_bytes - left,
-                (double) (max_bytes - left) / in_bytes * 100);
+        if (in_bytes == 0)
+                log_debug("ZSTD decompression finished (no input data)");
+        else
+                log_debug("ZSTD decompression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
+                          in_bytes,
+                          max_bytes - left,
+                          (double) (max_bytes - left) / in_bytes * 100);
         return 0;
 #else
         return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),