Module Name:    src
Committed By:   rillig
Date:           Sun May 22 21:16:50 UTC 2022

Modified Files:
        src/tests/usr.bin/compress: t_pr_19722.sh
        src/usr.bin/compress: compress.c

Log Message:
uncompress: if the source doesn't exist, don't touch the target

Fixes part of PR bin/19722, reported by Giorgos Keramidas.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/compress/t_pr_19722.sh
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/compress/compress.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/compress/t_pr_19722.sh
diff -u src/tests/usr.bin/compress/t_pr_19722.sh:1.2 src/tests/usr.bin/compress/t_pr_19722.sh:1.3
--- src/tests/usr.bin/compress/t_pr_19722.sh:1.2	Sun May 22 20:49:12 2022
+++ src/tests/usr.bin/compress/t_pr_19722.sh	Sun May 22 21:16:50 2022
@@ -1,4 +1,4 @@
-#	$NetBSD: t_pr_19722.sh,v 1.2 2022/05/22 20:49:12 rillig Exp $
+#	$NetBSD: t_pr_19722.sh,v 1.3 2022/05/22 21:16:50 rillig Exp $
 #
 # Copyright (c) 2022 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -92,15 +92,17 @@ atf_test_case 'uncompress_no_source_no_t
 uncompress_no_source_no_target_body()
 {
 	# PR 19722: uncompressing a missing source creates empty target
+	#
+	# Before compress.c 1.28 from 2022-05-22, uncompress created an empty
+	# target file and didn't clean it up.
 
 	atf_check \
 	    -s 'not-exit:0' \
 	    -e 'inline:uncompress: file.Z: No such file or directory\n' \
 	    uncompress -f file
 
-	# FIXME: The target file must not be created.
-	atf_check cat file
-	atf_check test ! -f nonexistent.Z
+	atf_check test ! -f file
+	atf_check test ! -f file.Z
 }
 
 
@@ -108,6 +110,9 @@ atf_test_case 'uncompress_no_source_exis
 uncompress_no_source_existing_target_body()
 {
 	# PR 19722: uncompressing a missing source truncates target
+	#
+	# Before compress.c 1.28 from 2022-05-22, uncompress truncated the
+	# target.
 
 	atf_check sh -c "echo 'hello' > file"
 
@@ -116,8 +121,7 @@ uncompress_no_source_existing_target_bod
 	    -e 'inline:uncompress: file.Z: No such file or directory\n' \
 	    uncompress -f file
 
-	# FIXME: The file must not be truncated.
-	atf_check cat file
+	atf_check -o 'inline:hello\n' cat file
 	atf_check test ! -f file.Z
 }
 

Index: src/usr.bin/compress/compress.c
diff -u src/usr.bin/compress/compress.c:1.27 src/usr.bin/compress/compress.c:1.28
--- src/usr.bin/compress/compress.c:1.27	Sun May 22 19:41:49 2022
+++ src/usr.bin/compress/compress.c	Sun May 22 21:16:50 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: compress.c,v 1.27 2022/05/22 19:41:49 rillig Exp $	*/
+/*	$NetBSD: compress.c,v 1.28 2022/05/22 21:16:50 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 19
 #if 0
 static char sccsid[] = "@(#)compress.c	8.2 (Berkeley) 1/7/94";
 #else
-__RCSID("$NetBSD: compress.c,v 1.27 2022/05/22 19:41:49 rillig Exp $");
+__RCSID("$NetBSD: compress.c,v 1.28 2022/05/22 21:16:50 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -313,15 +313,15 @@ decompress(const char *in, const char *o
 		oreg = 0;
 
 	ifp = ofp = NULL;
-	if ((ofp = fopen(out, "w")) == NULL) {
-		cwarn("%s", out);
-		return;
-	}
 
 	if ((ifp = zopen(in, "r", bits)) == NULL) {
 		cwarn("%s", in);
 		goto err;
 	}
+	if ((ofp = fopen(out, "w")) == NULL) {
+		cwarn("%s", out);
+		goto err;
+	}
 	if (!isstdin) {
 		if (stat(in, &sb)) {
 			cwarn("%s", in);

Reply via email to