Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongodbStore.ListDirectoryEntries panics on Find method failure #2488

Merged
merged 1 commit into from
Dec 4, 2021

Conversation

Bl1tz23
Copy link
Contributor

@Bl1tz23 Bl1tz23 commented Dec 3, 2021

Hello!
I made a fix for nil pointer deference panic in *MongodbStore.ListDirectoryEntries method.
When *mongo.Collection.Find returns an error, *mongo.Cursor, which this method also returns, might be nil.

log:

Dec 02 12:59:11 mon001 weed[9050]: panic: runtime error: invalid memory address or nil pointer dereference
Dec 02 12:59:11 mon001 weed[9050]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x13f6922]  
Dec 02 12:59:11 mon001 weed[9050]: goroutine 2054744 [running]:  
Dec 02 12:59:11 mon001 weed[9050]: go.mongodb.org/mongo-driver/mongo.(*Cursor).next(0xc0099cfa40, {0x28327c0, 0xc009af8f30}, 0xc0)
Dec 02 12:59:11 mon001 weed[9050]:         /go/pkg/mod/go.mongodb.org/mongo-driver@v1.7.0/mongo/cursor.go:102 +0x22
Dec 02 12:59:11 mon001 weed[9050]: go.mongodb.org/mongo-driver/mongo.(*Cursor).Next(...)
Dec 02 12:59:11 mon001 weed[9050]:         /go/pkg/mod/go.mongodb.org/mongo-driver@v1.7.0/mongo/cursor.go:81
Dec 02 12:59:11 mon001 weed[9050]: github.com/chrislusf/seaweedfs/weed/filer/mongodb.(*MongodbStore).ListDirectoryEntries(0xc0007f5b90, {0x28327c0, 0xc009af8f30}, {0xc010e20870, 0x0}, {0xc009d497d0, 0x17}, 0x0, 0x400, 0xc009af9230)

@@ -193,6 +193,10 @@ func (store *MongodbStore) ListDirectoryEntries(ctx context.Context, dirPath uti
optLimit := int64(limit)
opts := &options.FindOptions{Limit: &optLimit, Sort: bson.M{"name": 1}}
cur, err := store.connect.Database(store.database).Collection(store.collectionName).Find(ctx, where, opts)
if err != nil {
return lastFileName, fmt.Errorf("failed to list directory entries: find error: %w", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use %v ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the attention to my pull request.
I guess it's better to use %w directive since go 1.13. Directive %w are similar to %v, but error returned with %w will have an Unwrap method. And also allows to use errors.Is and errors.As functions.
https://go.dev/blog/go1.13-errors

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to learn from this! Thanks!

@chrislusf chrislusf merged commit 9b0da7c into seaweedfs:master Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants