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

--Dxnq1zWXvFF0Q93v
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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 =3D oreg =3D !exists || S_ISREG(sb.st_mode);
=20
 	ifp =3D ofp =3D NULL;
-	if ((ofp =3D fopen(out, "w")) =3D=3D NULL) {
-		cwarn("%s", out);
-		return;
-	}
-
 	if ((ifp =3D zopen(in, "r", bits)) =3D=3D NULL) {
 		cwarn("%s", in);
-		goto err;
+		return;
 	}
 	if (stat(in, &sb)) {
 		cwarn("%s", in);
 		goto err;
 	}
 	if (!S_ISREG(sb.st_mode))
 		isreg =3D 0;
=20
+	if ((ofp =3D fopen(out, "w")) =3D=3D NULL) {
+		cwarn("%s", out);
+		goto err;
+	}
 	while ((nr =3D fread(buf, 1, sizeof(buf), ifp)) !=3D 0)
 		if (fwrite(buf, 1, nr, ofp) !=3D nr) {
 			cwarn("%s", out);
 			goto err;
 		}
%%%

--Dxnq1zWXvFF0Q93v
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (FreeBSD)

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

--Dxnq1zWXvFF0Q93v--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




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