summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndrei Formiga <archimedes_siracusa@hotmail.com>2016-05-27 19:03:15 -0300
committerAndrei Formiga <archimedes_siracusa@hotmail.com>2016-05-27 19:03:15 -0300
commitc439ef2e71f6c98aa0a9de1d2036a5422454171c (patch)
tree63334c840744169563ce39205bb14fca92d60e6c
parent51c62a211bca12cc2f470add7bf64b0f4536d6e6 (diff)
downloadNim-c439ef2e71f6c98aa0a9de1d2036a5422454171c.tar.gz
Fix issue #4001: invalid pragma {. hint[]: off .}
The bug was in processNote, module pragmas. The code assumed that
a nkBracketExpr AST node always had two children (without testing
this), and tried to access elements with index 0 and 1 in the sons
array of the nkBracketExpr node.

The code that triggered the bug was just {. hint[]: off .}
by itself in a module; in this case the nkBracketExpr has only
one children in the sons array, so the code in processNote caused
an out-of-bounds array access.

This commit also adds a test to guarantee that this pragma is
rejected, as is.
-rw-r--r--compiler/pragmas.nim1
-rw-r--r--tests/pragmas/thintoff.nim6
2 files changed, 7 insertions, 0 deletions
diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim
index 38d17eb62..dc09d8fc4 100644
--- a/compiler/pragmas.nim
+++ b/compiler/pragmas.nim
@@ -277,6 +277,7 @@ proc processDynLib(c: PContext, n: PNode, sym: PSym) =
 proc processNote(c: PContext, n: PNode) =
   if (n.kind == nkExprColonExpr) and (sonsLen(n) == 2) and
       (n.sons[0].kind == nkBracketExpr) and
+      (n.sons[0].sons.len == 2) and
       (n.sons[0].sons[1].kind == nkIdent) and
       (n.sons[0].sons[0].kind == nkIdent):
       #and (n.sons[1].kind == nkIdent):
diff --git a/tests/pragmas/thintoff.nim b/tests/pragmas/thintoff.nim
new file mode 100644
index 000000000..7a5b344e6
--- /dev/null
+++ b/tests/pragmas/thintoff.nim
@@ -0,0 +1,6 @@
+# issue #4001
+discard """
+  errormsg: "invalid pragma: hint[]: off"
+"""
+
+{. hint[]: off .}