Permalink
Browse files

[fix] Copy Buffers instead of slicing them for ObjectId and Binary va…

…lues (fixes GH-5)
  • Loading branch information...
1 parent 233d05b commit 1d3e0b35fa34ab22081693c89fc6f192bce38e07 @marcello3d committed Feb 1, 2012
Showing with 9 additions and 3 deletions.
  1. +9 −3 lib/bson.js
View
@@ -293,12 +293,15 @@ function parse(buffer, offset, isArray) {
case BINARY_TYPE:
var binaryLength = readInt()
var subtype = buffer[offset++]
- var binary = buffer.slice(offset, offset += binaryLength)
+ var binary = new Buffer(binaryLength)
+ buffer.copy(binary, 0, offset, offset += binaryLength)
binary.subtype = subtype
object[name] = binary
break
case OBJECT_ID_TYPE:
- object[name] = new ObjectId(buffer.slice(offset, offset += 12))
+ var objectIdBuffer = new Buffer(12)
+ buffer.copy(objectIdBuffer, 0, offset, offset +=12)
+ object[name] = new ObjectId(objectIdBuffer)
break
case BOOLEAN_TYPE:
object[name] = buffer[offset++] == 1
@@ -310,7 +313,10 @@ function parse(buffer, offset, isArray) {
object[name] = new RegExp(readCString(), readCString())
break
case DB_REF_TYPE:
- object[name] = new DBRef(readString(), new ObjectId(buffer.slice(offset, offset += 12)))
+ var dbrefName = readString()
+ var dbrefObjectId = new Buffer(12)
+ buffer.copy(dbrefObjectId, 0, offset, offset +=12)
+ object[name] = new DBRef(dbrefName, new ObjectId(dbrefObjectId))
break
case CODE_TYPE:
object[name] = makeFunction(readString())

0 comments on commit 1d3e0b3

Please sign in to comment.