From owner-freebsd-audit Thu Nov 2 2:20:50 2000 Delivered-To: freebsd-audit@freebsd.org Received: from citusc17.usc.edu (citusc17.usc.edu [128.125.38.177]) by hub.freebsd.org (Postfix) with ESMTP id 5E62B37B4D7 for ; Thu, 2 Nov 2000 02:20:47 -0800 (PST) Received: (from kris@localhost) by citusc17.usc.edu (8.11.1/8.11.1) id eA2AMhw24484 for audit@freebsd.org; Thu, 2 Nov 2000 02:22:43 -0800 (PST) (envelope-from kris) Date: Thu, 2 Nov 2000 02:22:42 -0800 From: Kris Kennaway To: audit@freebsd.org Subject: sort(1) tempfile patch Message-ID: <20001102022242.A24460@citusc17.usc.edu> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-md5; protocol="application/pgp-signature"; boundary="FL5UXtIhxfXey3p5" Content-Disposition: inline User-Agent: Mutt/1.2.5i Sender: owner-freebsd-audit@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG --FL5UXtIhxfXey3p5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Sort creates braindead temporary filenames which are easily predictable. As far as I can see, the sequential numbering has no function, since the filenames are stored in a linked list and so the ordering information is already there. Please review.. Kris Index: sort.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: /mnt/ncvs/src/gnu/usr.bin/sort/sort.c,v retrieving revision 1.17 diff -u -r1.17 sort.c --- sort.c 2000/07/31 23:36:08 1.17 +++ sort.c 2000/11/02 06:12:48 @@ -340,7 +340,7 @@ FILE *fp; int fd; =20 - fd =3D open (file, O_EXCL | O_WRONLY | O_CREAT | O_TRUNC, 0600); + fd =3D open (file, O_WRONLY | O_CREAT | O_TRUNC, 0600); if (fd < 0 || (fp =3D fdopen (fd, "w")) =3D=3D NULL) { error (0, errno, "%s", file); @@ -420,22 +420,24 @@ static char * tempname (void) { - static unsigned int seq; + int fd; int len =3D strlen (temp_file_prefix); char *name =3D xmalloc (len + 1 + sizeof ("sort") - 1 + 5 + 5 + 1); struct tempnode *node; =20 node =3D (struct tempnode *) xmalloc (sizeof (struct tempnode)); sprintf (name, - "%s%ssort%5.5d%5.5d", + "%s%ssortXXXXXXXXXX", temp_file_prefix, - (len && temp_file_prefix[len - 1] !=3D '/') ? "/" : "", - (unsigned int) getpid () & 0xffff, seq); + (len && temp_file_prefix[len - 1] !=3D '/') ? "/" : ""); =20 - /* Make sure that SEQ's value fits in 5 digits. */ - ++seq; - if (seq >=3D 100000) - seq =3D 0; + if ((fd =3D mkstemp(name)) =3D=3D -1) + { + error (0, errno, _("mkstemp error")); + cleanup (); + exit (2); + } + close(fd); =20 node->name =3D name; node->next =3D temphead.next; --FL5UXtIhxfXey3p5 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.4 (FreeBSD) Comment: For info see http://www.gnupg.org iEYEARECAAYFAjoBQHIACgkQWry0BWjoQKXlYwCcCNvhjuQBaLz+GTZqifIvDuKm 118An2uv3XZRctuSQNCivFY97kpI3Oiz =lcbF -----END PGP SIGNATURE----- --FL5UXtIhxfXey3p5-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message