From 5c57185060ce9863119d310ed380772866cb21bb Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Sun, 3 Apr 2011 23:05:20 -0500 Subject: [PATCH] msvcrt: Ensure that old buffer contents are copied when allocating a growable pf_output buffer for the first time. --- dlls/msvcrt/wcs.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/dlls/msvcrt/wcs.c b/dlls/msvcrt/wcs.c index a7c7a77775..fc77656f1a 100644 --- a/dlls/msvcrt/wcs.c +++ b/dlls/msvcrt/wcs.c @@ -429,19 +429,39 @@ static inline int pf_check_auto_grow(pf_output *out, unsigned delta) out->len = max(out->len * 2, out->used + delta); if (out->unicode) { + WCHAR *ptr; + if (out->buf.W != out->grow.W) - out->buf.W = MSVCRT_realloc(out->buf.W, out->len * sizeof(WCHAR)); + { + if (!(ptr = MSVCRT_realloc(out->buf.W, out->len * sizeof(WCHAR)))) + return -1; + } else - out->buf.W = MSVCRT_malloc(out->len * sizeof(WCHAR)); - if (!out->buf.W) return -1; + { + if (!(ptr = MSVCRT_malloc(out->len * sizeof(WCHAR)))) + return -1; + memcpy(ptr, out->buf.W, out->used * sizeof(WCHAR)); + } + + out->buf.W = ptr; } else { + char *ptr; + if (out->buf.A != out->grow.A) - out->buf.A = MSVCRT_realloc(out->buf.A, out->len * sizeof(char)); + { + if (!(ptr = MSVCRT_realloc(out->buf.A, out->len * sizeof(char)))) + return -1; + } else - out->buf.A = MSVCRT_malloc(out->len * sizeof(char)); - if (!out->buf.A) return -1; + { + if (!(ptr = MSVCRT_malloc(out->len * sizeof(char)))) + return -1; + memcpy(ptr, out->buf.A, out->used * sizeof(char)); + } + + out->buf.A = ptr; } } return 0; -- 2.32.0.93.g670b81a890