1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check unsafe V function calls (#8752)

This commit is contained in:
Nick Treleaven
2021-02-14 18:31:42 +00:00
committed by GitHub
parent d3bcd5d305
commit ea803113c3
36 changed files with 200 additions and 161 deletions

View File

@@ -12,19 +12,21 @@ fn C.FreeEnvironmentStringsW(&u16) int
// `getenv` returns the value of the environment variable named by the key.
pub fn getenv(key string) string {
$if windows {
s := C._wgetenv(key.to_wide())
if s == 0 {
return ''
unsafe {
$if windows {
s := C._wgetenv(key.to_wide())
if s == 0 {
return ''
}
return string_from_wide(s)
} $else {
s := C.getenv(charptr(key.str))
if s == voidptr(0) {
return ''
}
// NB: C.getenv *requires* that the result be copied.
return cstring_to_vstring(byteptr(s))
}
return string_from_wide(s)
} $else {
s := C.getenv(charptr(key.str))
if s == voidptr(0) {
return ''
}
// NB: C.getenv *requires* that the result be copied.
return cstring_to_vstring(byteptr(s))
}
}

View File

@@ -36,14 +36,14 @@ pub fn fd_slurp(fd int) []string {
// read from filedescriptor, don't block
// return [bytestring,nrbytes]
pub fn fd_read(fd int, maxbytes int) (string, int) {
mut buf := malloc(maxbytes)
nbytes := C.read(fd, buf, maxbytes)
if nbytes < 0 {
free(buf)
return '', nbytes
}
unsafe {
mut buf := malloc(maxbytes)
nbytes := C.read(fd, buf, maxbytes)
if nbytes < 0 {
free(buf)
return '', nbytes
}
buf[nbytes] = 0
return tos(buf, nbytes), nbytes
}
return tos(buf, nbytes), nbytes
}

View File

@@ -600,8 +600,10 @@ pub fn resource_abs_path(path string) string {
}
fp := join_path(base_path, path)
res := real_path(fp)
fp.free()
base_path.free()
unsafe {
fp.free()
base_path.free()
}
return res
}

View File

@@ -715,21 +715,25 @@ pub fn chdir(path string) {
pub fn getwd() string {
$if windows {
max := 512 // max_path_len * sizeof(wchar_t)
buf := &u16(vcalloc(max * 2))
if C._wgetcwd(buf, max) == 0 {
free(buf)
return ''
unsafe {
buf := &u16(vcalloc(max * 2))
if C._wgetcwd(buf, max) == 0 {
free(buf)
return ''
}
return string_from_wide(buf)
}
return string_from_wide(buf)
} $else {
buf := vcalloc(512)
if C.getcwd(charptr(buf), 512) == 0 {
unsafe {
if C.getcwd(charptr(buf), 512) == 0 {
free(buf)
return ''
}
res := buf.vstring().clone()
free(buf)
return ''
return res
}
res := unsafe { buf.vstring() }.clone()
free(buf)
return res
}
}

View File

@@ -54,16 +54,18 @@ fn C.symlink(charptr, charptr) int
pub fn uname() Uname {
mut u := Uname{}
utsize := sizeof(C.utsname)
x := malloc(int(utsize))
d := &C.utsname(x)
if C.uname(d) == 0 {
u.sysname = cstring_to_vstring(byteptr(d.sysname))
u.nodename = cstring_to_vstring(byteptr(d.nodename))
u.release = cstring_to_vstring(byteptr(d.release))
u.version = cstring_to_vstring(byteptr(d.version))
u.machine = cstring_to_vstring(byteptr(d.machine))
unsafe {
x := malloc(int(utsize))
d := &C.utsname(x)
if C.uname(d) == 0 {
u.sysname = cstring_to_vstring(byteptr(d.sysname))
u.nodename = cstring_to_vstring(byteptr(d.nodename))
u.release = cstring_to_vstring(byteptr(d.release))
u.version = cstring_to_vstring(byteptr(d.version))
u.machine = cstring_to_vstring(byteptr(d.machine))
}
free(d)
}
free(d)
return u
}
@@ -166,9 +168,11 @@ pub fn exec(cmd string) ?Result {
}
buf := [4096]byte{}
mut res := strings.new_builder(1024)
for C.fgets(charptr(buf), 4096, f) != 0 {
bufbp := byteptr(buf)
res.write_bytes(bufbp, vstrlen(bufbp))
unsafe {
for C.fgets(charptr(buf), 4096, f) != 0 {
bufbp := byteptr(buf)
res.write_bytes(bufbp, vstrlen(bufbp))
}
}
soutput := res.str()
// res.free()