From c439ef2e71f6c98aa0a9de1d2036a5422454171c Mon Sep 17 00:00:00 2001 From: Andrei Formiga Date: Fri, 27 May 2016 19:03:15 -0300 Subject: [PATCH 1/2] 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. --- compiler/pragmas.nim | 1 + tests/pragmas/thintoff.nim | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 tests/pragmas/thintoff.nim diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 38d17eb62a..dc09d8fc47 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 0000000000..7a5b344e66 --- /dev/null +++ b/tests/pragmas/thintoff.nim @@ -0,0 +1,6 @@ +# issue #4001 +discard """ + errormsg: "invalid pragma: hint[]: off" +""" + +{. hint[]: off .} From 929865ff628322da213f2296b6dcef3f4107129f Mon Sep 17 00:00:00 2001 From: Andrei Formiga Date: Fri, 27 May 2016 19:38:54 -0300 Subject: [PATCH 2/2] Deleted test for fix #4001 --- tests/pragmas/thintoff.nim | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 tests/pragmas/thintoff.nim diff --git a/tests/pragmas/thintoff.nim b/tests/pragmas/thintoff.nim deleted file mode 100644 index 7a5b344e66..0000000000 --- a/tests/pragmas/thintoff.nim +++ /dev/null @@ -1,6 +0,0 @@ -# issue #4001 -discard """ - errormsg: "invalid pragma: hint[]: off" -""" - -{. hint[]: off .}