jaswdr / faker

:rocket: Ultimate fake data generator for Go with zero dependencies
https://pkg.go.dev/github.com/jaswdr/faker
MIT License
569 stars 59 forks source link

[BUG] Uint() is panicking #87

Closed donatorsky closed 2 years ago

donatorsky commented 2 years ago

Hi! First and foremost, I'd like to thank You an amazing work :) I've been on PHP's side for a long time, where I was using Faker a lot. After switching to Go world, this faker is definitely the easiest and most convenient to use. I haven't seen any issue reported regarding Uint(), so here is what I found out.

Describe the bug Calling Faker.Uint() panics.

To Reproduce

import (
    "fmt"
    "testing"

    "github.com/jaswdr/faker"
)

func TestUint(t *testing.T) {
    fakerInstance := faker.New()

    t.Run("It is panicking", func(t *testing.T) {
        //userId := uint(fakerInstance.Int()) // Temporary workaround
        userId := fakerInstance.UInt()

        fmt.Println(userId)
    })
}

Expected behavior UInt() returns a random uint value.

Screenshots

obraz obraz obraz

Desktop (please complete the following information):

Additional context I was able to debug, that UInt() calls IntBetween(0, maxValueOf_int_type), and IntBetween() is later calling f.Generator.Intn(diff+1). The diff+1 is the problem here since it overflows int type max value and, in the end, when rand.Intn() receives negative value, it panics.

I'm also attaching a few screenshots from debugging chain.

jaswdr commented 2 years ago

@donatorsky thanks for the reporting, this seems to be also the root cause of another issue, I'll check it.

jaswdr commented 2 years ago

I released a fix that potentially would fix this issue, could you try to update to v1.10.2 and try again?

donatorsky commented 2 years ago

@jaswdr Can confirm, it worked for me! 🙂

jaswdr commented 2 years ago

thanks for confirming @donatorsky, I'm resolving this issue for now, but feel free to reopen it if you see it again.