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,6 +33,7 @@ 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 {
unsafe {
for version in versions { for version in versions {
required_bytes := 0 // TODO mut required_bytes := 0 // TODO mut
result := C.RegQueryValueEx(key, version.to_wide(), 0, 0, 0, &required_bytes) result := C.RegQueryValueEx(key, version.to_wide(), 0, 0, 0, &required_bytes)
@ -57,6 +58,7 @@ fn find_windows_kit_internal(key RegKey, versions []string) ?string {
return string_from_wide(value) return string_from_wide(value)
} }
} }
}
return error('windows kit not found') return error('windows kit not found')
} }

View File

@ -681,6 +681,7 @@ 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 {
unsafe {
max_line_chars := 256 max_line_chars := 256
buf := malloc(max_line_chars * 2) buf := malloc(max_line_chars * 2)
h_input := C.GetStdHandle(STD_INPUT_HANDLE) h_input := C.GetStdHandle(STD_INPUT_HANDLE)
@ -703,6 +704,7 @@ pub fn get_raw_line() string {
offset++ 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,6 +207,7 @@ 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 {
unsafe {
mut sz := 4096 // Optimized length mut sz := 4096 // Optimized length
mut buf := &u16(malloc(4096)) mut buf := &u16(malloc(4096))
for { for {
@ -222,6 +223,7 @@ pub fn get_module_filename(handle HANDLE) ?string {
} }
} }
} }
}
panic('this should be unreachable') // TODO remove unreachable after loop panic('this should be unreachable') // TODO remove unreachable after loop
} }