[GODRIVER-3024] Runtime Error 'index out of range' in MongoDB Go Driver ExtJSON Parsing Created: 30/Oct/23  Updated: 08/Jan/24

Status: Backlog
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Preston Vasquez Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to GODRIVER-3025 Create a peeking methodology for bson... Backlog
is related to GODRIVER-2835 Support reading a stream of BSON docu... Backlog
Quarter: FY25Q1
Documentation Changes Summary:

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?


 Description   

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



 Comments   
Comment by Preston Vasquez [ 30/Oct/23 ]

Recommend using a peek method as described in GODRIVER-3025 to resolve this.

Generated at Thu Feb 08 08:39:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.