Permalink
Browse files

Fix panic with using built-in (scalar) in array context

  • Loading branch information...
benhoyt committed Nov 6, 2018
1 parent 8eed4ce commit c59731f5543bf9b48cf92a981b66696a5ab0ceae
Showing with 9 additions and 0 deletions.
  1. +4 −0 goawk.go
  2. +2 −0 interp/interp_test.go
  3. +3 −0 parser/resolve.go
@@ -30,6 +30,10 @@ package main
/*
TODO:
- why (before fix) does program '0 in FS' give error:
unexpected fault address 0xffffffffffffffe8
fatal error: fault
instead of "panic: runtime error: index out of range"?
- performance testing: I/O, allocations, CPU
+ PoC: is interpreting via a "heavy AST" faster?
i.e., with execute functions on the AST elements instead of the switch/case
@@ -258,6 +258,8 @@ BEGIN {
"x", "", "NF set to negative value: -1", "negative value"},
{`{ NF=1234567; }`, "x", "", "NF set too large: 1234567", ""},
{`BEGIN { $1234567=1 }`, "", "", "field index too large: 1234567", ""},
{`0 in FS # !awk - doesn't flag this as an error`, "x", "",
`parse error at 1:6: can't use scalar "FS" as array`, "array"},

// Lots of NF tests with different combinations of NF, $, and number
// of input fields. Some of these cause segmentation faults on awk
@@ -174,6 +174,9 @@ func (p *parser) varRef(name string, pos Position) *VarExpr {
// ArrayExpr.Index won't be set till later)
func (p *parser) arrayRef(name string, pos Position) *ArrayExpr {
scope, funcName := p.getScope(name)
if scope == ScopeSpecial {
panic(p.error("can't use scalar %q as array", name))
}
expr := &ArrayExpr{scope, 0, name}
p.arrayRefs = append(p.arrayRefs, arrayRef{funcName, expr, pos})
typ := p.varTypes[funcName][name].typ

0 comments on commit c59731f

Please sign in to comment.