Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fromHex invalid hex behavior #303

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
- jkkang ([email protected])
- Deklan Webster ([email protected])
- Martin Heidegger ([email protected])
- junderw ([email protected])

#### Generated by bin/update-authors.sh.
35 changes: 32 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,12 @@ function hexWrite (buf, string, offset, length) {
}
let i
for (i = 0; i < length; ++i) {
const parsed = parseInt(string.substr(i * 2, 2), 16)
if (numberIsNaN(parsed)) return i
buf[offset + i] = parsed
const a = hexCharValueTable[string[i * 2]]
junderw marked this conversation as resolved.
Show resolved Hide resolved
const b = hexCharValueTable[string[i * 2 + 1]]
if (a === undefined || b === undefined) {
return i
}
buf[offset + i] = a << 4 | b
}
return i
}
Expand Down Expand Up @@ -2098,6 +2101,32 @@ const hexSliceLookupTable = (function () {
return table
})()

// hex lookup table for Buffer.from(x, 'hex')
const hexCharValueTable = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
a: 10,
b: 11,
c: 12,
d: 13,
e: 14,
f: 15,
A: 10,
B: 11,
C: 12,
D: 13,
E: 14,
F: 15
}

// Return not function with Error if BigInt not supported
function defineBigIntMethod (fn) {
return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn
Expand Down
6 changes: 6 additions & 0 deletions test/node/test-buffer-badhex.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const assert = require('assert');
assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4);
assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0xef, 0x01]));
assert.strictEqual(buf.toString('hex'), 'abcdef01');
// Node Buffer behavior check
// > Buffer.from('abc def01','hex')
// <Buffer ab>
assert.strictEqual(buf.write('abc def01', 0, 'hex'), 1);
assert.deepStrictEqual(buf, new Buffer([0xab]));
assert.strictEqual(buf.toString('hex'), 'ab');
Comment on lines +21 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

buf here is a 4 bytes long buffer, so these asserts can never succeed? Did you try this locally? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

buf should NOT be a 4 byte buffer. That's the point.

If it is a 4 byte buffer, than the code is broken.

a non-hex character should end the hex parsing and only the 2-hex pairs up until the first non-hex is output as a Buffer.

in this case, [0xab]

Copy link
Contributor

Choose a reason for hiding this comment

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

But you are doing .write on an already existing Buffer that is 4 bytes long. write cannot truncate the length of the buffer.

With my updated tests it checks that write only wrote 1 byte, and that it left the three bytes after as 00 00 00.


const copy = Buffer.from(buf.toString('hex'), 'hex');
assert.strictEqual(buf.toString('hex'), copy.toString('hex'));
Expand Down
Loading