From b6ca10cd5eec06bc0f9d875cbff29d602fb01bf1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 16 Jan 2023 15:29:45 +0000 Subject: [PATCH] Fix memory leak in `os.get_current_directory` on failure on *nix systems --- core/os/os_darwin.odin | 1 + core/os/os_freebsd.odin | 1 + core/os/os_linux.odin | 1 + core/os/os_openbsd.odin | 1 + 4 files changed, 4 insertions(+) diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin index 456ac1d76..2d8a871c1 100644 --- a/core/os/os_darwin.odin +++ b/core/os/os_darwin.odin @@ -701,6 +701,7 @@ get_current_directory :: proc() -> string { return string(cwd) } if Errno(get_last_error()) != ERANGE { + delete(buf) return "" } resize(&buf, len(buf)+page_size) diff --git a/core/os/os_freebsd.odin b/core/os/os_freebsd.odin index eacbae97a..0ab48efaf 100644 --- a/core/os/os_freebsd.odin +++ b/core/os/os_freebsd.odin @@ -651,6 +651,7 @@ get_current_directory :: proc() -> string { return string(cwd) } if Errno(get_last_error()) != ERANGE { + delete(buf) return "" } resize(&buf, len(buf)+page_size) diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin index 6f2196504..1a66f7704 100644 --- a/core/os/os_linux.odin +++ b/core/os/os_linux.odin @@ -823,6 +823,7 @@ get_current_directory :: proc() -> string { return strings.string_from_nul_terminated_ptr(&buf[0], len(buf)) } if _get_errno(res) != ERANGE { + delete(buf) return "" } resize(&buf, len(buf)+page_size) diff --git a/core/os/os_openbsd.odin b/core/os/os_openbsd.odin index 3423c1692..75ad19c46 100644 --- a/core/os/os_openbsd.odin +++ b/core/os/os_openbsd.odin @@ -649,6 +649,7 @@ get_current_directory :: proc() -> string { return string(cwd) } if Errno(get_last_error()) != ERANGE { + delete(buf) return "" } resize(&buf, len(buf) + MAX_PATH)