builtin: fix leaks in `os.execute()` and `[]string{} == []string{}`

pull/9440/head
Delyan Angelov 2021-03-23 20:36:19 +02:00
parent d098a3caca
commit 7f91b75cbc
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 22 additions and 7 deletions

View File

@ -685,8 +685,12 @@ pub fn (a1 []string) eq(a2 []string) bool {
if a1.len != a2.len {
return false
}
size_of_string := int(sizeof(string))
for i in 0 .. a1.len {
if a1[i] != a2[i] {
offset := i * size_of_string
s1 := &string(unsafe { byteptr(a1.data) + offset })
s2 := &string(unsafe { byteptr(a2.data) + offset })
if *s1 != *s2 {
return false
}
}

View File

@ -157,6 +157,7 @@ pub fn mkdir(path string) ?bool {
}
// execute starts the specified command, waits for it to complete, and returns its output.
[manualfree]
pub fn execute(cmd string) Result {
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
// return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' }
@ -171,6 +172,9 @@ pub fn execute(cmd string) Result {
}
buf := [4096]byte{}
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }
}
unsafe {
bufbp := &buf[0]
for C.fgets(charptr(bufbp), 4096, f) != 0 {
@ -178,7 +182,6 @@ pub fn execute(cmd string) Result {
}
}
soutput := res.str()
// res.free()
exit_code := vpclose(f)
return Result{
exit_code: exit_code
@ -196,19 +199,25 @@ pub:
redirect_stdout bool
}
// pub fn command(cmd Command) Command {
//}
[manualfree]
pub fn (mut c Command) start() ? {
pcmd := '$c.path 2>&1'
pcmd := c.path + ' 2>&1'
defer {
unsafe { pcmd.free() }
}
c.f = vpopen(pcmd)
if isnil(c.f) {
return error('exec("$c.path") failed')
}
}
[manualfree]
pub fn (mut c Command) read_line() string {
buf := [4096]byte{}
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }
}
unsafe {
bufbp := &buf[0]
for C.fgets(charptr(bufbp), 4096, c.f) != 0 {
@ -216,14 +225,16 @@ pub fn (mut c Command) read_line() string {
for i in 0 .. len {
if bufbp[i] == `\n` {
res.write_ptr(bufbp, i)
return res.str()
final := res.str()
return final
}
}
res.write_ptr(bufbp, len)
}
}
c.eof = true
return res.str()
final := res.str()
return final
}
pub fn (c &Command) close() ? {