Always terminate unicode strings master 0.3.1
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 9 Feb 2010 00:42:09 +0000 (00:42 +0000)
committerJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 9 Feb 2010 00:42:09 +0000 (00:42 +0000)
whelk/unicode.c

index 763f978..1b688d2 100644 (file)
@@ -8,11 +8,15 @@ char *whelk_utf16_to_utf8(const WCHAR *utf16,int len)
     int n;
     char *utf8;
     n=WideCharToMultiByte(CP_UTF8,0,utf16,len,NULL,0,NULL,NULL);
-    if (n)
+    if (n || !len)
     {
-       utf8=malloc(n);
+       utf8=malloc(len<0?n:n+1);
        if (utf8)
+       {
            WideCharToMultiByte(CP_UTF8,0,utf16,len,utf8,n,NULL,NULL);
+           if (len>=0)
+               utf8[n]=0;
+       }
     }
     else
        utf8=NULL;
@@ -24,11 +28,15 @@ WCHAR *whelk_utf8_to_utf16(const char *utf8,int len)
     int n;
     WCHAR *utf16;
     n=MultiByteToWideChar(CP_UTF8,0,utf8,len,NULL,0);
-    if (n)
+    if (n || !len)
     {
-       utf16=malloc(n*sizeof(WCHAR));
+       utf16=malloc((len<0?n:n+1)*sizeof(WCHAR));
        if (utf16)
+       {
            MultiByteToWideChar(CP_UTF8,0,utf8,len,utf16,n);
+           if (len>=0)
+               utf16[n]=0;
+       }
     }
     else
        utf16=NULL;