Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Fix utf8 decoding (#5533)
Browse files Browse the repository at this point in the history
* Fixes #5492

* Linting
  • Loading branch information
ngotchac authored and jacogr committed May 2, 2017
1 parent b0e4c91 commit a56b2f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion js/src/abi/decoder/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ export default class Decoder {

const str = taken.bytes.map((code) => String.fromCharCode(code)).join('');

return new DecodeResult(new Token(param.type, utf8.decode(str)), offset + 1);
let decoded;

try {
decoded = utf8.decode(str);
} catch (error) {
decoded = str;
}

return new DecodeResult(new Token(param.type, decoded), offset + 1);

case 'array':
lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber();
Expand Down
8 changes: 8 additions & 0 deletions js/src/abi/decoder/decoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('abi/decoder/Decoder', () => {
const int1 = '0111111111111111111111111111111111111111111111111111111111111111';
const intn = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85';
const string1 = '6761766f66796f726b0000000000000000000000000000000000000000000000';
const string2 = '4665726ee16e64657a0000000000000000000000000000000000000000000000';
const tokenAddress1 = new Token('address', `0x${address1.slice(-40)}`);
const tokenAddress2 = new Token('address', `0x${address2.slice(-40)}`);
const tokenAddress3 = new Token('address', `0x${address3.slice(-40)}`);
Expand All @@ -53,6 +54,7 @@ describe('abi/decoder/Decoder', () => {
const tokenUint1 = new Token('uint', new BigNumber(int1, 16));
const tokenUintn = new Token('uint', new BigNumber(intn, 16));
const tokenString1 = new Token('string', 'gavofyork');
const tokenString2 = new Token('string', 'Fernández');
const slices = [ address1, address2, address3, address4 ];

describe('peek', () => {
Expand Down Expand Up @@ -160,6 +162,12 @@ describe('abi/decoder/Decoder', () => {
).to.deep.equal(tokenString1);
});

it('decodes utf8-invalid string', () => {
expect(
Decoder.decodeParam(new ParamType('string'), [padU32(0x20), padU32(9), string2], 0).token
).to.deep.equal(tokenString2);
});

it('decodes string (indexed)', () => {
expect(
Decoder.decodeParam(new ParamType('string', null, 0, true), [bytes1], 0)
Expand Down

0 comments on commit a56b2f5

Please sign in to comment.