]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
compress: prevent divide-by-zero when no data is read (#37706)
authorAlex <alexguo1023@gmail.com>
Tue, 3 Jun 2025 01:51:00 +0000 (21:51 -0400)
committerGitHub <noreply@github.com>
Tue, 3 Jun 2025 01:51:00 +0000 (10:51 +0900)
If the first call to `loop_read()` returns 0 (no input), `total_in`
remains 0, causing `total_out/total_in` to potential divide by zero.

We add a check before logging the compression ratio to skip the
percentage calculation when total_in is zero.

Co-authored-by: jinyaoguo <guo846@purdue.edu>
src/basic/compress.c

index 3270a7ed104ea8ffaa9765ce68e8f0cd5906b697..e9abe6fead76f9bf65b2fe9c7d242718e1740d98 100644 (file)
@@ -921,9 +921,12 @@ int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_unco
         if (ret_uncompressed_size)
                 *ret_uncompressed_size = total_in;
 
-        log_debug("LZ4 compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
-                  total_in, total_out,
-                  (double) total_out / total_in * 100);
+        if (total_in == 0)
+                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);
 
         return 0;
 #else