如何在 Go 1.18 中对 nil 值进行模糊测试?

发布于 2025-01-15 18:42:50 字数 1347 浏览 3 评论 0原文

我正在尝试使用 Go 1.18 go test -fuzz 命令来模糊 nil 值,以便在尝试取消引用指向字符串的指针之前确保我没有忘记安全检查。

这是我的尝试:

// main.go
package main

import (
    "fmt"
)

func HandleString(s *string) {
    // most common programmer error is dereferencing nil pointers without safety checks
    fmt.Println(*s)
}

func main() {
    s := "Hello, world"
    HandleString(&s)
}
// main_test.go
package main

import "testing"

func FuzzHandleString(f *testing.F) {
    h := "hello world"
    for _, seed := range []*string{nil, new(string), &h} {
        f.Add(seed)
    }
    f.Fuzz(func(t *testing.T, in *string) {
        HandleString(in)
    })
}

不幸的是,我收到一条错误消息,指出 go fuzz 不支持指针,仅支持字符串数据类型。

我通过编写这样的测试找到了一种解决方法:

package main

import "testing"

func FuzzHandleString(f *testing.F) {
    h := "hello world"
    for _, seed := range []string{"-1", "", h} {
        f.Add(seed)
    }
    f.Fuzz(func(t *testing.T, in string) {
        if in == "-1" {
            HandleString(nil)
        } else {
            HandleString(&in)
        }
    })
}

这是一个可怕的设计,使得种子数据数组不易重用,并且需要每个模糊函数中的逻辑来欺骗我的方法采用 nil 值。

当然一定有我没有想到的更好的方法吗?

谢谢

I am trying to use Go 1.18 go test -fuzz command to fuzz nil values so that I can ensure I didn't forget safety checks before attempting to dereference a pointer to a string.

Here is my attempt:

// main.go
package main

import (
    "fmt"
)

func HandleString(s *string) {
    // most common programmer error is dereferencing nil pointers without safety checks
    fmt.Println(*s)
}

func main() {
    s := "Hello, world"
    HandleString(&s)
}
// main_test.go
package main

import "testing"

func FuzzHandleString(f *testing.F) {
    h := "hello world"
    for _, seed := range []*string{nil, new(string), &h} {
        f.Add(seed)
    }
    f.Fuzz(func(t *testing.T, in *string) {
        HandleString(in)
    })
}

Unfortunately i get an error saying go fuzz doesn't support pointers and only supports the string datatype.

I found a workaround by writing my test like this:

package main

import "testing"

func FuzzHandleString(f *testing.F) {
    h := "hello world"
    for _, seed := range []string{"-1", "", h} {
        f.Add(seed)
    }
    f.Fuzz(func(t *testing.T, in string) {
        if in == "-1" {
            HandleString(nil)
        } else {
            HandleString(&in)
        }
    })
}

This is a horrible design, and makes the seed data array not easily reusable , and requires logic in every fuzz function to trick my method into taking a nil value.

Surely there has to be a better way I didn't think of?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2025-01-22 18:42:50

您的代码很尴尬,因为这不是 fuzzing 的用途。模糊测试是向你的函数抛出随机值来测试你可能没有考虑到的边缘情况。

你所做的更像是边界测试。您有一个已知的棘手输入,例如 nil、空字符串或空数组。在自己的单元测试中测试它们。

最后,我想问为什么 nilhandleString 的有效输入。 nil 不是字符串。除非有充分的理由,否则它应该是一个错误,并且您应该测试该错误。

Your code is awkward because this is not what fuzzing is for. Fuzzing is to throw random values at your functions to test edge cases you may not have thought about.

What you're doing is more like bounds testing. You have a known tricky inputs like nil, or empty string, or an empty array. Test them in their own unit tests.

Finally, I'd question why nil is a valid input to handleString. nil is not a string. It should be an error, unless there's a good reason otherwise, and you should be testing for that error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文