common/hexutil: improve hex string syntax validation in Decode function

This commit is contained in:
emmmm 2025-07-02 07:45:32 +02:00 committed by GitHub
parent 62a17fdb25
commit a8562b5f35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -64,6 +64,14 @@ func Decode(input string) ([]byte, error) {
if !has0xPrefix(input) {
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:])
if err != nil {
err = mapError(err)
@ -239,3 +247,8 @@ func mapError(err error) error {
}
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')
}