os: Make windows allocations more consistent & LocalFree args_list

if max is 512 then allocate `max * sizeof(wchar_t)`
pull/1299/head
F1ssi0N 2019-07-24 17:25:18 +01:00 committed by Alexander Medvednikov
parent f8bc893972
commit ca70d1f8a3
1 changed files with 9 additions and 6 deletions

View File

@ -100,6 +100,8 @@ fn init_os_args(argc int, argv *byteptr) []string {
for i := 0; i < args_count; i++ {
args << string_from_wide(&u16(args_list[i]))
}
C.LocalFree(args_list)
} $else {
for i := 0; i < argc; i++ {
args << string(argv[i])
@ -520,7 +522,7 @@ pub fn get_line() string {
pub fn get_raw_line() string {
$if windows {
max := 512 // MAX_PATH * sizeof(wchar_t)
buf := &u16(malloc(max))
buf := &u16(malloc(max*2))
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
if h_input == INVALID_HANDLE_VALUE {
panic('get_raw_line() error getting input handle.')
@ -658,8 +660,9 @@ pub fn executable() string {
return string(result, count)
}
$if windows {
mut result := &u16(malloc(512)) // MAX_PATH * sizeof(wchar_t)
len := int(C.GetModuleFileName( 0, result, MAX_PATH ))
max := 512
mut result := &u16(malloc(max*2)) // MAX_PATH * sizeof(wchar_t)
len := int(C.GetModuleFileName( 0, result, max ))
return string_from_wide2(result, len)
}
$if mac {
@ -731,9 +734,9 @@ pub fn chdir(path string) {
pub fn getwd() string {
$if windows {
max := 1024 // MAX_PATH * sizeof(wchar_t)
buf := &u16(malloc(max))
if C._wgetcwd(buf, max/2) == 0 {
max := 512 // MAX_PATH * sizeof(wchar_t)
buf := &u16(malloc(max*2))
if C._wgetcwd(buf, max) == 0 {
return ''
}
return string_from_wide(buf)