Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 6 Jan 2003 08:23:09 +0200
From:      Giorgos Keramidas <keramida@freebsd.org>
To:        freebsd-audit@freebsd.org
Subject:   hopefully, a fix for an old uncompress(1) bug
Message-ID:  <20030106062309.GA37109@gothmog.gr>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
Gary Swearingen brought the following bug to my attention while
posting a `fix' for the manpage of compress(1) and uncompress(1).
Instead of documenting the bug, I thought we might try to fix it.

When uncompress is called with an input filename that does not end in
a .Z extension, it attempts to add the necessary extension anyway.
The order of file opening in the compress() and decompress() functions
is reversed though, and it causes the following `interesting' behavior:

    % ls -l
    -rw-rw-r--  1 giorgos  wheel  -     0 Jan  6 08:04 lala
    % uncompress -f lala
    uncompress: lala.Z: No such file or directory
    % ls -l
    %

The file called `lala' just happened to have an unfortunate file name
and was unlinked at the end of decompress() function :-/

The following patch seems to fix this for me, but I'm not sure if it
breaks the `expected' behavior of uncompress for some case I haven't
thought about yet.  The compress() and decompress() functions in
compress.c are almost identical and they have been since revision 1.1
of the file, and I have only tested this lightly:

    % ls -l
    total 16
    -rw-rw-r--  1 giorgos  wheel  -     0 Jan  6 08:17 bar
    -rwxrwxr-x  1 giorgos  wheel  - 14681 Jan  6 08:04 uncompress
    % ./uncompress -f bar
    uncompress: bar.Z: No such file or directory
    % ls -l
    total 16
    -rw-rw-r--  1 giorgos  wheel  -     0 Jan  6 08:17 bar
    -rwxrwxr-x  1 giorgos  wheel  - 14681 Jan  6 08:04 uncompress
    %

What's your opinion all?

%%%
Index: compress.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/compress/compress.c,v
retrieving revision 1.20
diff -u -5 -r1.20 compress.c
--- compress.c	28 Jul 2002 15:32:17 -0000	1.20
+++ compress.c	6 Jan 2003 06:15:06 -0000
@@ -298,26 +298,25 @@
 	if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
 		return;
 	isreg = oreg = !exists || S_ISREG(sb.st_mode);
 
 	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;
+		return;
 	}
 	if (stat(in, &sb)) {
 		cwarn("%s", in);
 		goto err;
 	}
 	if (!S_ISREG(sb.st_mode))
 		isreg = 0;
 
+	if ((ofp = fopen(out, "w")) == NULL) {
+		cwarn("%s", out);
+		goto err;
+	}
 	while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
 		if (fwrite(buf, 1, nr, ofp) != nr) {
 			cwarn("%s", out);
 			goto err;
 		}
%%%

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (FreeBSD)

iD8DBQE+GSDN1g+UGjGGA7YRAv1LAJ9b3aPht3Xpy5OjRIGWw5zY5q5mIgCdFMYy
hC59Wi5t9n2VPBLRjW+Cklk=
=/zIe
-----END PGP SIGNATURE-----

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030106062309.GA37109>