[PATCH v4 0/1] MR2131: vbscript: Fix memory leak in Split().
While running in XCode's profiler, I noticed a memory leak in `Global_Split`. When the strings are being copied into the SafeArray, the `BSTR` is not freed after `VariantCopyInd`. -- v4: vbscript: Fix memory leak in Split() https://gitlab.winehq.org/wine/wine/-/merge_requests/2131
From: Jason Millard <jsm174(a)gmail.com> --- dlls/vbscript/global.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/dlls/vbscript/global.c b/dlls/vbscript/global.c index 8c2e5b85982..7ca2c2b9c72 100644 --- a/dlls/vbscript/global.c +++ b/dlls/vbscript/global.c @@ -2578,12 +2578,12 @@ static HRESULT Global_Join(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, V static HRESULT Global_Split(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res) { - BSTR str, string, delimiter = NULL; + BSTR string, delimiter = NULL; int count, max, mode, len, start, end, ret, delimiterlen = 1; int i, *indices = NULL, *new_indices, indices_max = 8; SAFEARRAYBOUND bounds; SAFEARRAY *sa = NULL; - VARIANT *data, var; + VARIANT *data; HRESULT hres = S_OK; TRACE("%s %u...\n", debugstr_variant(args), args_cnt); @@ -2691,19 +2691,13 @@ static HRESULT Global_Split(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, start = 0; for (i = 0; i < count; i++) { - str = SysAllocStringLen(string + start, indices[i] - start); - if (!str) { + V_VT(&data[i]) = VT_BSTR; + V_BSTR(&data[i]) = SysAllocStringLen(string + start, indices[i] - start); + + if (!V_BSTR(&data[i])) { hres = E_OUTOFMEMORY; break; } - V_VT(&var) = VT_BSTR; - V_BSTR(&var) = str; - - hres = VariantCopyInd(data+i, &var); - if(FAILED(hres)) { - SafeArrayUnaccessData(sa); - goto error; - } start = indices[i]+delimiterlen; } SafeArrayUnaccessData(sa); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/2131
Updated again to remove `str` and `var` variables. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/2131#note_23455
This merge request was approved by Jacek Caban. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/2131
participants (3)
-
Jacek Caban (@jacek) -
Jason Millard -
Jason Millard (@jsm174)