]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - iconvdata/uhc.c
CVE-2014-6040: Crashes on invalid input in IBM gconv modules [BZ #17325]
[thirdparty/glibc.git] / iconvdata / uhc.c
index ac4be8a5aa27bbfae7dc3d2de2ca437e9f0cf482..629f2d627c2c69c25d2eef98588f37356dbd70bc 100644 (file)
@@ -1,23 +1,23 @@
 /* Mapping tables for UHC handling.
-   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1998-2014 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Jungshik Shin <jshin@pantheon.yale.edu>, 1998.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
+#include <dlfcn.h>
 #include <stdint.h>
 #include <ksc5601.h>
 
@@ -3044,6 +3044,7 @@ static const char uhc_hangul_from_ucs[11172][2] =
 #define MIN_NEEDED_FROM                1
 #define MAX_NEEDED_FROM                2
 #define MIN_NEEDED_TO          4
+#define ONE_DIRECTION          0
 
 /* First define the conversion function from UHC to UCS4.  */
 #define MIN_NEEDED_INPUT       MIN_NEEDED_FROM
@@ -3063,19 +3064,20 @@ static const char uhc_hangul_from_ucs[11172][2] =
 */                                                                           \
     if (ch <= 0x7f)                                                          \
       ++inptr;                                                               \
-    else if (ch <= 0x80 || ch >= 0xfe || ch == 0xc9)                         \
+    else if (__builtin_expect (ch <= 0x80, 0)                                \
+            || __builtin_expect (ch >= 0xfe, 0)                              \
+            || __builtin_expect (ch == 0xc9, 0))                             \
       {                                                                              \
        /* This is illegal.  */                                               \
-       result = __GCONV_ILLEGAL_INPUT;                                       \
-       break;                                                                \
+       STANDARD_FROM_LOOP_ERR_HANDLER (1);                                   \
       }                                                                              \
     else                                                                     \
       {                                                                              \
-       /* Two-byte character.  First test whether the next character         \
+       /* Two-byte character.  First test whether the next byte              \
           is also available.  */                                             \
        uint32_t ch2;                                                         \
                                                                              \
-       if (NEED_LENGTH_TEST && inptr + 1 >= inend)                           \
+       if (__glibc_unlikely (inptr + 1 >= inend))                            \
          {                                                                   \
            /* The second character is not available.  Store                  \
               the intermediate result.  */                                   \
@@ -3105,12 +3107,15 @@ static const char uhc_hangul_from_ucs[11172][2] =
                                                                              \
        if (ch < 0xa1 || ch2 < 0xa1)                                          \
          {                                                                   \
-           if (ch > 0xc6 || ch2 <0x41 || (ch2 > 0x5a && ch2 < 0x61)          \
-               || (ch2 > 0x7a && ch2 < 0x81) || (ch == 0xc6 && ch2 > 0x52))  \
+           if (__builtin_expect (ch > 0xc6, 0)                               \
+               || __builtin_expect (ch2 < 0x41, 0)                           \
+               || __builtin_expect (ch2 > 0xfe, 0)                           \
+               || (__builtin_expect (ch2 > 0x5a, 0) && ch2 < 0x61)           \
+               || (__builtin_expect (ch2 > 0x7a, 0) && ch2 < 0x81)           \
+               || (__builtin_expect (ch == 0xc6, 0) && ch2 > 0x52))          \
              {                                                               \
                /* This is not legal.  */                                     \
-               result = __GCONV_ILLEGAL_INPUT;                               \
-               break;                                                        \
+               STANDARD_FROM_LOOP_ERR_HANDLER (1);                           \
              }                                                               \
                                                                              \
            ch = uhc_extra_to_ucs[ch2 - 0x41                                  \
@@ -3119,11 +3124,10 @@ static const char uhc_hangul_from_ucs[11172][2] =
                                     ? (ch - 0x81) * 178                      \
                                     : 5696 + (ch - 0xa1) * 84)];             \
                                                                              \
-           if (ch == 0)                                                      \
+           if (__glibc_unlikely (ch == 0))                                   \
              {                                                               \
                /* This is an illegal character.  */                          \
-               result = __GCONV_ILLEGAL_INPUT;                               \
-               break;                                                        \
+               STANDARD_FROM_LOOP_ERR_HANDLER (2);                           \
              }                                                               \
                                                                              \
            inptr += 2;                                                       \
@@ -3131,16 +3135,25 @@ static const char uhc_hangul_from_ucs[11172][2] =
        else                                                                  \
          {                                                                   \
            ch = ksc5601_to_ucs4 (&inptr, 2, 0x80);                           \
-           if (ch == __UNKNOWN_10646_CHAR)                                   \
+           if (__builtin_expect (ch == __UNKNOWN_10646_CHAR, 0)              \
+               || __builtin_expect (ch == 0x327e, 0))                        \
              {                                                               \
                /* Illegal.  */                                               \
-               result = __GCONV_ILLEGAL_INPUT;                               \
-               break;                                                        \
+               STANDARD_FROM_LOOP_ERR_HANDLER (2);                           \
              }                                                               \
          }                                                                   \
       }                                                                              \
                                                                              \
-    *((uint32_t *) outptr)++ = ch;                                           \
+    put32 (outptr, ch);                                                              \
+    outptr += 4;                                                             \
+  }
+#define LOOP_NEED_FLAGS
+#define ONEBYTE_BODY \
+  {                                                                          \
+    if (c < 0x80)                                                            \
+      return c;                                                                      \
+    else                                                                     \
+      return WEOF;                                                           \
   }
 #include <iconv/loop.c>
 
@@ -3152,16 +3165,16 @@ static const char uhc_hangul_from_ucs[11172][2] =
 #define LOOPFCT                        TO_LOOP
 #define BODY \
   {                                                                          \
-    uint32_t ch = *((uint32_t *) inptr);                                     \
+    uint32_t ch = get32 (inptr);                                             \
                                                                              \
-    if (ch < 0x7f)                                                           \
+    if (ch <= 0x7f)                                                          \
       /* XXX Think about 0x5c ; '\'.  */                                     \
-      *outptr = ch;                                                          \
+      *outptr++ = ch;                                                        \
     else if (ch >= 0xac00 && ch <= 0xd7a3)                                   \
       {                                                                              \
        const char *s = uhc_hangul_from_ucs[ch - 0xac00];                     \
                                                                              \
-       if (NEED_LENGTH_TEST && outptr + 2 > outend)                          \
+       if (__glibc_unlikely (outptr + 2 > outend))                           \
          {                                                                   \
            result = __GCONV_FULL_OUTPUT;                                     \
            break;                                                            \
@@ -3172,19 +3185,16 @@ static const char uhc_hangul_from_ucs[11172][2] =
       }                                                                              \
     else if ((ch >= 0x4e00 && ch <= 0x9fa5) || (ch >= 0xf900 && ch <= 0xfa0b))\
       {                                                                              \
-       size_t written = ucs4_to_ksc5601_hanja (ch, outptr,                   \
-                                               (NEED_LENGTH_TEST             \
-                                                ? outend - outptr : 2));     \
+       size_t written = ucs4_to_ksc5601_hanja (ch, outptr, outend - outptr); \
                                                                              \
-       if (NEED_LENGTH_TEST && written == 0)                                 \
+       if (__glibc_unlikely (written == 0))                                  \
          {                                                                   \
            result = __GCONV_FULL_OUTPUT;                                     \
            break;                                                            \
          }                                                                   \
-       if (written == __UNKNOWN_10646_CHAR)                                  \
+       if (__glibc_unlikely (written == __UNKNOWN_10646_CHAR))               \
          {                                                                   \
-           result = __GCONV_ILLEGAL_INPUT;                                   \
-           break;                                                            \
+           STANDARD_TO_LOOP_ERR_HANDLER (4);                                 \
          }                                                                   \
                                                                              \
        *outptr++ |= 0x80;                                                    \
@@ -3196,18 +3206,17 @@ static const char uhc_hangul_from_ucs[11172][2] =
 */                                                                           \
     else                                                                     \
       {                                                                              \
-       size_t written = ucs4_to_ksc5601_sym (ch, outptr,                     \
-                                             (NEED_LENGTH_TEST               \
-                                              ? outend - outptr : 2));       \
+       size_t written = ucs4_to_ksc5601_sym (ch, outptr, outend - outptr);   \
                                                                              \
-       if (NEED_LENGTH_TEST && written == 0)                                 \
+       if (__builtin_expect (ch == 0x327e, 0)                                \
+           || __builtin_expect (written == __UNKNOWN_10646_CHAR, 0))         \
          {                                                                   \
-           result = __GCONV_FULL_OUTPUT;                                     \
-           break;                                                            \
+           UNICODE_TAG_HANDLER (ch, 4);                                      \
+           STANDARD_TO_LOOP_ERR_HANDLER (4);                                 \
          }                                                                   \
-       if (written == __UNKNOWN_10646_CHAR)                                  \
+       if (__glibc_unlikely (written == 0))                                  \
          {                                                                   \
-           result = __GCONV_ILLEGAL_INPUT;                                   \
+           result = __GCONV_FULL_OUTPUT;                                     \
            break;                                                            \
          }                                                                   \
                                                                              \
@@ -3217,6 +3226,7 @@ static const char uhc_hangul_from_ucs[11172][2] =
                                                                              \
     inptr += 4;                                                                      \
   }
+#define LOOP_NEED_FLAGS
 #include <iconv/loop.c>