Browse Source

Add extra NUL termination check for string length in win32 general string convertors

gingerBill 5 years ago
parent
commit
58466a6f3b
1 changed files with 8 additions and 3 deletions
  1. 8 3
      core/sys/win32/general.odin

+ 8 - 3
core/sys/win32/general.odin

@@ -791,8 +791,10 @@ utf8_to_utf16 :: proc(s: string, allocator := context.temp_allocator) -> []u16 {
 	}
 	}
 
 
 	text[n] = 0;
 	text[n] = 0;
-
-	return text[:len(text)-1];
+	for n >= 0 && text[n] == 0 {
+		n -= 1;
+	}
+	return text[:n];
 }
 }
 utf8_to_wstring :: proc(s: string, allocator := context.temp_allocator) -> Wstring {
 utf8_to_wstring :: proc(s: string, allocator := context.temp_allocator) -> Wstring {
 	if res := utf8_to_utf16(s, allocator); res != nil {
 	if res := utf8_to_utf16(s, allocator); res != nil {
@@ -821,7 +823,10 @@ utf16_to_utf8 :: proc(s: []u16, allocator := context.temp_allocator) -> string {
 
 
 	text[n] = 0;
 	text[n] = 0;
 
 
-	return string(text[:len(text)-1]);
+	for n >= 0 && text[n] == 0 {
+		n -= 1;
+	}
+	return string(text[:n]);
 }
 }