emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r117733: Minor cleanups of str_collate fix.


From: Paul Eggert
Subject: [Emacs-diffs] trunk r117733: Minor cleanups of str_collate fix.
Date: Mon, 25 Aug 2014 05:45:03 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 117733
revision-id: address@hidden
parent: address@hidden
fixes bug: http://debbugs.gnu.org/18051
committer: Paul Eggert <address@hidden>
branch nick: trunk
timestamp: Sun 2014-08-24 22:44:57 -0700
message:
  Minor cleanups of str_collate fix.
  
  * 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.
modified:
  src/ChangeLog                  changelog-20091113204419-o5vbwnq5f7feedwu-1438
  src/fns.c                      fns.c-20091113204419-o5vbwnq5f7feedwu-203
  src/lisp.h                     lisp.h-20091113204419-o5vbwnq5f7feedwu-253
  src/sysdep.c                   sysdep.c-20091113204419-o5vbwnq5f7feedwu-448
=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2014-08-24 15:40:07 +0000
+++ b/src/ChangeLog     2014-08-25 05:44:57 +0000
@@ -1,3 +1,15 @@
+2014-08-25  Paul Eggert  <address@hidden>
+
+       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  <address@hidden>
 
        * fns.c (Fstring_collate_lessp, Fstring_collate_equalp): New DEFUNs.

=== modified file 'src/fns.c'
--- a/src/fns.c 2014-08-24 15:40:07 +0000
+++ b/src/fns.c 2014-08-25 05:44:57 +0000
@@ -344,11 +344,6 @@
   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.
 

=== modified file 'src/lisp.h'
--- a/src/lisp.h        2014-07-28 06:28:15 +0000
+++ b/src/lisp.h        2014-08-25 05:44:57 +0000
@@ -4295,6 +4295,7 @@
 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);

=== modified file 'src/sysdep.c'
--- a/src/sysdep.c      2014-08-24 15:40:07 +0000
+++ b/src/sysdep.c      2014-08-25 05:44:57 +0000
@@ -3517,56 +3517,61 @@
 /* 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 @@
     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 ();


reply via email to

[Prev in Thread] Current Thread [Next in Thread]