Fix for segfault

main^2
Jef Roosens 2022-02-19 21:41:26 +01:00
parent 6c435c2a1e
commit 57c4af0aaf
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 39 additions and 38 deletions

View File

@ -3,5 +3,5 @@ module main
import docker import docker
fn build() { fn build() {
println(docker.containers() or { panic("yeet") }) println(docker.containers() or { panic('yeet') })
} }

View File

@ -9,7 +9,7 @@ struct Container {
} }
pub fn containers() ?[]Container { pub fn containers() ?[]Container {
res := docker.get(urllib.parse('/containers/json') ?) ? res := get(urllib.parse('/containers/json') ?) ?
return json.decode([]Container, res.text) return json.decode([]Container, res.text) or {}
} }

View File

@ -5,16 +5,19 @@ import net.urllib
import net.http import net.http
const socket = '/var/run/docker.sock' const socket = '/var/run/docker.sock'
const buf_len = 1024 const buf_len = 1024
fn request(method string, url urllib.URL) ?http.Response { fn request(method string, url urllib.URL) ?http.Response {
req := "$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n" req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
// Open a connection to the socket // Open a connection to the socket
mut s := unix.connect_stream(socket) ? mut s := unix.connect_stream(docker.socket) ?
defer { defer {
s.close() ? // This or is required because otherwise, the V compiler segfaults for
// some reason
s.close() or {}
} }
// Write the request to the socket // Write the request to the socket
@ -23,18 +26,16 @@ fn request(method string, url urllib.URL) ?http.Response {
// Wait for the server to respond // Wait for the server to respond
s.wait_for_write() ? s.wait_for_write() ?
mut buf := []byte{len: buf_len} mut buf := []byte{len: docker.buf_len}
mut res := []byte{} mut res := []byte{}
mut c := 0 mut c := 0
for { for {
c = s.read(mut buf) or { c = s.read(mut buf) or { return error('Failed to read data from socket.') }
return error('Failed to read data from socket.')
}
res << buf[..c] res << buf[..c]
if c < buf_len { if c < docker.buf_len {
break break
} }
} }