Skip to content

Commit 9980a0b

Browse files
craftitacmorrow
authored andcommitted
CXX-603 Fix for dropping parts of blocks appended to a gridfs file with appendChunk
and add unit test for gridfs GridFileBuilder to check it handles block boundries correctly Signed-off-by: Andrew Morrow <acm@mongodb.com>
1 parent ef3ed2f commit 9980a0b

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/mongo/client/gridfs.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,14 @@ namespace mongo {
321321
invariant( _pendingDataSize <= _chunkSize );
322322
if (_pendingDataSize == _chunkSize) {
323323
_appendPendingData();
324-
_appendChunk( data + size, length - size, false );
324+
const char* const end = data + length;
325+
data = _appendChunk( data + size, length - size, false );
326+
if (data != end) {
327+
invariant(data < end);
328+
size_t nsize = static_cast<size_t>(end - data);
329+
memcpy( _pendingData.get() + _pendingDataSize, data, nsize );
330+
_pendingDataSize += nsize;
331+
}
325332
}
326333
}
327334
else {

src/mongo/integration/standalone/gridfs_test.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,45 @@ namespace {
226226

227227
TEST_F(GridFSTest, GridFileBuilder) {
228228
GridFileBuilder gfb(_gfs.get());
229-
for (int i=0; i<DATA_LEN; i+=2)
230-
gfb.appendChunk(DATA + i, min(2, DATA_LEN - i));
229+
size_t totalSize = 0;
230+
for (int i=0; i<DATA_LEN; i+=2) {
231+
size_t chunkSize = min(2, DATA_LEN - i);
232+
gfb.appendChunk(DATA + i, chunkSize);
233+
totalSize += chunkSize;
234+
}
231235
gfb.buildFile(DATA_NAME);
232236
GridFile gf = _gfs->findFileByName(DATA_NAME);
233237
ASSERT_EQUALS(gf.getNumChunks(), 1);
238+
ASSERT_EQUALS(gf.getContentLength(), totalSize);
234239
}
235240

236241
TEST_F(GridFSTest, GridFileBuilderMultipleChunks) {
237242
_gfs->setChunkSize(1);
238243
GridFileBuilder gfb(_gfs.get());
239-
for (int i=0; i<DATA_LEN; i+=2)
240-
gfb.appendChunk(DATA + i, min(2, DATA_LEN - i));
244+
size_t total_size = 0;
245+
for (int i=0; i<DATA_LEN; i+=2) {
246+
size_t chunk_size = min(2, DATA_LEN - i);
247+
gfb.appendChunk(DATA + i, chunk_size);
248+
total_size += chunk_size;
249+
}
241250
gfb.buildFile(DATA_NAME);
242251
GridFile gf = _gfs->findFileByName(DATA_NAME);
243252
ASSERT_EQUALS(gf.getNumChunks(), DATA_LEN);
253+
ASSERT_EQUALS(gf.getContentLength(), total_size);
254+
}
255+
256+
TEST_F(GridFSTest, GridFileBuilderAcrossChunkBoundry) {
257+
_gfs->setChunkSize(11);
258+
GridFileBuilder gfb(_gfs.get());
259+
size_t total_size = 0;
260+
for (int i=0; i<DATA_LEN; i+=2) {
261+
size_t chunk_size = min(2, DATA_LEN - i);
262+
gfb.appendChunk(DATA + i, chunk_size);
263+
total_size += chunk_size;
264+
}
265+
gfb.buildFile(DATA_NAME);
266+
GridFile gf = _gfs->findFileByName(DATA_NAME);
267+
ASSERT_EQUALS(gf.getContentLength(), total_size);
244268
}
245269

246270
} // namespace

0 commit comments

Comments
 (0)