summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2021-07-21 16:55:50 +0200
committerGitHub <noreply@github.com>2021-07-21 16:55:50 +0200
commit01fc9e58ca8b694eac6c07b05c26c9d98cbd141c (patch)
tree8c666d469fe32fb47130cd4a11c5e7efda8545c3 /compiler
parent58080525a1dcad23de6ad0cf9ca6988f103de350 (diff)
downloadNim-01fc9e58ca8b694eac6c07b05c26c9d98cbd141c.tar.gz
fixes #18550 (#18553)
* fixes #18550

* update the manual to reflect reality
Diffstat (limited to 'compiler')
-rw-r--r--compiler/lineinfos.nim2
-rw-r--r--compiler/sempass2.nim16
-rw-r--r--compiler/sighashes.nim4
-rw-r--r--compiler/types.nim15
4 files changed, 36 insertions, 1 deletions
diff --git a/compiler/lineinfos.nim b/compiler/lineinfos.nim
index a3243d5fc..3f31ceb97 100644
--- a/compiler/lineinfos.nim
+++ b/compiler/lineinfos.nim
@@ -71,6 +71,7 @@ type
     warnCannotOpen = "CannotOpen",
     warnFileChanged = "FileChanged",
     warnSuspiciousEnumConv = "EnumConv",
+    warnCstringConv = "CStringConv",
     warnUser = "User",
     # hints
     hintSuccess = "Success", hintSuccessX = "SuccessX",
@@ -155,6 +156,7 @@ const
     warnCannotOpen: "cannot open: $1",
     warnFileChanged: "file changed: $1",
     warnSuspiciousEnumConv: "$1",
+    warnCstringConv: "$1",
     warnUser: "$1",
     hintSuccess: "operation successful: $#",
     # keep in sync with `testament.isSuccess`
diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim
index 1e1672cad..ce21c3ed2 100644
--- a/compiler/sempass2.nim
+++ b/compiler/sempass2.nim
@@ -951,6 +951,15 @@ proc trackInnerProc(tracked: PEffects, n: PNode) =
   else:
     for ch in n: trackInnerProc(tracked, ch)
 
+proc allowCStringConv(n: PNode): bool =
+  case n.kind
+  of nkStrLit..nkTripleStrLit: result = true
+  of nkSym: result = n.sym.kind in {skConst, skParam}
+  of nkAddr: result = isCharArrayPtr(n.typ, true)
+  of nkCallKinds:
+    result = isCharArrayPtr(n.typ, n[0].kind == nkSym and n[0].sym.magic == mAddr)
+  else: result = isCharArrayPtr(n.typ, false)
+
 proc track(tracked: PEffects, n: PNode) =
   case n.kind
   of nkSym:
@@ -1157,6 +1166,13 @@ proc track(tracked: PEffects, n: PNode) =
       if tracked.owner.kind != skMacro:
         createTypeBoundOps(tracked, n.typ, n.info)
   of nkHiddenStdConv, nkHiddenSubConv, nkConv:
+    if n.kind in {nkHiddenStdConv, nkHiddenSubConv} and
+        n.typ.skipTypes(abstractInst).kind == tyCstring and
+        not allowCStringConv(n[1]):
+      message(tracked.config, n.info, warnCstringConv,
+        "implicit conversion to 'cstring' from a non-const location: $1; this will become a compile time error in the future" %
+          [$n[1]])
+
     if n.len == 2:
       track(tracked, n[1])
       if tracked.owner.kind != skMacro:
diff --git a/compiler/sighashes.nim b/compiler/sighashes.nim
index f6df422e6..0ff97017f 100644
--- a/compiler/sighashes.nim
+++ b/compiler/sighashes.nim
@@ -14,7 +14,9 @@ from hashes import Hash
 import types
 
 proc `&=`(c: var MD5Context, s: string) = md5Update(c, s, s.len)
-proc `&=`(c: var MD5Context, ch: char) = md5Update(c, unsafeAddr ch, 1)
+proc `&=`(c: var MD5Context, ch: char) =
+  # XXX suspicious code here; relies on ch being zero terminated?
+  md5Update(c, unsafeAddr ch, 1)
 proc `&=`(c: var MD5Context, r: Rope) =
   for l in leaves(r): md5Update(c, l, l.len)
 proc `&=`(c: var MD5Context, i: BiggestInt) =
diff --git a/compiler/types.nim b/compiler/types.nim
index 798f872c2..f0bd99a17 100644
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -1664,3 +1664,18 @@ proc lookupFieldAgain*(ty: PType; field: PSym): PSym =
     if result != nil: break
     ty = ty[0]
   if result == nil: result = field
+
+proc isCharArrayPtr*(t: PType; allowPointerToChar: bool): bool =
+  let t = t.skipTypes(abstractInst)
+  if t.kind == tyPtr:
+    let pointsTo = t[0].skipTypes(abstractInst)
+    case pointsTo.kind
+    of tyUncheckedArray:
+      result = pointsTo[0].kind == tyChar
+    of tyArray:
+      result = pointsTo[1].kind == tyChar and firstOrd(nil, pointsTo[0]) == 0 and
+        skipTypes(pointsTo[0], {tyRange}).kind in {tyInt..tyInt64}
+    of tyChar:
+      result = allowPointerToChar
+    else:
+      discard