From 8fe4eaceb0519910f2f9c757dcf809702707f11c Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:24:33 +0000 Subject: [PATCH] follow up #22380; fixes incorrect usage of `newWideCString` --- lib/pure/os.nim | 4 ++-- lib/std/private/ospaths2.nim | 4 ++-- lib/std/syncio.nim | 2 +- lib/std/widestrs.nim | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 2b52d4d99f3fe..3afb9cdd7dc6a 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -424,13 +424,13 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", when defined(windows): var bufsize = MAX_PATH.int32 var unused: WideCString = nil - var res = newWideCString("", bufsize) + var res = newWideCString(bufsize) while true: var L = getFullPathNameW(newWideCString(filename), bufsize, res, unused) if L == 0'i32: raiseOSError(osLastError(), filename) elif L > bufsize: - res = newWideCString("", L) + res = newWideCString(L) bufsize = L else: result = res$L diff --git a/lib/std/private/ospaths2.nim b/lib/std/private/ospaths2.nim index 37fae3ccd0188..5dd09d7e696d1 100644 --- a/lib/std/private/ospaths2.nim +++ b/lib/std/private/ospaths2.nim @@ -862,13 +862,13 @@ when not defined(nimscript): raiseAssert "use -d:nodejs to have `getCurrentDir` defined" elif defined(windows): var bufsize = MAX_PATH.int32 - var res = newWideCString("", bufsize) + var res = newWideCString(bufsize) while true: var L = getCurrentDirectoryW(bufsize, res) if L == 0'i32: raiseOSError(osLastError()) elif L > bufsize: - res = newWideCString("", L) + res = newWideCString(L) bufsize = L else: result = res$L diff --git a/lib/std/syncio.nim b/lib/std/syncio.nim index b664c3b609ca4..38c151bb7ba5b 100644 --- a/lib/std/syncio.nim +++ b/lib/std/syncio.nim @@ -419,7 +419,7 @@ proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect], if f.isatty: const numberOfCharsToRead = 2048 var numberOfCharsRead = 0'i32 - var buffer = newWideCString("", numberOfCharsToRead) + var buffer = newWideCString(numberOfCharsToRead) if readConsole(getOsFileHandle(f), addr(buffer[0]), numberOfCharsToRead, addr(numberOfCharsRead), nil) == 0: var error = getLastError() diff --git a/lib/std/widestrs.nim b/lib/std/widestrs.nim index 0bf50be455c16..3ea4f520f26f6 100644 --- a/lib/std/widestrs.nim +++ b/lib/std/widestrs.nim @@ -154,6 +154,7 @@ when not (defined(cpu16) or defined(cpu8)): createWide(result, size * 2 + 2) proc newWideCString*(source: cstring, L: int): WideCStringObj = + ## Warning:: `source` needs to be preallocated with the length `L` createWide(result, L * 2 + 2) var d = 0 for ch in runes(source, L):