From 0f6cd1bb10eab7d9a6fd623f9d7ae77cd2d87cef Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 5 Aug 2013 08:33:22 +0000 Subject: win32.c: fix wrong trimming * win32/win32.c (wstr_to_mbstr, mbstr_to_wstr): fix wrong trimming. WideCharToMultiByte() and MultiByteToWideChar() do not count NUL-terminator in the size for conversion result, unless the input length is -1. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42384 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- win32/win32.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'win32') diff --git a/win32/win32.c b/win32/win32.c index 0a03d7bcca..ab2b64a5e4 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -1954,10 +1954,14 @@ static char * wstr_to_mbstr(UINT cp, const WCHAR *wstr, int clen, long *plen) { char *ptr; - int len = WideCharToMultiByte(cp, 0, wstr, clen, NULL, 0, NULL, NULL) - 1; - if (!(ptr = malloc(len + 1))) return 0; - WideCharToMultiByte(cp, 0, wstr, clen, ptr, len + 1, NULL, NULL); - if (plen) *plen = len; + int len = WideCharToMultiByte(cp, 0, wstr, clen, NULL, 0, NULL, NULL); + if (!(ptr = malloc(len))) return 0; + WideCharToMultiByte(cp, 0, wstr, clen, ptr, len, NULL, NULL); + if (plen) { + /* exclude NUL only if NUL-terminated string */ + if (clen == -1) --len; + *plen = len; + } return ptr; } @@ -1966,10 +1970,14 @@ static WCHAR * mbstr_to_wstr(UINT cp, const char *str, int clen, long *plen) { WCHAR *ptr; - int len = MultiByteToWideChar(cp, 0, str, clen, NULL, 0) - 1; - if (!(ptr = malloc(sizeof(WCHAR) * (len + 1)))) return 0; - MultiByteToWideChar(cp, 0, str, clen, ptr, len + 1); - if (plen) *plen = len; + int len = MultiByteToWideChar(cp, 0, str, clen, NULL, 0); + if (!(ptr = malloc(sizeof(WCHAR) * len))) return 0; + MultiByteToWideChar(cp, 0, str, clen, ptr, len); + if (plen) { + /* exclude NUL only if NUL-terminated string */ + if (clen == -1) --len; + *plen = len; + } return ptr; } -- cgit v1.2.3