mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 06:06:44 +00:00
common/hexutil: improve hex string syntax validation in Decode function
This commit is contained in:
parent
62a17fdb25
commit
a8562b5f35
1 changed files with 13 additions and 0 deletions
|
|
@ -64,6 +64,14 @@ func Decode(input string) ([]byte, error) {
|
||||||
if !has0xPrefix(input) {
|
if !has0xPrefix(input) {
|
||||||
return nil, ErrMissingPrefix
|
return nil, ErrMissingPrefix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for invalid characters before passing to hex.DecodeString
|
||||||
|
for i := 2; i < len(input); i++ {
|
||||||
|
if !isHexCharacter(input[i]) {
|
||||||
|
return nil, ErrSyntax
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
b, err := hex.DecodeString(input[2:])
|
b, err := hex.DecodeString(input[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = mapError(err)
|
err = mapError(err)
|
||||||
|
|
@ -239,3 +247,8 @@ func mapError(err error) error {
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isHexCharacter returns true if c is a valid hexadecimal character.
|
||||||
|
func isHexCharacter(c byte) bool {
|
||||||
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue