From 13b4cd9d5848ecc10b828880bbeca8f204aa33f0 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 26 Apr 2023 16:02:09 -0300 Subject: [PATCH] os: fix memleak from getline on Linux (#18022) --- vlib/os/os.c.v | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/vlib/os/os.c.v b/vlib/os/os.c.v index 8b03e2c643..92927e9eda 100644 --- a/vlib/os/os.c.v +++ b/vlib/os/os.c.v @@ -571,7 +571,14 @@ pub fn get_raw_line() string { max := usize(0) buf := &char(0) nr_chars := unsafe { C.getline(&buf, &max, C.stdin) } - return unsafe { tos(&u8(buf), if nr_chars < 0 { 0 } else { nr_chars }) } + str := unsafe { tos(&u8(buf), if nr_chars < 0 { 0 } else { nr_chars }) } + ret := str.clone() + unsafe { + if nr_chars > 0 && buf != 0 { + C.free(buf) + } + } + return ret } }