ruby-changes:30332
From: nobu <ko1@a...>
Date: Mon, 5 Aug 2013 17:33:42 +0900 (JST)
Subject: [ruby-changes:30332] nobu:r42384 (trunk): win32.c: fix wrong trimming
nobu 2013-08-05 17:33:22 +0900 (Mon, 05 Aug 2013) New Revision: 42384 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42384 Log: 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. Modified files: trunk/ChangeLog trunk/win32/win32.c Index: ChangeLog =================================================================== --- ChangeLog (revision 42383) +++ ChangeLog (revision 42384) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Aug 5 17:33:18 2013 Nobuyoshi Nakada <nobu@r...> + + * 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. + Mon Aug 5 11:51:00 2013 Charlie Somerville <charliesome@r...> * include/ruby/encoding.h: document which user flags are used by Index: win32/win32.c =================================================================== --- win32/win32.c (revision 42383) +++ win32/win32.c (revision 42384) @@ -1954,10 +1954,14 @@ static char * https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L1954 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 * https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L1970 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; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/