Skip to content

Commit

Permalink
os: fix double free in os.get_raw_line() (used by os.input), with `-a…
Browse files Browse the repository at this point in the history
…utofree` (#21204)
  • Loading branch information
felipensp committed Apr 8, 2024
1 parent 427b6db commit 3db7b44
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions vlib/os/os.c.v
Expand Up @@ -539,9 +539,11 @@ pub fn get_raw_line() string {
nr_chars := unsafe { C.getline(&buf, &max, C.stdin) }
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)
$if !autofree {
unsafe {
if nr_chars > 0 && buf != 0 {
C.free(buf)
}
}
}
return ret
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/slow_tests/valgrind/free_heap_foos.c.v
Expand Up @@ -31,13 +31,13 @@ fn create_some_foos() {

fn main() {
create_some_foos()
if C._VAUTOFREE == 1 {
$if autofree {
assert frees.len == 2
assert frees[0] == 222
assert frees[1] == 111
}
create_some_foos()
if C._VAUTOFREE == 1 {
$if autofree {
assert frees.len == 4
assert frees[0] == 222
assert frees[1] == 111
Expand Down
Expand Up @@ -25,7 +25,7 @@ fn use_some_returned_variables() {
println(dd)
}
b := test(2)
if C._VAUTOFREE == 1 {
$if autofree {
assert frees.len == 2
assert frees[0] == 444
assert frees[1] == 333
Expand All @@ -35,7 +35,7 @@ fn use_some_returned_variables() {

fn main() {
use_some_returned_variables()
if C._VAUTOFREE == 1 {
$if autofree {
assert frees.len == 4
assert frees[0] == 444
assert frees[1] == 333
Expand Down

0 comments on commit 3db7b44

Please sign in to comment.