[PATCH 0/1] MR2976: msvcrt: Fix memory leak on error path in _wgetcwd (scan-build).
From: Alex Henrie <alexhenrie24(a)gmail.com> --- dlls/msvcrt/dir.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dlls/msvcrt/dir.c b/dlls/msvcrt/dir.c index 72c8018a74b..e6723d31fd9 100644 --- a/dlls/msvcrt/dir.c +++ b/dlls/msvcrt/dir.c @@ -819,16 +819,16 @@ wchar_t* CDECL _wgetcwd(wchar_t * buf, int size) if (dir_len < 1) return NULL; /* FIXME: Real return value untested */ - if (!buf) - { - if (size <= dir_len) size = dir_len + 1; - if (!(buf = malloc( size * sizeof(WCHAR) ))) return NULL; - } + if (!buf && size <= dir_len) + size = dir_len + 1; if (dir_len >= size) { *_errno() = ERANGE; return NULL; /* buf too small */ } + if (!buf && !(buf = malloc(size * sizeof(WCHAR)))) + return NULL; + wcscpy(buf,dir); return buf; } -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/2976
Piotr Caban (@piotr) commented about dlls/msvcrt/dir.c:
if (dir_len < 1) return NULL; /* FIXME: Real return value untested */
- if (!buf) - { - if (size <= dir_len) size = dir_len + 1; - if (!(buf = malloc( size * sizeof(WCHAR) ))) return NULL; - } + if (!buf && size <= dir_len) + size = dir_len + 1; if (dir_len >= size) There's no leak here. In case !buf we're updating size if it's to small.
The code can be changed to: ```suggestion:-0+0 else if (dir_len >= size) ``` to make it easier to read. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/2976#note_34643
participants (3)
-
Alex Henrie -
Alex Henrie (@alexhenrie) -
Piotr Caban (@piotr)