summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorOscar NihlgÄrd <oscarnihlgard@gmail.com>2019-10-24 11:17:01 +0200
committerAndreas Rumpf <rumpf_a@web.de>2019-10-24 11:17:01 +0200
commit4ac100c9122231ce82f56b51f86ee6e906e0b389 (patch)
treeb73fb4483d849dd112bf79e54696251abb5f3042
parent9ccfcf5dd2572c858913cf092a6a571a5e4a8df0 (diff)
downloadNim-4ac100c9122231ce82f56b51f86ee6e906e0b389.tar.gz
Fix jsgen bug with uninitialized seq (#12500) [backport]
-rw-r--r--compiler/jsgen.nim2
-rw-r--r--tests/js/t12223.nim20
2 files changed, 21 insertions, 1 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index a19f35722..4f2fef6b8 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -1169,7 +1169,7 @@ proc genArrayAddr(p: PProc, n: PNode, r: var TCompRes) =
     first = firstOrd(p.config, typ.sons[0])
   if optBoundsCheck in p.options:
     useMagic(p, "chckIndx")
-    r.res = "chckIndx($1, $2, $3.length+$2-1)-$2" % [b.res, rope(first), tmp]
+    r.res = "chckIndx($1, $2, ($3 != null ? $3.length : 0)+$2-1)-$2" % [b.res, rope(first), tmp]
   elif first != 0:
     r.res = "($1)-$2" % [b.res, rope(first)]
   else:
diff --git a/tests/js/t12223.nim b/tests/js/t12223.nim
new file mode 100644
index 000000000..c0e75fb44
--- /dev/null
+++ b/tests/js/t12223.nim
@@ -0,0 +1,20 @@
+discard """
+  action: "run"
+  output: '''
+caught
+index out of bounds, the container is empty
+'''
+"""
+
+proc fun() =
+  var z: seq[string]
+  discard z[4]
+
+proc main()=
+  try:
+    fun()
+  except Exception as e:
+    echo "caught"
+    echo getCurrentExceptionMsg()
+
+main()
\ No newline at end of file