Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(bug) Nil pointer dereference when calling a variation without Init #360

Closed
joshm91 opened this issue Oct 4, 2022 · 4 comments · Fixed by #361
Closed

(bug) Nil pointer dereference when calling a variation without Init #360

joshm91 opened this issue Oct 4, 2022 · 4 comments · Fixed by #361
Assignees
Labels
bug Something isn't working good first issue Good for newcomers p1 High priority

Comments

@joshm91
Copy link

joshm91 commented Oct 4, 2022

Observed behavior

When calling a variation (e.g., ffclient.BoolVariation("test", ffUser, false) without first calling ffclient.Init(), a panic happens due to a nil pointer dereference.

This is happening due to directly referencing ff here:

// BoolVariation return the value of the flag in boolean.
// An error is return if you don't have init the library before calling the function.
// If the key does not exist we return the default value.
func BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error) {
return ff.BoolVariation(flagKey, user, defaultValue)
}

where ff is a pointer type:

var ff *GoFeatureFlag

that is only set when calling Init():

func Init(config Config) error {
var err error
onceFF.Do(func() {
ff, err = New(config)
})
return err
}

Expected Behavior

I expect the variation call to return an error, and set the return value to the default specified in the call. This behaviour is expected based on the existing documentation, both in function docs:

// BoolVariation return the value of the flag in boolean.
// An error is return if you don't have init the library before calling the function.
// If the key does not exist we return the default value.
func BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error) {

and in the documentation site:

The default value is return when an error is encountered _(`ffclient` not initialized, variation with wrong type, flag does not exist ...)._

I do not expect a panic/nil pointer dereference.

Steps to reproduce

package main

import (
	"fmt"

	ffclient "github.com/thomaspoignant/go-feature-flag"
	"github.com/thomaspoignant/go-feature-flag/ffuser"
)

func main() {
	res, err := ffclient.BoolVariation("test", ffuser.NewUser("test"), false) // <-- This results in nil pointer dereference
	fmt.Println(res)
	fmt.Println(err)
}

Results in:

➜ go run .
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x99 pc=0x102cc2230]

goroutine 1 [running]:
github.com/thomaspoignant/go-feature-flag.(*GoFeatureFlag).boolVariation(0x12a2bbfff?, {0x102cc30e3?, 0x14000066da8?}, {{0x102cc30e3?, 0x12a3f2e40?}, 0x1?, 0x14000112780?}, 0x8?)
	/Users/josh/go/pkg/mod/github.com/thomaspoignant/[email protected]/variation.go:217 +0x40
github.com/thomaspoignant/go-feature-flag.(*GoFeatureFlag).BoolVariation(0x14000159f28?, {0x102cc30e3, 0x4}, {{0x102cc30e3?, 0x0?}, 0xa0?, 0x14000112780?}, 0x44?)
	/Users/josh/go/pkg/mod/github.com/thomaspoignant/[email protected]/variation.go:84 +0x48
github.com/thomaspoignant/go-feature-flag.BoolVariation(...)
	/Users/josh/go/pkg/mod/github.com/thomaspoignant/[email protected]/variation.go:26
main.main()
	/var/folders/f4/dlqjqljs70dbl279lrbdcdjc0000gn/T/tmp.6GuEmS7X/main.go:11 +0x58
exit status 2
@joshm91 joshm91 added bug Something isn't working needs-triage A priority should be added to the issue labels Oct 4, 2022
@thomaspoignant thomaspoignant added p1 High priority good first issue Good for newcomers and removed needs-triage A priority should be added to the issue labels Oct 5, 2022
@thomaspoignant thomaspoignant self-assigned this Oct 5, 2022
@thomaspoignant
Copy link
Owner

@joshm91 thanks for this super details issue 🙏 this is much appreciated.
I am sorry that this bug was introduced.

I have opened a PR #361 to fix the issue.
I will create a new version soon with the fix.

@thomaspoignant
Copy link
Owner

The fix is available in the version v0.28.1.

@joshm91
Copy link
Author

joshm91 commented Oct 5, 2022

Wow, thank you so much for the quick fix, and for this great library in general!

@thomaspoignant
Copy link
Owner

Wow, thank you so much for the quick fix, and for this great library in general!

Thanks a lot for your feedback this is much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers p1 High priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants