Skip to content

CDRIVER-5506 error if mongoc_gridfs_file_readv reads an incomplete chunk #1565

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

Merged
merged 4 commits into from
Mar 29, 2024

Conversation

kevinAlbs
Copy link
Collaborator

@kevinAlbs kevinAlbs commented Mar 27, 2024

Summary

Address a hang observed in mongoc_gridfs_file_readv when reading a corrupt incomplete chunk. If an incomplete chunk is detected, an error is now returned.

Background & Motivation

Though the mongoc_gridfs_t API notably does not conform to the GridFS spec, the GridFS spec requires returning an error for an unexpected length:

As drivers stream the stored file they MUST check that each chunk received is the next expected chunk (i.e. it has the expected "n" value) and that the data field is of the expected length

The cause of the hang is an assumption that chunks (excluding the last) contain data of length chunk size. _mongoc_gridfs_file_refresh_page calculates the chunk to read from the position of the last read byte:

file->n = (int32_t) (file->pos / file->chunk_size);

If an incomplete chunk is read, file->pos may be smaller than assumed, resulting in repeated setting of file->n to an already-read chunk.

Fixes a possible hang when `mongoc_gridfs_file_readv` is called with a corrupt interior chunk sized smaller than `chunk_size`.

Implementation assumes chunks (excluding the last chunk) are of size `chunk_size` for calcluating offsets.
@kevinAlbs kevinAlbs marked this pull request as ready for review March 27, 2024 18:03
@kevinAlbs kevinAlbs requested a review from kkloberdanz March 27, 2024 18:03
Copy link
Contributor

@kkloberdanz kkloberdanz left a comment

Choose a reason for hiding this comment

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

Looks good!

@kevinAlbs kevinAlbs merged commit ddcce35 into mongodb:master Mar 29, 2024
kevinAlbs added a commit that referenced this pull request Mar 29, 2024
…chunk (#1565)

* include more info in error message

* add regression test for reading incomplete chunk

* CDRIVER-5506 error if reading an incomplete chunk

Fixes a possible hang when `mongoc_gridfs_file_readv` is called with a corrupt interior chunk sized smaller than `chunk_size`.

Implementation assumes chunks (excluding the last chunk) are of size `chunk_size` for calcluating offsets.
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.

2 participants