skx / gobasic

A BASIC interpreter written in golang.
https://blog.steve.fi/tags/basic/
GNU General Public License v2.0
328 stars 27 forks source link

We're accidentally returning `nil` somewhere. #78

Closed skx closed 5 years ago

skx commented 5 years ago

The following "program" causes a null-pointer dereference:

  DIM a(10,10)0FOR X=0TO 1""a[00000000000000 00EXT 00

   frodo ~/go/src/github.com/skx/gobasic $ ./gobasic t.bas 
   panic: runtime error: invalid memory address or nil pointer dereference
   [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x4ab1b7]

   goroutine 1 [running]:
   github.com/skx/gobasic/eval.(*Interpreter).expr(0xc000090000, 0x1, 0x4ed7a1, 0x5)
    /home/skx/go/src/github.com/skx/gobasic/eval/eval.go:558 +0x57
   github.com/skx/gobasic/eval.(*Interpreter).RunOnce(0xc000090000, 0x0, 0x0)
    /home/skx/go/src/github.com/skx/gobasic/eval/eval.go:2261 +0xe0
   github.com/skx/gobasic/eval.(*Interpreter).Run(0xc000090000, 0xc000090000, 0x0)
    /home/skx/go/src/github.com/skx/gobasic/eval/eval.go:2289 +0x54
   main.main()
    /home/skx/go/src/github.com/skx/gobasic/main.go:92 +0x3ab

This happens here:

   func (e *Interpreter) expr(allowBinOp bool) object.Object {

    // First argument.
    t1 := e.term()

    if t1 == nil {
        fmt.Printf("NIL OBJECT!!!!\n")
        return object.Error("Nil object!")
}

(t1 is nil)

I suspect that the findIndex code is tripping up on this section of the input:

 ... a[00000000000000 00EXT 00

Something for later in the day :)