Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
buffer: fix hex parsing and add bad hex string test.
  • Loading branch information
chjj committed Jul 8, 2016
commit ae2126747fb79aa879fdcecc1aa12aeca579429b
4 changes: 2 additions & 2 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const int8_t unbase64_table[256] =
};


const int8_t unhex_table[256] =
static const int8_t unhex_table[256] =
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
Expand All @@ -163,7 +163,7 @@ const int8_t unhex_table[256] =
};

#define unhex(x) \
static_cast<uint8_t>(unhex_table[static_cast<uint8_t>(x)])
static_cast<unsigned>(unhex_table[static_cast<uint8_t>(x)])


template <typename TypeName>
Expand Down
44 changes: 44 additions & 0 deletions test/parallel/test-buffer-badhex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
var common = require('../common');
var assert = require('assert');

var Buffer = require('buffer').Buffer;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const here please


// Test hex strings and bad hex strings
{
const buf1 = Buffer.alloc(4);
assert.equal(buf1.length, 4);
assert.deepStrictEqual(buf1, new Buffer([0, 0, 0, 0]));
assert.equal(buf1.write('abcdxx', 0, 'hex'), 2);
assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0x00, 0x00]));
assert.equal(buf1.toString('hex'), 'abcd0000');
assert.equal(buf1.write('abcdef01', 0, 'hex'), 4);
assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0xef, 0x01]));
assert.equal(buf1.toString('hex'), 'abcdef01');

const buf2 = Buffer.from(buf1.toString('hex'), 'hex');
assert.equal(buf1.toString('hex'), buf2.toString('hex'));

const buf3 = Buffer.alloc(5);
assert.equal(buf3.write('abcdxx', 1, 'hex'), 2);
assert.equal(buf3.toString('hex'), '00abcd0000');

const buf4 = Buffer.alloc(4);
assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
assert.equal(buf4.write('xxabcd', 0, 'hex'), 0);
assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
assert.equal(buf4.write('xxab', 1, 'hex'), 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: assert.strictEqual is generally preferred over assert.equal, especially for comparisons with 0. :)

assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
assert.equal(buf4.write('cdxxab', 0, 'hex'), 1);
assert.deepStrictEqual(buf4, new Buffer([0xcd, 0, 0, 0]));

const buf5 = Buffer.alloc(256);
for (let i = 0; i < 256; i++)
buf5[i] = i;

const hex = buf5.toString('hex');
assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf5);

const badHex = hex.slice(0, 256) + 'xx' + hex.slice(256, 510);
assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf5.slice(0, 128));
}