]> git.eshelyaron.com Git - emacs.git/commitdiff
Minor cleanups of str_collate fix.
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 25 Aug 2014 05:44:57 +0000 (22:44 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 25 Aug 2014 05:44:57 +0000 (22:44 -0700)
* fns.c (str_collate): Move decl from here ...
* lisp.h (str_collate): ... to here.
* sysdep.c (str_collate): Prune away some of the forest of ifdefs.
Remove unnecessary casts.  Use SAFE_NALLOCA to avoid
potential problems with integer overflow.  Don't assume
setlocale succeeds.  Remove unnecessary test before restoring
locale via setlocale, and free the copied setlocale string
when done with it.

Fixes: debbugs:18051
src/ChangeLog
src/fns.c
src/lisp.h
src/sysdep.c

index 72d7d405f7a125394e2d255dbc9214460e8605c4..9b70b8f541108e569433fb69598db327c65886ca 100644 (file)
@@ -1,3 +1,15 @@
+2014-08-25  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Minor cleanups of str_collate fix (Bug#18051).
+       * fns.c (str_collate): Move decl from here ...
+       * lisp.h (str_collate): ... to here.
+       * sysdep.c (str_collate): Prune away some of the forest of ifdefs.
+       Remove unnecessary casts.  Use SAFE_NALLOCA to avoid
+       potential problems with integer overflow.  Don't assume
+       setlocale succeeds.  Remove unnecessary test before restoring
+       locale via setlocale, and free the copied setlocale string
+       when done with it.
+
 2014-08-24  Michael Albinus  <michael.albinus@gmx.de>
 
        * fns.c (Fstring_collate_lessp, Fstring_collate_equalp): New DEFUNs.
index fbcec4e659ef1969878c49f18cf779a398e66d67..616b54d193c632f084fcb791e8935a9e0d0b6970 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -344,11 +344,6 @@ Symbols are also allowed; their print names are used instead.  */)
   return i1 < SCHARS (s2) ? Qt : Qnil;
 }
 
-#ifdef __STDC_ISO_10646__
-/* Defined in sysdep.c.  */
-extern ptrdiff_t str_collate (Lisp_Object, Lisp_Object);
-#endif /* __STDC_ISO_10646__ */
-
 DEFUN ("string-collate-lessp", Fstring_collate_lessp, Sstring_collate_lessp, 2, 2, 0,
        doc: /* Return t if first arg string is less than second in collation order.
 
index 67299706c6b3aee40e8631271b3956add0f42914..1e2a8b6535f17547ab5ca88f79aea4dfa25865ae 100644 (file)
@@ -4295,6 +4295,7 @@ extern void lock_file (Lisp_Object);
 extern void unlock_file (Lisp_Object);
 extern void unlock_buffer (struct buffer *);
 extern void syms_of_filelock (void);
+extern ptrdiff_t str_collate (Lisp_Object, Lisp_Object);
 
 /* Defined in sound.c.  */
 extern void syms_of_sound (void);
index 619361472e49e379d21ff21b8a75f2edf208b360..856d668bb71ba12de035035216e21001ea3d36d4 100644 (file)
@@ -3517,56 +3517,61 @@ system_process_attributes (Lisp_Object pid)
 /* Wide character string collation.  */
 
 #ifdef __STDC_ISO_10646__
-#include <wchar.h>
+# include <wchar.h>
 
-#if defined (HAVE_USELOCALE) || defined (HAVE_SETLOCALE)
-#include <locale.h>
-#endif /* HAVE_USELOCALE || HAVE_SETLOCALE */
+# if defined HAVE_USELOCALE || defined HAVE_SETLOCALE
+#  include <locale.h>
+# endif
+# ifndef HAVE_SETLOCALE
+#  define setlocale(category, locale) ((char *) 0)
+# endif
 
 ptrdiff_t
 str_collate (Lisp_Object s1, Lisp_Object s2)
 {
-  register ptrdiff_t res, len, i, i_byte;
+  ptrdiff_t res, len, i, i_byte;
   wchar_t *p1, *p2;
   Lisp_Object lc_collate;
-#ifdef HAVE_USELOCALE
-  locale_t loc = (locale_t) 0, oldloc = (locale_t) 0;
-#elif defined (HAVE_SETLOCALE)
+# ifdef HAVE_USELOCALE
+  locale_t loc = 0, oldloc = 0;
+# else
   char *oldloc = NULL;
-#endif /* HAVE_USELOCALE */
+# endif
 
   USE_SAFE_ALLOCA;
 
   /* Convert byte stream to code points.  */
   len = SCHARS (s1); i = i_byte = 0;
-  p1 = (wchar_t *) SAFE_ALLOCA ((len+1) * (sizeof *p1));
+  SAFE_NALLOCA (p1, 1, len + 1);
   while (i < len)
     FETCH_STRING_CHAR_ADVANCE (*(p1+i-1), s1, i, i_byte);
   *(p1+len) = 0;
 
   len = SCHARS (s2); i = i_byte = 0;
-  p2 = (wchar_t *) SAFE_ALLOCA ((len+1) * (sizeof *p2));
+  SAFE_NALLOCA (p2, 1, len + 1);
   while (i < len)
     FETCH_STRING_CHAR_ADVANCE (*(p2+i-1), s2, i, i_byte);
   *(p2+len) = 0;
 
-#if defined (HAVE_USELOCALE) || defined (HAVE_SETLOCALE)
   /* Create a new locale object, and set it.  */
   lc_collate =
     Fgetenv_internal (build_string ("LC_COLLATE"), Vprocess_environment);
 
-#ifdef HAVE_USELOCALE
-  if (STRINGP (lc_collate)
-      && (loc = newlocale (LC_COLLATE_MASK, SSDATA (lc_collate), (locale_t) 0)))
-    oldloc = uselocale (loc);
-#elif defined (HAVE_SETLOCALE)
   if (STRINGP (lc_collate))
     {
-      oldloc = xstrdup (setlocale (LC_COLLATE, NULL));
-      setlocale (LC_COLLATE, SSDATA (lc_collate));
+#ifdef HAVE_USELOCALE
+      loc = newlocale (LC_COLLATE_MASK, SSDATA (lc_collate), 0);
+      if (loc)
+       oldloc = uselocale (loc);
+#else
+      oldloc = setlocale (LC_COLLATE, NULL);
+      if (oldloc)
+       {
+         oldloc = xstrdup (oldloc);
+         setlocale (LC_COLLATE, SSDATA (lc_collate));
+       }
+#endif
     }
-#endif /* HAVE_USELOCALE */
-#endif /* HAVE_USELOCALE || HAVE_SETLOCALE */
 
   res = wcscoll (p1, p2);
 
@@ -3576,11 +3581,11 @@ str_collate (Lisp_Object s1, Lisp_Object s2)
     freelocale (loc);
   if (oldloc)
     uselocale (oldloc);
-#elif defined (HAVE_SETLOCALE)
+#else
   /* Restore the original locale. */
-  if (oldloc)
-    setlocale (LC_COLLATE, oldloc);
-#endif /* HAVE_USELOCALE */
+  setlocale (LC_COLLATE, oldloc);
+  xfree (oldloc);
+#endif
 
   /* Return result.  */
   SAFE_FREE ();