eina unicode - fix calloc stupidity

in looking at CID 1230994 i noticed that eina_unicode_unicode_to_utf8
and eina_unicode_utf8_to_unicode are really dumb. they calloc an array
of bytes then proceed to fill them all in anyway. why? also the
realloc handing in eina_unicode_unicode_to_utf8 wasn't strictly corect
and could leak memory. so this just fixes silly slow code and a leak.
This commit is contained in:
Carsten Haitzler 2014-08-13 09:05:37 +09:00
parent d539152156
commit 6dc52db881
1 changed files with 13 additions and 9 deletions

View File

@ -327,9 +327,8 @@ eina_unicode_utf8_to_unicode(const char *utf, int *_len)
EINA_SAFETY_ON_NULL_RETURN_VAL(utf, NULL);
len = eina_unicode_utf8_get_len(utf);
if (_len)
*_len = len;
buf = (Eina_Unicode *) calloc(sizeof(Eina_Unicode), (len + 1));
if (_len) *_len = len;
buf = malloc(sizeof(Eina_Unicode) * (len + 1));
if (!buf) return buf;
for (i = 0, ind = 0, uind = buf ; i < len ; i++, uind++)
@ -343,7 +342,7 @@ eina_unicode_utf8_to_unicode(const char *utf, int *_len)
EAPI char *
eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
{
char *buf;
char *buf, *buf2;
const Eina_Unicode *uind;
char *ind;
int ulen, len;
@ -351,7 +350,8 @@ eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
EINA_SAFETY_ON_NULL_RETURN_VAL(uni, NULL);
ulen = eina_unicode_strlen(uni);
buf = (char *) calloc(ulen + 1, EINA_UNICODE_UTF8_BYTES_PER_CHAR);
buf = malloc((ulen + 1) * EINA_UNICODE_UTF8_BYTES_PER_CHAR);
if (!buf) return NULL;
len = 0;
for (uind = uni, ind = buf ; *uind ; uind++)
@ -416,10 +416,14 @@ eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
/* Do something */
}
}
buf = realloc(buf, len + 1);
buf[len] = '\0';
if (_len)
*_len = len;
buf2 = realloc(buf, len + 1);
if (!buf2)
{
free(buf);
return NULL;
}
buf2[len] = 0;
if (_len) *_len = len;
return buf;
}