aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2014-08-24 22:44:57 -0700
committerPaul Eggert2014-08-24 22:44:57 -0700
commit90c5c87753fb69dafd664615558fa3fdce3ba5b1 (patch)
treed072b51fd01b88140786d85b5fb4927ccce1a1af
parentd74553559b62cc7b07ed32c35391c1b75eda540f (diff)
downloademacs-90c5c87753fb69dafd664615558fa3fdce3ba5b1.tar.gz
emacs-90c5c87753fb69dafd664615558fa3fdce3ba5b1.zip
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. Fixes: debbugs:18051
-rw-r--r--src/ChangeLog12
-rw-r--r--src/fns.c5
-rw-r--r--src/lisp.h1
-rw-r--r--src/sysdep.c55
4 files changed, 43 insertions, 30 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 72d7d405f7a..9b70b8f5411 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,15 @@
12014-08-25 Paul Eggert <eggert@cs.ucla.edu>
2
3 Minor cleanups of str_collate fix (Bug#18051).
4 * fns.c (str_collate): Move decl from here ...
5 * lisp.h (str_collate): ... to here.
6 * sysdep.c (str_collate): Prune away some of the forest of ifdefs.
7 Remove unnecessary casts. Use SAFE_NALLOCA to avoid
8 potential problems with integer overflow. Don't assume
9 setlocale succeeds. Remove unnecessary test before restoring
10 locale via setlocale, and free the copied setlocale string
11 when done with it.
12
12014-08-24 Michael Albinus <michael.albinus@gmx.de> 132014-08-24 Michael Albinus <michael.albinus@gmx.de>
2 14
3 * fns.c (Fstring_collate_lessp, Fstring_collate_equalp): New DEFUNs. 15 * fns.c (Fstring_collate_lessp, Fstring_collate_equalp): New DEFUNs.
diff --git a/src/fns.c b/src/fns.c
index fbcec4e659e..616b54d193c 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -344,11 +344,6 @@ Symbols are also allowed; their print names are used instead. */)
344 return i1 < SCHARS (s2) ? Qt : Qnil; 344 return i1 < SCHARS (s2) ? Qt : Qnil;
345} 345}
346 346
347#ifdef __STDC_ISO_10646__
348/* Defined in sysdep.c. */
349extern ptrdiff_t str_collate (Lisp_Object, Lisp_Object);
350#endif /* __STDC_ISO_10646__ */
351
352DEFUN ("string-collate-lessp", Fstring_collate_lessp, Sstring_collate_lessp, 2, 2, 0, 347DEFUN ("string-collate-lessp", Fstring_collate_lessp, Sstring_collate_lessp, 2, 2, 0,
353 doc: /* Return t if first arg string is less than second in collation order. 348 doc: /* Return t if first arg string is less than second in collation order.
354 349
diff --git a/src/lisp.h b/src/lisp.h
index 67299706c6b..1e2a8b6535f 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -4295,6 +4295,7 @@ extern void lock_file (Lisp_Object);
4295extern void unlock_file (Lisp_Object); 4295extern void unlock_file (Lisp_Object);
4296extern void unlock_buffer (struct buffer *); 4296extern void unlock_buffer (struct buffer *);
4297extern void syms_of_filelock (void); 4297extern void syms_of_filelock (void);
4298extern ptrdiff_t str_collate (Lisp_Object, Lisp_Object);
4298 4299
4299/* Defined in sound.c. */ 4300/* Defined in sound.c. */
4300extern void syms_of_sound (void); 4301extern void syms_of_sound (void);
diff --git a/src/sysdep.c b/src/sysdep.c
index 619361472e4..856d668bb71 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -3517,56 +3517,61 @@ system_process_attributes (Lisp_Object pid)
3517/* Wide character string collation. */ 3517/* Wide character string collation. */
3518 3518
3519#ifdef __STDC_ISO_10646__ 3519#ifdef __STDC_ISO_10646__
3520#include <wchar.h> 3520# include <wchar.h>
3521 3521
3522#if defined (HAVE_USELOCALE) || defined (HAVE_SETLOCALE) 3522# if defined HAVE_USELOCALE || defined HAVE_SETLOCALE
3523#include <locale.h> 3523# include <locale.h>
3524#endif /* HAVE_USELOCALE || HAVE_SETLOCALE */ 3524# endif
3525# ifndef HAVE_SETLOCALE
3526# define setlocale(category, locale) ((char *) 0)
3527# endif
3525 3528
3526ptrdiff_t 3529ptrdiff_t
3527str_collate (Lisp_Object s1, Lisp_Object s2) 3530str_collate (Lisp_Object s1, Lisp_Object s2)
3528{ 3531{
3529 register ptrdiff_t res, len, i, i_byte; 3532 ptrdiff_t res, len, i, i_byte;
3530 wchar_t *p1, *p2; 3533 wchar_t *p1, *p2;
3531 Lisp_Object lc_collate; 3534 Lisp_Object lc_collate;
3532#ifdef HAVE_USELOCALE 3535# ifdef HAVE_USELOCALE
3533 locale_t loc = (locale_t) 0, oldloc = (locale_t) 0; 3536 locale_t loc = 0, oldloc = 0;
3534#elif defined (HAVE_SETLOCALE) 3537# else
3535 char *oldloc = NULL; 3538 char *oldloc = NULL;
3536#endif /* HAVE_USELOCALE */ 3539# endif
3537 3540
3538 USE_SAFE_ALLOCA; 3541 USE_SAFE_ALLOCA;
3539 3542
3540 /* Convert byte stream to code points. */ 3543 /* Convert byte stream to code points. */
3541 len = SCHARS (s1); i = i_byte = 0; 3544 len = SCHARS (s1); i = i_byte = 0;
3542 p1 = (wchar_t *) SAFE_ALLOCA ((len+1) * (sizeof *p1)); 3545 SAFE_NALLOCA (p1, 1, len + 1);
3543 while (i < len) 3546 while (i < len)
3544 FETCH_STRING_CHAR_ADVANCE (*(p1+i-1), s1, i, i_byte); 3547 FETCH_STRING_CHAR_ADVANCE (*(p1+i-1), s1, i, i_byte);
3545 *(p1+len) = 0; 3548 *(p1+len) = 0;
3546 3549
3547 len = SCHARS (s2); i = i_byte = 0; 3550 len = SCHARS (s2); i = i_byte = 0;
3548 p2 = (wchar_t *) SAFE_ALLOCA ((len+1) * (sizeof *p2)); 3551 SAFE_NALLOCA (p2, 1, len + 1);
3549 while (i < len) 3552 while (i < len)
3550 FETCH_STRING_CHAR_ADVANCE (*(p2+i-1), s2, i, i_byte); 3553 FETCH_STRING_CHAR_ADVANCE (*(p2+i-1), s2, i, i_byte);
3551 *(p2+len) = 0; 3554 *(p2+len) = 0;
3552 3555
3553#if defined (HAVE_USELOCALE) || defined (HAVE_SETLOCALE)
3554 /* Create a new locale object, and set it. */ 3556 /* Create a new locale object, and set it. */
3555 lc_collate = 3557 lc_collate =
3556 Fgetenv_internal (build_string ("LC_COLLATE"), Vprocess_environment); 3558 Fgetenv_internal (build_string ("LC_COLLATE"), Vprocess_environment);
3557 3559
3558#ifdef HAVE_USELOCALE
3559 if (STRINGP (lc_collate)
3560 && (loc = newlocale (LC_COLLATE_MASK, SSDATA (lc_collate), (locale_t) 0)))
3561 oldloc = uselocale (loc);
3562#elif defined (HAVE_SETLOCALE)
3563 if (STRINGP (lc_collate)) 3560 if (STRINGP (lc_collate))
3564 { 3561 {
3565 oldloc = xstrdup (setlocale (LC_COLLATE, NULL)); 3562#ifdef HAVE_USELOCALE
3566 setlocale (LC_COLLATE, SSDATA (lc_collate)); 3563 loc = newlocale (LC_COLLATE_MASK, SSDATA (lc_collate), 0);
3564 if (loc)
3565 oldloc = uselocale (loc);
3566#else
3567 oldloc = setlocale (LC_COLLATE, NULL);
3568 if (oldloc)
3569 {
3570 oldloc = xstrdup (oldloc);
3571 setlocale (LC_COLLATE, SSDATA (lc_collate));
3572 }
3573#endif
3567 } 3574 }
3568#endif /* HAVE_USELOCALE */
3569#endif /* HAVE_USELOCALE || HAVE_SETLOCALE */
3570 3575
3571 res = wcscoll (p1, p2); 3576 res = wcscoll (p1, p2);
3572 3577
@@ -3576,11 +3581,11 @@ str_collate (Lisp_Object s1, Lisp_Object s2)
3576 freelocale (loc); 3581 freelocale (loc);
3577 if (oldloc) 3582 if (oldloc)
3578 uselocale (oldloc); 3583 uselocale (oldloc);
3579#elif defined (HAVE_SETLOCALE) 3584#else
3580 /* Restore the original locale. */ 3585 /* Restore the original locale. */
3581 if (oldloc) 3586 setlocale (LC_COLLATE, oldloc);
3582 setlocale (LC_COLLATE, oldloc); 3587 xfree (oldloc);
3583#endif /* HAVE_USELOCALE */ 3588#endif
3584 3589
3585 /* Return result. */ 3590 /* Return result. */
3586 SAFE_FREE (); 3591 SAFE_FREE ();