-
-
Notifications
You must be signed in to change notification settings - Fork 233
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
Conversation
Note: This new implementation also mimics the code for NodeJS hex parser used in the https://github.com/nodejs/node/blob/v14.18.1/src/string_bytes.cc#L246-L261 |
assert.deepStrictEqual(buf, new Buffer([0xab])); | ||
assert.strictEqual(buf.toString('hex'), 'ab'); |
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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
.
With CI passing now, I'm slightly more confident with this going out (see #335) |
In NodeJS, the behavior of Buffer.from(x, 'hex') differed slightly on its handling of bad hex parsing.
My fix also increased performance. I tried parsing a hex string
'7c'.repeat(5e7)
and it went from 2.8 s to ~800ms.