summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAmjad Ben Hedhili <amjadhedhili@outlook.com>2023-09-23 16:08:24 +0100
committerGitHub <noreply@github.com>2023-09-23 17:08:24 +0200
commita6c281bd1d6e47fb8d137008e6ba944b8b2eb9a3 (patch)
treefc6df96b398eae4f1f883c93c8afe937da66c28f
parenteadd0d72cffd0d8470f42b873abda251e2e5eded (diff)
downloadNim-a6c281bd1d6e47fb8d137008e6ba944b8b2eb9a3.tar.gz
Fix `newStringUninit` not setting the '\0' terminator [backport] (#22746)
Causes problems when working with `cstring`s.
-rw-r--r--lib/system.nim8
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 97d9d72ad..1ded4090b 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1639,10 +1639,14 @@ when not defined(js):
     ## the same effect can be achieved with the `&` operator or with `add`.
     result = newStringOfCap(len)
     when defined(nimSeqsV2):
-      cast[ptr int](addr result)[] = len
+      let s = cast[ptr NimStringV2](addr result)
+      if len > 0:
+        s.len = len
+        s.p.data[len] = '\0'
     else:
-      var s = cast[PGenericSeq](result)
+      let s = cast[NimString](result)
       s.len = len
+      s.data[len] = '\0'
 else:
   proc newStringUninit*(len: Natural): string {.
     magic: "NewString", importc: "mnewString", noSideEffect.}