Skip to content
Go: Discard partial bytes when hex decoding fails

Go: Discard partial bytes when hex decoding fails

November 13, 2026

hex.DecodeString can return decoded bytes together with an error. For an all-or-nothing field, discard that partial result before handing data to the rest of the program.

Set up

Tested on Linux with Go 1.26.10. Use Bash with Go on your PATH. Keep the same terminal through cleanup, and stop if setup fails.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as hex_decode_reject_partial.go:

package main

import (
	"encoding/hex"
	"fmt"
)

func decodeField(input string) ([]byte, error) {
	data, err := hex.DecodeString(input)
	if err != nil {
		return nil, err
	}
	return data, nil
}
func main() {
	for _, input := range []string{"4142", "41zz", "414", ""} {
		partial, rawErr := hex.DecodeString(input)
		accepted, err := decodeField(input)
		fmt.Printf("%q: raw=%x raw_error=%t accepted=%x error=%t\n", input, partial, rawErr != nil, accepted, err != nil)
	}
}
go run hex_decode_reject_partial.go

Observed output:

"4142": raw=4142 raw_error=false accepted=4142 error=false
"41zz": raw=41 raw_error=true accepted= error=true
"414": raw=41 raw_error=true accepted= error=true
"": raw= raw_error=false accepted= error=false

Read the result

Both malformed inputs contain a complete first byte. The raw call returns that byte, while the wrapper returns no accepted data. The error remains available to the caller. Empty input is valid hex and produces an empty result, which is different from a decoding failure.

Apply it carefully

Successful decoding checks the hex representation only. Enforce the required decoded length separately for a fixed-size identifier or digest. Bound untrusted input before decoding to control allocation, and do not silently remove invalid characters to make a malformed field pass.

See the Go standard-library reference.

Clean up

Remove the source and the empty scratch directory. Go may retain build-cache files outside this directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- hex_decode_reject_partial.go
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on