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

util: check that hex to byte conversion is valid in hexToBytes #3185

Merged
merged 6 commits into from
Dec 12, 2023

Conversation

gabrocheleau
Copy link
Contributor

@gabrocheleau gabrocheleau commented Dec 9, 2023

This addresses an issue where an invalid hex (e.g. '0x\x19Ethereum Signed Message:\n32') ) wouldn't throw.

We now validate that every parsing of hex to int (through parseInt in base 16) returns a valid int and not a NaN.

Copy link

codecov bot commented Dec 9, 2023

Codecov Report

Merging #3185 (f2afa31) into master (de70612) will decrease coverage by 0.26%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
block 87.52% <ø> (ø)
blockchain 91.60% <ø> (ø)
client 84.56% <ø> (+0.03%) ⬆️
common 98.25% <ø> (ø)
devp2p 82.11% <ø> (?)
ethash ∅ <ø> (∅)
evm 73.88% <ø> (ø)
statemanager 85.72% <ø> (ø)
trie 89.76% <ø> (ø)
tx 95.73% <ø> (ø)
util 89.05% <100.00%> (ø)
vm 80.83% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@jochem-brouwer
Copy link
Member

Have tested, with and without checks:

jochem@jochem-OMEN-Laptop-15-ek1xxx:~/Documents/ejs2/hive/clients/ethereumjs/ethereumjs-monorepo/packages/util$ ts-node ./test/test
hexToBytes: 446.597ms
jochem@jochem-OMEN-Laptop-15-ek1xxx:~/Documents/ejs2/hive/clients/ethereumjs/ethereumjs-monorepo/packages/util$ ts-node ./test/test
hexToBytes: 449.86ms
jochem@jochem-OMEN-Laptop-15-ek1xxx:~/Documents/ejs2/hive/clients/ethereumjs/ethereumjs-monorepo/packages/util$ ts-node ./test/test
hexToBytes: 443.832ms
jochem@jochem-OMEN-Laptop-15-ek1xxx:~/Documents/ejs2/hive/clients/ethereumjs/ethereumjs-monorepo/packages/util$ npm run build

> @ethereumjs/util@9.0.1 build
> ../../config/cli/ts-build.sh

[Node build] Using tsconfig.prod.cjs.json
> tsc --build ./tsconfig.prod.cjs.json
[Node build] Working... DONE

[ESM build] Using tsconfig.prod.esm.json
> tsc --build ./tsconfig.prod.esm.json
[ESM build] Working... DONE

[Post Build Fixes]Adding ./dist/cjs/package.json
Adding ./dist/esm/package.json


jochem@jochem-OMEN-Laptop-15-ek1xxx:~/Documents/ejs2/hive/clients/ethereumjs/ethereumjs-monorepo/packages/util$ ts-node ./test/test
hexToBytes: 428.618ms
jochem@jochem-OMEN-Laptop-15-ek1xxx:~/Documents/ejs2/hive/clients/ethereumjs/ethereumjs-monorepo/packages/util$ ts-node ./test/test
hexToBytes: 442.045ms
jochem@jochem-OMEN-Laptop-15-ek1xxx:~/Documents/ejs2/hive/clients/ethereumjs/ethereumjs-monorepo/packages/util$ ts-node ./test/test
hexToBytes: 445.398ms

@jochem-brouwer
Copy link
Member

Performance influence is thus neglegible, but before merging would like an opinion from @acolytec3 @holgerd77

acolytec3
acolytec3 previously approved these changes Dec 10, 2023
Copy link
Contributor

@acolytec3 acolytec3 left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -87,8 +87,8 @@ export const hexToBytes = (hex: string): Uint8Array => {
throw new Error(`hex argument type ${typeof hex} must be of type string`)
}

if (!hex.startsWith('0x')) {
throw new Error(`prefixed hex input should start with 0x, got ${hex.substring(0, 2)}`)
if (!/^0x[0-9a-fA-F]*$/.test(hex)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this approach better. Hopefully doesn't turn out to be a performance hit.

Copy link
Contributor

@acolytec3 acolytec3 left a comment

Choose a reason for hiding this comment

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

LGTM

@acolytec3 acolytec3 merged commit 9698584 into master Dec 12, 2023
46 checks passed
@scorbajio scorbajio deleted the util/validate-hex-in-hextobytes branch December 12, 2023 16:56
@holgerd77
Copy link
Member

Did also a simple performance test here, for these kind of utility method test a simple one liner in ts-node or so also always work pretty well:

console.time('t'); for (let i=0; i<100000; i++) { hexToBytes(`0x123456789${i}`) }; console.timeEnd('t');

Can confirm that there seems to be no significant differences in performance. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants