Skip to content

Commit

Permalink
Fix borsh map deserialization
Browse files Browse the repository at this point in the history
getSpan() was computing value size from the wrong offset.
The constructor also caused wrong calculations when only one of the
layouts has a variable size.
  • Loading branch information
kiwec committed Feb 11, 2025
1 parent fa38102 commit d557023
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions ts/packages/borsh/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class MapEntryLayout<K, V> extends LayoutCls<[K, V]> {
valueLayout: Layout<V>;

constructor(keyLayout: Layout<K>, valueLayout: Layout<V>, property?: string) {
super(keyLayout.span + valueLayout.span, property);
super(0, property);
this.keyLayout = keyLayout;
this.valueLayout = valueLayout;
}
Expand All @@ -322,9 +322,10 @@ class MapEntryLayout<K, V> extends LayoutCls<[K, V]> {
}

getSpan(b: Buffer, offset?: number): number {
return (
this.keyLayout.getSpan(b, offset) + this.valueLayout.getSpan(b, offset)
);
offset = offset || 0;
const keySpan = this.keyLayout.getSpan(b, offset);
const valueSpan = this.valueLayout.getSpan(b, offset + keySpan);
return keySpan + valueSpan;
}
}

Expand Down

0 comments on commit d557023

Please sign in to comment.