Changeset 1521


Ignore:
Timestamp:
Sep 27, 2004, 7:12:09 AM (21 years ago)
Author:
bird
Message:

Fixed some potential overwrites in codepage conversion.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/src/libiconv/iconv.c

    • Property cvs2svn:cvs-rev changed from 1.6 to 1.7
    r1520 r1521  
    7878iconv_open (const char *cp_to, const char *cp_from)
    7979{
    80   iconv_t conv;
    81   uconv_attribute_t attr;
    82   FS_VAR();
    83 
    84   conv = (iconv_t) calloc (sizeof (struct __libc_iconv_s), 1);
    85   if (conv == NULL)
    86     {
    87       errno = ENOMEM;
    88       return (iconv_t)(-1);
    89     }
    90 
    91   FS_SAVE_LOAD();
    92   conv->ucp_from = (UniChar *) malloc ((strlen (cp_from) + 2 + 1) * sizeof (UniChar));
    93   if (conv->ucp_from == NULL)
    94     {
    95       free (conv);
    96       errno = ENOMEM;
    97       FS_RESTORE();
    98       return (iconv_t)(-1);
    99     }
    100   __libc_TranslateCodepage (cp_from, conv->ucp_from);
    101   if (UniCreateUconvObject (conv->ucp_from, &conv->from))
    102     {
    103       free (conv->ucp_from);
    104       free (conv);
    105       errno = EINVAL;
    106       FS_RESTORE();
    107       return (iconv_t)(-1);
    108     }
    109 
    110   conv->ucp_to = (UniChar *) malloc ((strlen (cp_to) + 2 + 1) * sizeof (UniChar));
    111   if (conv->ucp_to == NULL)
    112     {
    113       UniFreeUconvObject (conv->from);
    114       free (conv->ucp_from);
    115       free (conv);
    116       errno = ENOMEM;
    117       FS_RESTORE();
    118       return (iconv_t)(-1);
    119     }
    120   __libc_TranslateCodepage (cp_to, conv->ucp_to);
    121   if (UniCreateUconvObject (conv->ucp_to, &conv->to))
    122     {
    123       UniFreeUconvObject (conv->from);
    124       free (conv->ucp_from);
    125       free (conv->ucp_to);
    126       free (conv);
    127       errno = EINVAL;
    128       FS_RESTORE();
    129       return (iconv_t)(-1);
    130     }
    131 
    132   UniQueryUconvObject (conv->from, &attr, sizeof (attr), NULL, NULL, NULL);
    133   /* Do not treat 0x7f as a control character
    134      (don't understand what it exactly means but without it MBCS prefix
    135      character detection sometimes could fail (when 0x7f is a prefix)).
    136      And don't treat the string as a path (the docs also don't explain
    137      what it exactly means, but I'm pretty sure converted texts will
    138      mostly not be paths).  */
    139   attr.converttype &= ~(CVTTYPE_CTRL7F | CVTTYPE_PATH);
    140   UniSetUconvObject (conv->from, &attr);
    141 
    142   _smutex_request(&gsmtxIconv);
    143   conv->pPrev = NULL;
    144   conv->pNext = gIconvHead;
    145   if (conv->pNext)
    146     conv->pNext->pPrev = conv;
    147   gIconvHead = conv;
    148   _smutex_release(&gsmtxIconv);
    149 
    150   FS_RESTORE();
    151   return conv;
     80    iconv_t conv = (iconv_t) calloc (sizeof (struct __libc_iconv_s), 1);
     81    if (conv)
     82    {
     83        /* add a good bit of room for cp conversion. */
     84        size_t cb = (strlen(cp_from) + 7) * sizeof(UniChar);
     85        if (cb < 48)
     86            cb = 48;
     87        conv->ucp_from = (UniChar *)malloc(cb);
     88        if (conv->ucp_from)
     89        {
     90            FS_VAR();
     91            FS_SAVE_LOAD();
     92            __libc_TranslateCodepage(cp_from, conv->ucp_from);
     93            if (!UniCreateUconvObject(conv->ucp_from, &conv->from))
     94            {
     95                /* add a good bit of room for cp conversion. */
     96                cb = (strlen(cp_to) + 7) * sizeof(UniChar);
     97                if (cb < 48)
     98                    cb = 48;
     99                conv->ucp_to = (UniChar *)malloc(cb);
     100                if (conv->ucp_to)
     101                {
     102                    __libc_TranslateCodepage(cp_to, conv->ucp_to);
     103                    if (!UniCreateUconvObject(conv->ucp_to, &conv->to))
     104                    {
     105                        /* Do not treat 0x7f as a control character
     106                           (don't understand what it exactly means but without it MBCS prefix
     107                           character detection sometimes could fail (when 0x7f is a prefix)).
     108                           And don't treat the string as a path (the docs also don't explain
     109                           what it exactly means, but I'm pretty sure converted texts will
     110                           mostly not be paths).  */
     111                        uconv_attribute_t   attr;
     112                        UniQueryUconvObject(conv->from, &attr, sizeof (attr), NULL, NULL, NULL);
     113                        attr.converttype &= ~(CVTTYPE_CTRL7F | CVTTYPE_PATH);
     114                        UniSetUconvObject(conv->from, &attr);
     115
     116                        _smutex_request(&gsmtxIconv);
     117                        conv->pPrev = NULL;
     118                        conv->pNext = gIconvHead;
     119                        if (conv->pNext)
     120                            conv->pNext->pPrev = conv;
     121                        gIconvHead = conv;
     122                        _smutex_release(&gsmtxIconv);
     123
     124                        FS_RESTORE();
     125                        return conv;
     126                    }
     127                    else
     128                        errno = EINVAL;
     129                    free(conv->ucp_to);
     130                }
     131                else
     132                    errno = ENOMEM;
     133                UniFreeUconvObject(conv->from);
     134            }
     135            else
     136                errno = EINVAL;
     137            FS_RESTORE();
     138            free(conv->ucp_from);
     139        }
     140        else
     141            errno = ENOMEM;
     142        free(conv);
     143    }
     144    else
     145        errno = ENOMEM;
     146
     147    return (iconv_t)(-1);
    152148}
    153149
Note: See TracChangeset for help on using the changeset viewer.