Skip to content

Commit

Permalink
Don't suppress encoding errors when shared structures are saved
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Nov 16, 2023
1 parent 6b010f5 commit 0e960cb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class Packr extends Unpackr {
}
if (hasSharedUpdate)
hasSharedUpdate = false
let encodingError;
try {
if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
writeStruct(value);
Expand Down Expand Up @@ -171,6 +172,9 @@ export class Packr extends Unpackr {
return target
}
return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
} catch(error) {
encodingError = error;
throw error;
} finally {
if (structures) {
resetStructures();
Expand All @@ -179,12 +183,14 @@ export class Packr extends Unpackr {
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
let returnBuffer = target.subarray(start, position)
let newSharedData = prepareStructures(structures, packr);
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
// get updated structures and try again if the update failed
return packr.pack(value, encodeOptions)
if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
// get updated structures and try again if the update failed
return packr.pack(value, encodeOptions)
}
packr.lastNamedStructuresLength = sharedLength
return returnBuffer
}
packr.lastNamedStructuresLength = sharedLength
return returnBuffer
}
}
if (encodeOptions & RESET_BUFFER_MODE)
Expand Down
15 changes: 15 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,21 @@ suite('msgpackr basic tests', function() {
var deserialized = unpack(serialized)
assert.equal(deserialized.aDate, data.aDate.toString())
})
test('standard pack fails on circular reference with shared structures', function () {
var data = {}
data.self = data;
let structures = []
let savedStructures
let packr = new Packr({
structures,
saveStructures(structures) {
savedStructures = structures
}
})
assert.throws(function () {
packr.pack(data);
});
})

test('proto handling', function() {
var objectWithProto = JSON.parse('{"__proto__":{"foo":3}}');
Expand Down

0 comments on commit 0e960cb

Please sign in to comment.