vlib: remove malloc unsafe warning

pull/3810/head
yuyi 2020-02-22 19:41:24 +08:00 committed by GitHub
parent 3c3ca1e61f
commit 1a1aa267b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 54 deletions

View File

@ -33,28 +33,30 @@ const (
// Given a root key look for one of the subkeys in 'versions' and get the path // Given a root key look for one of the subkeys in 'versions' and get the path
fn find_windows_kit_internal(key RegKey, versions []string) ?string { fn find_windows_kit_internal(key RegKey, versions []string) ?string {
$if windows { $if windows {
for version in versions { unsafe {
required_bytes := 0 // TODO mut for version in versions {
result := C.RegQueryValueEx(key, version.to_wide(), 0, 0, 0, &required_bytes) required_bytes := 0 // TODO mut
length := required_bytes / 2 result := C.RegQueryValueEx(key, version.to_wide(), 0, 0, 0, &required_bytes)
if result != 0 { length := required_bytes / 2
continue if result != 0 {
continue
}
alloc_length := (required_bytes + 2)
mut value := &u16(malloc(alloc_length))
if isnil(value) {
continue
}
result2 := C.RegQueryValueEx(key, version.to_wide(), 0, 0, value, &alloc_length)
if result2 != 0 {
continue
}
// We might need to manually null terminate this thing
// So just make sure that we do that
if (value[length - 1] != u16(0)) {
value[length] = u16(0)
}
return string_from_wide(value)
} }
alloc_length := (required_bytes + 2)
mut value := &u16(malloc(alloc_length))
if isnil(value) {
continue
}
result2 := C.RegQueryValueEx(key, version.to_wide(), 0, 0, value, &alloc_length)
if result2 != 0 {
continue
}
// We might need to manually null terminate this thing
// So just make sure that we do that
if (value[length - 1] != u16(0)) {
value[length] = u16(0)
}
return string_from_wide(value)
} }
} }
return error('windows kit not found') return error('windows kit not found')

View File

@ -681,28 +681,30 @@ pub fn get_line() string {
// get_raw_line returns a one-line string from stdin along with '\n' if there is any // get_raw_line returns a one-line string from stdin along with '\n' if there is any
pub fn get_raw_line() string { pub fn get_raw_line() string {
$if windows { $if windows {
max_line_chars := 256 unsafe {
buf := malloc(max_line_chars * 2) max_line_chars := 256
h_input := C.GetStdHandle(STD_INPUT_HANDLE) buf := malloc(max_line_chars * 2)
mut bytes_read := 0 h_input := C.GetStdHandle(STD_INPUT_HANDLE)
if is_atty(0) > 0 { mut bytes_read := 0
C.ReadConsole(h_input, buf, max_line_chars * 2, &bytes_read, 0) if is_atty(0) > 0 {
return string_from_wide2(&u16(buf), bytes_read) C.ReadConsole(h_input, buf, max_line_chars * 2, &bytes_read, 0)
} return string_from_wide2(&u16(buf), bytes_read)
mut offset := 0
for {
pos := buf + offset
res := C.ReadFile(h_input, pos, 1, &bytes_read, 0)
if !res || bytes_read == 0 {
break
} }
if *pos == `\n` || *pos == `\r` { mut offset := 0
for {
pos := buf + offset
res := C.ReadFile(h_input, pos, 1, &bytes_read, 0)
if !res || bytes_read == 0 {
break
}
if *pos == `\n` || *pos == `\r` {
offset++
break
}
offset++ offset++
break
} }
offset++ return string(buf, offset)
} }
return string(buf, offset)
} $else { } $else {
max := size_t(0) max := size_t(0)
mut buf := byteptr(0) mut buf := byteptr(0)

View File

@ -207,21 +207,23 @@ pub fn get_file_handle(path string) HANDLE {
// get_module_filename retrieves the fully qualified path for the file that contains the specified module. // get_module_filename retrieves the fully qualified path for the file that contains the specified module.
// The module must have been loaded by the current process. // The module must have been loaded by the current process.
pub fn get_module_filename(handle HANDLE) ?string { pub fn get_module_filename(handle HANDLE) ?string {
mut sz := 4096 // Optimized length unsafe {
mut buf := &u16(malloc(4096)) mut sz := 4096 // Optimized length
for { mut buf := &u16(malloc(4096))
status := int(C.GetModuleFileNameW(handle, voidptr(&buf), sz)) for {
match status { status := int(C.GetModuleFileNameW(handle, voidptr(&buf), sz))
SUCCESS { match status {
_filename := string_from_wide2(buf, sz) SUCCESS {
return _filename _filename := string_from_wide2(buf, sz)
} return _filename
else { }
// Must handled with GetLastError and converted by FormatMessage else {
return error('Cannot get file name from handle') // Must handled with GetLastError and converted by FormatMessage
} return error('Cannot get file name from handle')
} }
} }
}
}
panic('this should be unreachable') // TODO remove unreachable after loop panic('this should be unreachable') // TODO remove unreachable after loop
} }