Skip to content

Commit 8bc4ab8

Browse files
authored
os: fix get_raw_line for EOF on OpenBSD (fix #26361) (#26368)
1 parent ed95028 commit 8bc4ab8

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

vlib/os/os.c.v

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,21 @@ pub fn get_raw_line() string {
615615
} $else {
616616
max := usize(0)
617617
buf := &u8(unsafe { nil })
618+
619+
mut str := ''
618620
nr_chars := unsafe { C.getline(voidptr(&buf), &max, C.stdin) }
619-
str := unsafe { tos(buf, if nr_chars < 0 { 0 } else { nr_chars }) }
621+
// On OpenBSD, buf=0 for EOF => panic when calling tos function
622+
$if openbsd {
623+
if nr_chars != -1 {
624+
str = unsafe { tos(buf, nr_chars) }
625+
} else {
626+
if int(C.feof(C.stdin)) == 0 && int(C.ferror(C.stdin)) != 0 {
627+
panic('get_raw_line(): error to read string')
628+
}
629+
}
630+
} $else {
631+
str = unsafe { tos(buf, if nr_chars < 0 { 0 } else { nr_chars }) }
632+
}
620633
ret := str.clone()
621634
$if !autofree {
622635
unsafe {

0 commit comments

Comments
 (0)