Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 2 Nov 2000 02:22:42 -0800
From:      Kris Kennaway <kris@freebsd.org>
To:        audit@freebsd.org
Subject:   sort(1) tempfile patch
Message-ID:  <20001102022242.A24460@citusc17.usc.edu>

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

[-- Attachment #1 --]
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
===================================================================
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;
 
-  fd = open (file, O_EXCL | O_WRONLY | O_CREAT | O_TRUNC, 0600);
+  fd = open (file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
   if (fd < 0 || (fp = fdopen (fd, "w")) == NULL)
     {
       error (0, errno, "%s", file);
@@ -420,22 +420,24 @@
 static char *
 tempname (void)
 {
-  static unsigned int seq;
+  int fd;
   int len = strlen (temp_file_prefix);
   char *name = xmalloc (len + 1 + sizeof ("sort") - 1 + 5 + 5 + 1);
   struct tempnode *node;
 
   node = (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] != '/') ? "/" : "",
-	   (unsigned int) getpid () & 0xffff, seq);
+	   (len && temp_file_prefix[len - 1] != '/') ? "/" : "");
 
-  /* Make sure that SEQ's value fits in 5 digits.  */
-  ++seq;
-  if (seq >= 100000)
-    seq = 0;
+  if ((fd = mkstemp(name)) == -1)
+    {
+      error (0, errno, _("mkstemp error"));
+      cleanup ();
+      exit (2);
+    }
+  close(fd);
 
   node->name = name;
   node->next = temphead.next;


[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (FreeBSD)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjoBQHIACgkQWry0BWjoQKXlYwCcCNvhjuQBaLz+GTZqifIvDuKm
118An2uv3XZRctuSQNCivFY97kpI3Oiz
=lcbF
-----END PGP SIGNATURE-----

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