From 216ba4f0d3836274c14074084c7c47571dc1f471 Mon Sep 17 00:00:00 2001 From: Zhangrong Huang Date: Tue, 20 Jul 2004 01:21:57 +0000 Subject: [PATCH] Call wine_cp_wcstombs() to retrieve actual length of buffer before conversion. --- tools/wmc/write.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/wmc/write.c b/tools/wmc/write.c index bccb1374962..0932828e6da 100644 --- a/tools/wmc/write.c +++ b/tools/wmc/write.c @@ -94,12 +94,14 @@ static char str_header[] = static char *dup_u2c(int cp, const WCHAR *uc) { - int len = unistrlen(uc); - char *cptr = xmalloc(len+1); + int len; + char *cptr; const union cptable *cpdef = find_codepage(cp); if(!cpdef) internal_error(__FILE__, __LINE__, "Codepage %d not found (vanished?)", cp); - if((len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len+1, NULL, NULL)) < 0) + len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL); + cptr = xmalloc(len); + if((len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len, NULL, NULL)) < 0) internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", len); return cptr; } @@ -377,12 +379,14 @@ static char *make_string(WCHAR *uc, int len, int codepage) } else { - char *tmp = xmalloc(2*len+1); - char *cc = tmp; + char *tmp, *cc; + int mlen; const union cptable *cpdef = find_codepage(codepage); assert(cpdef != NULL); - if((i = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, 2*len+1, NULL, NULL)) < 0) + mlen = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL); + cc = tmp = xmalloc(mlen); + if((i = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, mlen, NULL, NULL)) < 0) internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i); *cptr++ = ' '; *cptr++ = '"';