2

I'm looking for a efficient way to test fixed-length strings via fuzzing (so that only the content of the string is variable, not the length). My first idea was to use arrays for this like

f.Fuzz(func(t *testing.T, stringUnderTest [8]byte))

Sadly this doesn't work, as arrays are not supported (https://go.dev/security/fuzz/). Is there any way to tell the fuzzer: "This string is only 8 digits"?

1 Answer 1

0

You can translate several machine words to the bytes sequence.

func wordsToBytes(words ...uint64) []byte {
    buf := make([]byte, 8*len(words))
    for n, v := range words {
        for i := range 8 {
            buf[n+i] = byte(v >> i * 8)
        }
    }
    return buf
 }

Then your fuzz target can accept several uint64 which will be converted to bytes.

func FuzzFoo(f *testing.F) {

    f.Fuzz(func(t *testing.T, w1 uint64, w2 uint64) {
        raw := wordsToBytes(w1, w2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.