Skip to content

cmd/yacc: Index out of range #9099

@cznic

Description

@cznic
What does 'go version' print?
go version devel +6e0eb636f60c Wed Nov 12 14:27:27 2014 -0800 linux/amd64


What steps reproduce the problem?
If possible, include a link to a program on play.golang.org.

1.
$ cat bug.y
%{
package main

import (
    "flag"
    "fmt"
)
%}

%union{}

%token T

%%

Start:
|   T Start

%%

type lx struct{
    i, max int
}

func (lx *lx) Lex(lval *yySymType) int {
    if lx.i += 1; lx.i < lx.max {
        return T
    }

    return 0
}

func (lx *lx) Error(s string) {
    fmt.Println(s)
}

func main() {
    delta := flag.Int("delta", 0, "produce yyMaxDepth+delta T tokens")
    flag.Parse()
    fmt.Println(yyParse(&lx{max: yyMaxDepth+*delta}))
}
$

What happened?
$ go tool yacc bug.y
$ go run y.go -delta -1
0
$ go run y.go -delta 1
0
$ go run y.go -delta 0
panic: runtime error: index out of range

goroutine 1 [running]:
main.yyParse(0x7f52b13a37d0, 0xc208032220, 0x56bbd0)
    /home/jnml/src/tmp/go-yacc/yaccpar:207 +0xcf1
main.main()
    /home/jnml/src/tmp/go-yacc/bug.y:39 +0xd6

goroutine 2 [runnable]:
runtime.forcegchelper()
    /home/jnml/go/src/runtime/proc.go:90
runtime.goexit()
    /home/jnml/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
    /home/jnml/go/src/runtime/mgc0.go:66
runtime.goexit()
    /home/jnml/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
    /home/jnml/go/src/runtime/malloc.go:705
runtime.goexit()
    /home/jnml/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2
$

What should have happened instead?
$ go tool yacc bug.y
$ go tool yacc bug.y
$ go run y.go -delta -1
0
$ go run y.go -delta 1
0
$ go run y.go -delta 0
0
$

Please provide any additional information below.
Patch ready, CL mail follows.

Attachments:

  1. bug.y (424 bytes)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions