From a78ac38cee99d2d776ae0796eeefc3b176f9e231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Mjaavatten?= Date: Mon, 25 Jul 2022 15:50:02 +0200 Subject: [PATCH] Fix missing error check on `wcstombs` --- lib/std/private/win_setenv.nim | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/std/private/win_setenv.nim b/lib/std/private/win_setenv.nim index 4b093e6f7b..10f731c07a 100644 --- a/lib/std/private/win_setenv.nim +++ b/lib/std/private/win_setenv.nim @@ -79,11 +79,11 @@ else: ]# if genviron != nil: - # Wrapping in try-except block, because first `wcstombs` fails with a - # "RangeDefect" if the current codepage cannot represent a character in - # `wideName`. In this case skip updating MBCS environment. - try: - let requiredSize = wcstombs(nil, wideName, 0).int + # wcstombs returns (size_t) (-1) if any characters cannot be represented + # in the current codepage. Skip updating MBCS environment in this case. + let requiredSizeS = wcstombs(nil, wideName, 0) + if requiredSizeS != high(csize_t): + let requiredSize = requiredSizeS.int var buf = newSeq[char](requiredSize + 1) let buf2 = buf[0].addr if wcstombs(buf2, wideName, csize_t(requiredSize + 1)) == csize_t(high(uint)): @@ -93,8 +93,6 @@ else: ptrToEnv[0] = '\0' ptrToEnv = c_getenv(buf2) ptrToEnv[1] = '=' - except RangeDefect: - discard # And now, we have to update the outer environment to have a proper empty value. if setEnvironmentVariableW(wideName, value.newWideCString) == 0: