Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-3024

Runtime Error 'index out of range' in MongoDB Go Driver ExtJSON Parsing

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Detailed steps to reproduce the problem?

      Include full traceback, if possible

      Reproducible with the following script:

      package main
      
      import (
      	"bytes"
      	"fmt"
      	"io"
      
      	"go.mongodb.org/mongo-driver/bson"
      	"go.mongodb.org/mongo-driver/bson/bsonrw"
      )
      
      func main() {
      	ej := []byte(`[{"x":1},{"y":2}]`)
      
      	rw, err := bsonrw.NewExtJSONValueReader(bytes.NewReader(ej), true)
      	if err != nil {
      		panic(err)
      	}
      
      	dec, err := bson.NewDecoder(rw)
      	if err != nil {
      		panic(err)
      	}
      
      	for {
      		var data interface{}
      		err := dec.Decode(&data)
      		if err == io.EOF {
      			break
      		}
      
      		if err != nil {
      			panic(err)
      		}
      
      		fmt.Println("data: ", data)
      	}
      }
      

      Output:

      ❯ go run main.go
      data:  [[{x 1}] [{y 2}]]
      panic: runtime error: index out of range [-2]
      
      goroutine 1 [running]:
      go.mongodb.org/mongo-driver/bson/bsonrw.(*extJSONValueReader).Type(0x14000143b98?)
              /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsonrw/extjson_reader.go:237 +0x48
      go.mongodb.org/mongo-driver/bson/bsoncodec.EmptyInterfaceCodec.decodeType({0xa0?}, {0x1400013cc40, 0x0, {0x0, 0x0}, {0x0, 0x0}, 0x0, 0x0, 0x0, ...}, ...)
              /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/empty_interface_codec.go:116 +0x1cc
      go.mongodb.org/mongo-driver/bson/bsoncodec.EmptyInterfaceCodec.DecodeValue({0x74?}, {0x1400013cc40, 0x0, {0x0, 0x0}, {0x0, 0x0}, 0x0, 0x0, 0x0, ...}, ...)
              /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/empty_interface_codec.go:152 +0x178
      go.mongodb.org/mongo-driver/bson.(*Decoder).Decode(0x14000143f18, {0x1043d6260?, 0x1400010bad0})
              /Users/preston.vasquez/Developer/mongo-go-driver/bson/decoder.go:134 +0x3b4
      main.main()
              /Users/preston.vasquez/Developer/technical/workshop/mongo/bson/more/main.go:27 +0x1b4
      exit status 2
      

      Definition of done: what must be done to consider the task complete?

      An io.Reader should return io.EOF when the stack is exhuasted.

      The exact Go version used, with patch level:

      go version go1.20.5 darwin/arm64

      The exact version of the Go driver used:

      go.mongodb.org/mongo-driver v1.11.4

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      local distro, 7.0.2 server version, replica set: 

      ${DRIVERS_TOOLS}/.evergreen/start-orchestration.sh /Users/preston.vasquez/Developer/mongo-orchestration-home
      curl -X POST http://localhost:8889/v1/replica_sets \	-H 'Content-Type: application/json' \	-d @${DRIVERS_TOOLS}/.evergreen/orchestration/configs/replica_sets/basic.json
       

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      OSX ventura 13.6

      Security Vulnerabilities

      NA

            Assignee:
            Unassigned Unassigned
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: