From 7d66a3f0ef7f2f5397e569bf0ad7c6e3b695c883 Mon Sep 17 00:00:00 2001 From: tb Date: Mon, 29 Aug 2022 19:42:01 +0000 Subject: [PATCH] Fix growth check in compress(1)/gzip(1) If a compressed file is larger than its expanded version, compress(1) and gzip(1) don't compress unless -f is given. As found by gkoehler, the check is not quite correct for very small files or files with sufficiently random data. Fix the check so that slight growth still triggers the check. ok millert --- usr.bin/compress/main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/usr.bin/compress/main.c b/usr.bin/compress/main.c index 66cd2287c3b..81d7bd590c0 100644 --- a/usr.bin/compress/main.c +++ b/usr.bin/compress/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.100 2022/04/10 18:05:39 jca Exp $ */ +/* $OpenBSD: main.c,v 1.101 2022/08/29 19:42:01 tb Exp $ */ /* * Copyright (c) 1992, 1993 @@ -582,7 +582,8 @@ docompress(const char *in, char *out, const struct compressor *method, error = FAILURE; } - if (!force && !cat && info.total_out >= info.total_in) { + if (!force && !cat && (info.hlen >= info.total_in || + info.total_out >= info.total_in - info.hlen)) { if (verbose > 0) fprintf(stderr, "file would grow; left unmodified\n"); (void) unlink(out); -- 2.20.1