1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: fix memleak from getline on Linux (#18022)

This commit is contained in:
Felipe Pena 2023-04-26 16:02:09 -03:00 committed by GitHub
parent e1e5076d94
commit 13b4cd9d58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}
}