icrowley / fake

Fake data generator for Go (Golang)
MIT License
592 stars 77 forks source link

fix data race in function generate() #36

Closed IrisesD closed 5 months ago

IrisesD commented 5 months ago

Hi, I've looked through the PRs and found that you've been working on solve the unsafe random number generator before. However, the problem still exists as:

func generate(lang, cat string, fallback bool) string {
    format := lookup(lang, cat+"_format", fallback)
    var result string
    for _, ru := range format {
        if ru != '#' {
            result += string(ru)
        } else {
            result += strconv.Itoa(r.Intn(10))
        }
    }
    return result
}

this function still remains using a global random number generator, which is unsafe if it's called concurrently. So I propose this PR to fix the problem.