forked from enlightenment/efl
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:
parent
d539152156
commit
6dc52db881
|
@ -327,9 +327,8 @@ eina_unicode_utf8_to_unicode(const char *utf, int *_len)
|
||||||
EINA_SAFETY_ON_NULL_RETURN_VAL(utf, NULL);
|
EINA_SAFETY_ON_NULL_RETURN_VAL(utf, NULL);
|
||||||
|
|
||||||
len = eina_unicode_utf8_get_len(utf);
|
len = eina_unicode_utf8_get_len(utf);
|
||||||
if (_len)
|
if (_len) *_len = len;
|
||||||
*_len = len;
|
buf = malloc(sizeof(Eina_Unicode) * (len + 1));
|
||||||
buf = (Eina_Unicode *) calloc(sizeof(Eina_Unicode), (len + 1));
|
|
||||||
if (!buf) return buf;
|
if (!buf) return buf;
|
||||||
|
|
||||||
for (i = 0, ind = 0, uind = buf ; i < len ; i++, uind++)
|
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 *
|
EAPI char *
|
||||||
eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
|
eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf, *buf2;
|
||||||
const Eina_Unicode *uind;
|
const Eina_Unicode *uind;
|
||||||
char *ind;
|
char *ind;
|
||||||
int ulen, len;
|
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);
|
EINA_SAFETY_ON_NULL_RETURN_VAL(uni, NULL);
|
||||||
|
|
||||||
ulen = eina_unicode_strlen(uni);
|
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;
|
len = 0;
|
||||||
for (uind = uni, ind = buf ; *uind ; uind++)
|
for (uind = uni, ind = buf ; *uind ; uind++)
|
||||||
|
@ -416,10 +416,14 @@ eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
|
||||||
/* Do something */
|
/* Do something */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf = realloc(buf, len + 1);
|
buf2 = realloc(buf, len + 1);
|
||||||
buf[len] = '\0';
|
if (!buf2)
|
||||||
if (_len)
|
{
|
||||||
*_len = len;
|
free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
buf2[len] = 0;
|
||||||
|
if (_len) *_len = len;
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue