From: Jim Meyering <jim@meyering.net>
Date: Fri, 28 Sep 2007 21:26:49 +0000 (+0200)
Subject: Fix canonicalize loop-detection corner case.
X-Git-Tag: v0.0~191
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=05552c43d21f272e927663aae4bc810e17d4d785;p=gnulib.git

Fix canonicalize loop-detection corner case.

Do not attempt to stat the symlink values stored via seen_triple.
Without this, coreutils' tests/misc/readlink-fp-loop test would fail
on linux-2.6.18, (but not 2.6.22).
* lib/canonicalize.c (seen_triple): Use triple_compare_ino_str, not
triple_compare.  The former compares dev,ino,filename, while the latter
would actually stat dirname(filename) when dev and ino were equal.
* lib/hash-triple.c: Install <string.h>.
(STREQ): Define.
(triple_compare_ino_str): New function.
* lib/hash-triple.h (triple_compare_ino_str): Declare it.
---

diff --git a/ChangeLog b/ChangeLog
index 80a7159338..44972ece21 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2007-09-28  Jim Meyering  <jim@meyering.net>
+
+	Fix canonicalize loop-detection corner case.
+	Do not attempt to stat the symlink values stored via seen_triple.
+	Without this, coreutils' tests/misc/readlink-fp-loop test would fail
+	on linux-2.6.18, (but not 2.6.22).
+	* lib/canonicalize.c (seen_triple): Use triple_compare_ino_str, not
+	triple_compare.  The former compares dev,ino,filename, while the latter
+	would actually stat dirname(filename) when dev and ino were equal.
+	* lib/hash-triple.c: Install <string.h>.
+	(STREQ): Define.
+	(triple_compare_ino_str): New function.
+	* lib/hash-triple.h (triple_compare_ino_str): Declare it.
+
 2007-09-28  Eric Blake  <ebb9@byu.net>
 
 	Enforce that AC_REPLACE_FUNCS files exist.
diff --git a/lib/canonicalize.c b/lib/canonicalize.c
index af2703cf7a..bbf3855f2f 100644
--- a/lib/canonicalize.c
+++ b/lib/canonicalize.c
@@ -135,7 +135,7 @@ seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
       *ht = hash_initialize (initial_capacity,
 			    NULL,
 			    triple_hash,
-			    triple_compare,
+			    triple_compare_ino_str,
 			    triple_free);
       if (*ht == NULL)
 	xalloc_die ();
diff --git a/lib/hash-triple.c b/lib/hash-triple.c
index 2644b5f677..a65a4db9be 100644
--- a/lib/hash-triple.c
+++ b/lib/hash-triple.c
@@ -22,11 +22,14 @@
 #include "hash-triple.h"
 
 #include <stdlib.h>
+#include <string.h>
 
 #include "hash-pjw.h"
 #include "same.h"
 #include "same-inode.h"
 
+#define STREQ(a, b) (strcmp ((a), (b)) == 0)
+
 /* Hash an F_triple, and *do* consider the file name.  */
 size_t
 triple_hash (void const *x, size_t table_size)
@@ -57,6 +60,14 @@ triple_compare (void const *x, void const *y)
   return (SAME_INODE (*a, *b) && same_name (a->name, b->name)) ? true : false;
 }
 
+bool
+triple_compare_ino_str (void const *x, void const *y)
+{
+  struct F_triple const *a = x;
+  struct F_triple const *b = y;
+  return (SAME_INODE (*a, *b) && STREQ (a->name, b->name)) ? true : false;
+}
+
 /* Free an F_triple.  */
 void
 triple_free (void *x)
diff --git a/lib/hash-triple.h b/lib/hash-triple.h
index 7abe970441..51863c9dff 100644
--- a/lib/hash-triple.h
+++ b/lib/hash-triple.h
@@ -16,6 +16,7 @@ struct F_triple
 extern size_t triple_hash (void const *x, size_t table_size);
 extern size_t triple_hash_no_name (void const *x, size_t table_size);
 extern bool triple_compare (void const *x, void const *y);
+extern bool triple_compare_ino_str (void const *x, void const *y);
 extern void triple_free (void *x);
 
 #endif