raster pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=6dc52db881cbe29dfe8b9a2a0a6745a8b3fa0673

commit 6dc52db881cbe29dfe8b9a2a0a6745a8b3fa0673
Author: Carsten Haitzler (Rasterman) <[email protected]>
Date:   Wed Aug 13 09:05:37 2014 +0900

    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.
---
 src/lib/eina/eina_unicode.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/lib/eina/eina_unicode.c b/src/lib/eina/eina_unicode.c
index f8dcc8d..44bfac6 100644
--- a/src/lib/eina/eina_unicode.c
+++ b/src/lib/eina/eina_unicode.c
@@ -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;
 }
 

-- 


Reply via email to