From 161859621840f1c0e00edd8438300b4039897e4e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 5 Feb 2020 12:23:49 +0200 Subject: [PATCH] os: fix compiler warnings when just importing os --- examples/.gitignore | 1 + vlib/os/os.v | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/.gitignore b/examples/.gitignore index 182d47571f..6f0b11537e 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -1,5 +1,6 @@ /cli /hello_world +/hanoi /json /links_scraper /log diff --git a/vlib/os/os.v b/vlib/os/os.v index 6c7dbb7dea..c62fba2a82 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -119,13 +119,14 @@ pub fn read_file(path string) ?string { if isnil(fp) { return error('failed to open file "$path"') } + defer { C.fclose(fp) } C.fseek(fp, 0, C.SEEK_END) fsize := C.ftell(fp) // C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below C.rewind(fp) - mut str := malloc(fsize + 1) + mut str := &byte(0) + unsafe { str = malloc(fsize + 1) } C.fread(str, fsize, 1, fp) - C.fclose(fp) str[fsize] = 0 return string(str,fsize) } @@ -707,13 +708,15 @@ pub fn get_raw_line() string { } return string(buf, offset) } $else { - max := size_t(256) - buf := charptr(malloc(int(max))) + max := size_t(0) + mut buf := byteptr(0) nr_chars := C.getline(&buf, &max, stdin) - if nr_chars == 0 { + defer { unsafe{ free(buf) } } + if nr_chars == 0 || nr_chars == -1 { return '' } - return string(byteptr(buf),nr_chars) + res := tos_clone( buf ) + return res } }