os: fix os.execute for command output that contains 0 bytes

pull/12443/head
Delyan Angelov 2021-11-12 09:39:15 +02:00
parent 4b42dcad8e
commit 20d63de136
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 24 additions and 4 deletions

View File

@ -343,16 +343,19 @@ pub fn execute(cmd string) Result {
output: 'exec("$cmd") failed'
}
}
fd := fileno(f)
buf := unsafe { malloc_noscan(4096) }
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }
}
unsafe {
bufbp := buf
for C.fgets(&char(bufbp), 4096, f) != 0 {
buflen := vstrlen(bufbp)
res.write_ptr(bufbp, buflen)
for {
len := C.read(fd, buf, 4096)
if len == 0 {
break
}
res.write_ptr(buf, len)
}
}
soutput := res.str()

View File

@ -841,3 +841,20 @@ fn test_expand_tilde_to_home() {
assert home_test == home_expansion_test
assert os.expand_tilde_to_home('~') == os.home_dir()
}
fn test_execute() {
$if !linux {
return
}
// The output of the next command contains a 0 byte in the middle.
// Nevertheless, the execute function *should* return a string that
// contains it:
result := os.execute('printenv --null LANGUAGE LANG')
hexresult := result.output.bytes().hex()
println(result.exit_code)
println(result.output.len)
println(hexresult)
assert result.exit_code == 0
assert result.output.len > 0
assert hexresult.contains('00')
}