mongodb/node-mongodb-native — pull request #1154 by vsivsi (2014-03-15 23:52:27+00:00)
Title: GridFS off-by-one bug in lastChunkNumber() causes uncaught throw and data loss

This is an extremely severe bug in that it causes an uncaught thrown error emanating from the mongodb core (during the md5 sum recalculation) due to a corruption of the chunk numbering following a write of appended data. The included test script `test/tests/manual_tests/lastChunkNumber_off_by_one.js` reproduces this bug, and results in an invalid file with two chunks each with `chunk.n === 0`.

The bug occurs when appending (write mode: "w+") to an existing non-zero length gridFS file with file length exactly equal to a multiple of its chunkSize. That is, when:

(file.length > 0) && ( file.length % file.chunkSize === 0 )

In addition to the 1.4 branch, this bug also affects the currently released version 1.3.23 and probably other under-development branches as well.