From: J. Ali Harlow Date: Tue, 9 Feb 2010 00:42:09 +0000 (+0000) Subject: Always terminate unicode strings X-Git-Tag: 0.3.1^0 X-Git-Url: http://project.juiblex.co.uk/git/?a=commitdiff_plain;h=196ba0eb4000400ad966fe14259634931e604b8a;p=whelk.git Always terminate unicode strings --- diff --git a/whelk/unicode.c b/whelk/unicode.c index 763f978..1b688d2 100644 --- a/whelk/unicode.c +++ b/whelk/unicode.c @@ -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;