uncompress: if the source is broken, don't delete the target

Fixes the second half of PR bin/19722, reported by Giorgos Keramidas.
This commit is contained in:
rillig 2022-05-22 21:39:44 +00:00
parent cd107d72e2
commit 33a964ec67
2 changed files with 21 additions and 9 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: t_pr_19722.sh,v 1.3 2022/05/22 21:16:50 rillig Exp $ # $NetBSD: t_pr_19722.sh,v 1.4 2022/05/22 21:39:44 rillig Exp $
# #
# Copyright (c) 2022 The NetBSD Foundation, Inc. # Copyright (c) 2022 The NetBSD Foundation, Inc.
# All rights reserved. # All rights reserved.
@ -148,6 +148,10 @@ atf_test_case 'uncompress_broken_source_existing_target'
uncompress_broken_source_existing_target_body() uncompress_broken_source_existing_target_body()
{ {
# PR 19722: uncompressing a broken source removes existing target # PR 19722: uncompressing a broken source removes existing target
#
# Before compress.c 1.29 from 2022-05-22, uncompress removed an
# existing target before checking that the source has the correct
# format.
echo 'broken' > file.Z echo 'broken' > file.Z
echo 'before' > file echo 'before' > file
@ -158,8 +162,7 @@ uncompress_broken_source_existing_target_body()
uncompress -f file.Z uncompress -f file.Z
atf_check -o 'inline:broken\n' cat file.Z atf_check -o 'inline:broken\n' cat file.Z
# FIXME: Must not be modified. atf_check -o 'inline:before\n' cat file
atf_check test ! -f file
} }

View File

@ -1,4 +1,4 @@
/* $NetBSD: compress.c,v 1.28 2022/05/22 21:16:50 rillig Exp $ */ /* $NetBSD: compress.c,v 1.29 2022/05/22 21:39:44 rillig Exp $ */
/*- /*-
* Copyright (c) 1992, 1993 * Copyright (c) 1992, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
#if 0 #if 0
static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94"; static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
#else #else
__RCSID("$NetBSD: compress.c,v 1.28 2022/05/22 21:16:50 rillig Exp $"); __RCSID("$NetBSD: compress.c,v 1.29 2022/05/22 21:39:44 rillig Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -318,10 +318,6 @@ decompress(const char *in, const char *out, int bits)
cwarn("%s", in); cwarn("%s", in);
goto err; goto err;
} }
if ((ofp = fopen(out, "w")) == NULL) {
cwarn("%s", out);
goto err;
}
if (!isstdin) { if (!isstdin) {
if (stat(in, &sb)) { if (stat(in, &sb)) {
cwarn("%s", in); cwarn("%s", in);
@ -333,6 +329,19 @@ decompress(const char *in, const char *out, int bits)
isreg = 1; isreg = 1;
} else } else
isreg = 0; isreg = 0;
if ((nr = fread(buf, 1, sizeof(buf), ifp)) == 0) {
cwarn("%s", in);
goto err;
}
if ((ofp = fopen(out, "w")) == NULL) {
cwarn("%s", out);
goto err;
}
if (fwrite(buf, 1, nr, ofp) != nr) {
cwarn("%s", out);
goto err;
}
oreg <<= 1; oreg <<= 1;
while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0) while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)