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

[Feature] invalid memory address or nil pointer dereference in pkg/client/listen/v1/rest/client.go New() #266

Open
eslam-mahmoud opened this issue Oct 24, 2024 · 1 comment
Labels
enhancement New feature or request low priority Do these last...
Milestone

Comments

@eslam-mahmoud
Copy link

What is the current behavior?

if send nil to deepgram-go-sdk/pkg/client/listen".NewREST in the second argument
we get "invalid memory address or nil pointer dereference"

panic: runtime error: invalid memory address or nil pointer dereference
goroutine 1 [running]:
github.com/deepgram/deepgram-go-sdk/pkg/client/listen/v1/rest.New({0xc000132420, 0x28}, 0x7f82073c95b8?)
/Users/eslam/go/pkg/mod/github.com/deepgram/[email protected]/pkg/client/listen/v1/rest/client.go:51 +0x27
github.com/deepgram/deepgram-go-sdk/pkg/client/listen.NewREST(...)
/Users/eslam/go/pkg/mod/github.com/deepgram/[email protected]/pkg/client/listen/client.go:47

Steps to reproduce

package main

import (
	deepgramAPI "github.com/deepgram/deepgram-go-sdk/pkg/api/listen/v1/rest"
	deepgramClient "github.com/deepgram/deepgram-go-sdk/pkg/client/listen"
)

func main() {
        deepGrampAPIKey := "123"
	dg := deepgramAPI.New(deepgramClient.NewREST(deepGrampAPIKey, nil))
	_ = dg
}

Workaround

package main

import (
	deepgramAPI "github.com/deepgram/deepgram-go-sdk/pkg/api/listen/v1/rest"
	deepgramClient "github.com/deepgram/deepgram-go-sdk/pkg/client/listen"
	"github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
)

func main() {
        deepGrampAPIKey := "123"
	dg := deepgramAPI.New(deepgramClient.NewREST(deepGrampAPIKey, &interfaces.ClientOptions{}))
	_ = dg
}

Suggested fix

In
https://github.com/deepgram/deepgram-go-sdk/blob/30cd597e17a2ea5d56614b0a9641aa44e5b4685f/pkg/client/listen/v1/rest/client.go#L49C1-L63C2
instead of

func New(apiKey string, options *interfaces.ClientOptions) *Client {
	if apiKey != "" {
		options.APIKey = apiKey
	}
	err := options.Parse()
	if err != nil {
		klog.V(1).Infof("options.Parse() failed. Err: %v\n", err)
		return nil
	}

	c := Client{
		common.NewREST(apiKey, options),
	}
	return &c
}

change it to

func New(apiKey string, options *interfaces.ClientOptions) *Client {
	if options == nil {
		options = &interfaces.ClientOptions{}
	}
	if apiKey != "" {
		options.APIKey = apiKey
	}
	err := options.Parse()
	if err != nil {
		klog.V(1).Infof("options.Parse() failed. Err: %v\n", err)
		return nil
	}

	c := Client{
		common.NewREST(apiKey, options),
	}
	return &c
}
@davidvonthenen
Copy link
Contributor

Hi @eslam-mahmoud

The documentation does call out that it is required, but this is good enhancement. Previously, this wasn't a pointer to the struct. When I get chance, I will add this.

@davidvonthenen davidvonthenen changed the title BUG invalid memory address or nil pointer dereference in pkg/client/listen/v1/rest/client.go New() [Feature] invalid memory address or nil pointer dereference in pkg/client/listen/v1/rest/client.go New() Oct 24, 2024
@davidvonthenen davidvonthenen added the enhancement New feature or request label Oct 24, 2024
@davidvonthenen davidvonthenen added this to the Go SDK 1.7 milestone Nov 7, 2024
@davidvonthenen davidvonthenen added the low priority Do these last... label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low priority Do these last...
Projects
None yet
Development

No branches or pull requests

2 participants