Skip to content

Commit 990145a

Browse files
authored
os: fix real_path() and executable() for GetFinalPathNameByHandleW() return zero value (fix #25482) (#25485)
1 parent 0df1ae8 commit 990145a

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

‎vlib/os/os.c.v‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ pub fn executable() string {
705705
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
706706
final_len := C.GetFinalPathNameByHandleW(file, unsafe { &u16(&final_path[0]) },
707707
max_path_buffer_size, 0)
708-
if final_len < u32(max_path_buffer_size) {
708+
if final_len < u32(max_path_buffer_size) && final_len != 0 {
709709
sret := unsafe { string_from_wide2(&u16(&final_path[0]), int(final_len)) }
710710
defer {
711711
unsafe { sret.free() }
@@ -714,7 +714,7 @@ pub fn executable() string {
714714
sret_slice := sret[4..]
715715
res := sret_slice.clone()
716716
return res
717-
} else {
717+
} else if final_len != 0 {
718718
eprintln('os.executable() saw that the executable file path was too long')
719719
}
720720
}
@@ -862,13 +862,15 @@ pub fn real_path(fpath string) string {
862862
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
863863
final_len := C.GetFinalPathNameByHandleW(file, pu16_fullpath, max_path_buffer_size,
864864
0)
865-
if final_len < u32(max_path_buffer_size) {
865+
if final_len < u32(max_path_buffer_size) && final_len != 0 {
866866
rt := unsafe { string_from_wide2(pu16_fullpath, int(final_len)) }
867867
srt := rt[4..]
868868
unsafe { res.free() }
869869
res = srt.clone()
870870
} else {
871-
eprintln('os.real_path() saw that the file path was too long')
871+
if final_len != 0 {
872+
eprintln('os.real_path() saw that the file path was too long')
873+
}
872874
unsafe { res.free() }
873875
return fpath.clone()
874876
}

0 commit comments

Comments
 (0)