Ver código fonte

libcompat: Fix scandir comparison function argument handling

Properly dereference the arguments. As a side effect this makes scandir
reentrant now that there's no static helper function.
Guillem Jover 18 anos atrás
pai
commit
7dd4f89992
4 arquivos alterados com 18 adições e 14 exclusões
  1. 12 0
      ChangeLog
  2. 4 2
      libcompat/alphasort.c
  3. 1 2
      libcompat/compat.h
  4. 1 10
      libcompat/scandir.c

+ 12 - 0
ChangeLog

@@ -1,3 +1,15 @@
+2008-09-14  Guillem Jover  <guillem@debian.org>
+
+	* libcompat/compat.h (alphasort): Change argument types to match
+	modern system ones (the current ones were missing one indirection
+	anyway).
+	* libcompat/alphasort.c (alphasort): Likewise. Properly dereference
+	arguments.
+	* libcompat/scandir.c (scandir_comparfn): Remove static function
+	variable.
+	(scandir_compar): Remove function.
+	(scandir): Pass compar function directly to qsort.
+
 2008-09-14  Guillem Jover  <guillem@debian.org>
 
 	* libcompat/strerror.c (strerror): Change string for unknown value.

+ 4 - 2
libcompat/alphasort.c

@@ -25,9 +25,11 @@
 
 #ifndef HAVE_ALPHASORT
 int
-alphasort(const struct dirent *a, const struct dirent *b)
+alphasort(const void *a, const void *b)
 {
-	return strcmp(a->d_name, b->d_name);
+	return strcmp((*(const struct dirent **)a)->d_name,
+	              (*(const struct dirent **)b)->d_name);
+
 }
 #endif
 

+ 1 - 2
libcompat/compat.h

@@ -65,8 +65,7 @@ int scandir(const char *dir, struct dirent ***namelist,
 #endif
 
 #ifndef HAVE_ALPHASORT
-struct dirent;
-int alphasort(const struct dirent *a, const struct dirent *b);
+int alphasort(const void *a, const void *b);
 #endif
 
 #ifndef HAVE_UNSETENV

+ 1 - 10
libcompat/scandir.c

@@ -26,14 +26,6 @@
 #include <string.h>
 
 #ifndef HAVE_SCANDIR
-static int (*scandir_comparfn)(const void *, const void *);
-static int
-scandir_compar(const void *a, const void *b)
-{
-	return scandir_comparfn(*(const struct dirent **)a,
-	                        *(const struct dirent **)b);
-}
-
 int
 scandir(const char *dir, struct dirent ***namelist,
         int (*select)(const struct dirent *),
@@ -76,8 +68,7 @@ scandir(const char *dir, struct dirent ***namelist,
 	}
 	(*namelist)[used] = NULL;
 
-	scandir_comparfn = compar;
-	qsort(*namelist, used, sizeof(struct dirent *), scandir_compar);
+	qsort(*namelist, used, sizeof(struct dirent *), compar);
 
 	return used;
 }