Print this page
5366 strcoll_l may destroy its arguments, then crash


  38 #include "localeimpl.h"
  39 #include "collate.h"
  40 
  41 #define ALLOCA_LIMIT    16
  42 
  43 /*
  44  * In order to properly handle multibyte locales, its easiet to just
  45  * convert to wide characters and then use wcscoll.  However if an
  46  * error occurs, we gracefully fall back to simple strcmp.  Caller
  47  * should check errno.
  48  */
  49 int
  50 strcoll_l(const char *s1, const char *s2, locale_t loc)
  51 {
  52         int ret;
  53         wchar_t *t1 = NULL, *t2 = NULL;
  54         wchar_t *w1 = NULL, *w2 = NULL;
  55         size_t sz1, sz2;
  56         const struct lc_collate *lcc = loc->collate;
  57 
  58         mbstate_t mbs1 = { 0 }; /* initial states */
  59         mbstate_t mbs2 = { 0 };
  60 
  61         if (lcc->lc_is_posix)
  62                 return (strcmp(s1, s2));
  63 
  64         sz1 = strlen(s1) + 1;
  65         sz2 = strlen(s2) + 1;
  66 
  67         /*
  68          * Simple assumption: conversion to wide format is strictly
  69          * reducing, i.e. a single byte (or multibyte character)
  70          * cannot result in multiple wide characters.
  71          *
  72          * We gain a bit of performance by giving preference to alloca
  73          * for small string allocations.
  74          */
  75         if (sz1 > ALLOCA_LIMIT) {
  76                 if ((t1 = malloc(sz1 * sizeof (wchar_t))) == NULL)
  77                         goto error;
  78                 w1 = t1;
  79         } else {
  80                 if ((w1 = alloca(sz1 * sizeof (wchar_t))) == NULL)
  81                         goto error;
  82         }
  83         if (sz2 > ALLOCA_LIMIT) {
  84                 if ((t2 = malloc(sz2 * sizeof (wchar_t))) == NULL)
  85                         goto error;
  86                 w2 = t2;
  87         } else {
  88                 if ((w2 = alloca(sz2 * sizeof (wchar_t))) == NULL)
  89                         goto error;
  90         }
  91 
  92         if ((mbsrtowcs_l(w1, &s1, sz1, &mbs1, loc)) == (size_t)-1)
  93                 goto error;
  94 
  95         if ((mbsrtowcs_l(w2, &s2, sz2, &mbs2, loc)) == (size_t)-1)
  96                 goto error;
  97 
  98         ret = wcscoll_l(w1, w2, loc);
  99         if (t1)
 100                 free(t1);
 101         if (t2)
 102                 free(t2);
 103 
 104         return (ret);
 105 
 106 error:
 107         if (t1)
 108                 free(t1);
 109         if (t2)
 110                 free(t2);
 111         return (strcmp(s1, s2));
 112 }
 113 
 114 int
 115 strcoll(const char *s1, const char *s2)


  38 #include "localeimpl.h"
  39 #include "collate.h"
  40 
  41 #define ALLOCA_LIMIT    16
  42 
  43 /*
  44  * In order to properly handle multibyte locales, its easiet to just
  45  * convert to wide characters and then use wcscoll.  However if an
  46  * error occurs, we gracefully fall back to simple strcmp.  Caller
  47  * should check errno.
  48  */
  49 int
  50 strcoll_l(const char *s1, const char *s2, locale_t loc)
  51 {
  52         int ret;
  53         wchar_t *t1 = NULL, *t2 = NULL;
  54         wchar_t *w1 = NULL, *w2 = NULL;
  55         size_t sz1, sz2;
  56         const struct lc_collate *lcc = loc->collate;
  57 



  58         if (lcc->lc_is_posix)
  59                 return (strcmp(s1, s2));
  60 
  61         sz1 = strlen(s1) + 1;
  62         sz2 = strlen(s2) + 1;
  63 
  64         /*
  65          * Simple assumption: conversion to wide format is strictly
  66          * reducing, i.e. a single byte (or multibyte character)
  67          * cannot result in multiple wide characters.
  68          *
  69          * We gain a bit of performance by giving preference to alloca
  70          * for small string allocations.
  71          */
  72         if (sz1 > ALLOCA_LIMIT) {
  73                 if ((t1 = malloc(sz1 * sizeof (wchar_t))) == NULL)
  74                         goto error;
  75                 w1 = t1;
  76         } else {
  77                 if ((w1 = alloca(sz1 * sizeof (wchar_t))) == NULL)
  78                         goto error;
  79         }
  80         if (sz2 > ALLOCA_LIMIT) {
  81                 if ((t2 = malloc(sz2 * sizeof (wchar_t))) == NULL)
  82                         goto error;
  83                 w2 = t2;
  84         } else {
  85                 if ((w2 = alloca(sz2 * sizeof (wchar_t))) == NULL)
  86                         goto error;
  87         }
  88 
  89         if ((mbstowcs_l(w1, s1, sz1, loc)) == (size_t)-1)
  90                 goto error;
  91 
  92         if ((mbstowcs_l(w2, s2, sz2, loc)) == (size_t)-1)
  93                 goto error;
  94 
  95         ret = wcscoll_l(w1, w2, loc);
  96         if (t1)
  97                 free(t1);
  98         if (t2)
  99                 free(t2);
 100 
 101         return (ret);
 102 
 103 error:
 104         if (t1)
 105                 free(t1);
 106         if (t2)
 107                 free(t2);
 108         return (strcmp(s1, s2));
 109 }
 110 
 111 int
 112 strcoll(const char *s1, const char *s2)