os: fix a file descriptor leak in os.read_bytes() (#10723)

pull/10730/head
waspoza 2021-07-09 22:49:46 +02:00 committed by GitHub
parent 477d442f18
commit 075e09b10e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -71,6 +71,9 @@ struct C.dirent {
[manualfree]
pub fn read_bytes(path string) ?[]byte {
mut fp := vfopen(path, 'rb') ?
defer {
C.fclose(fp)
}
cseek := C.fseek(fp, 0, C.SEEK_END)
if cseek != 0 {
return error('fseek failed')
@ -85,7 +88,6 @@ pub fn read_bytes(path string) ?[]byte {
if nr_read_elements == 0 && fsize > 0 {
return error('fread failed')
}
C.fclose(fp)
res.trim(nr_read_elements * fsize)
return res
}