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

invalid memory address or nil pointer dereference using Header.Peek() in handler #1964

Open
4lisalehi opened this issue Feb 23, 2025 · 1 comment

Comments

@4lisalehi
Copy link

4lisalehi commented Feb 23, 2025

I am encountering the following segfault panic in my fasthttp handler function when using ctx.Request.Header.Peek to retrieve header values:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation]

The issue occurs specifically when calling a synchronous function (GetTestVariants) that processes these header values. Below is a simplified version of the code:

const HeaderKey = "X-Header-Key"

func (a *Handler) Handle(ctx *fasthttp.RequestCtx) {
	header1 := string(ctx.Request.Header.Peek("Header-A"))
	header2 := string(ctx.Request.Header.Peek("Header-B"))

	assignedVariants := a.testProvider.GetTestVariants(header1, header2)

	encoded := encodeGroupVariants(assignedVariants)
	ctx.Response.Header.Set(HeaderKey, encoded)
}

The GetTestVariants function is synchronous and completes before the handler returns. Here is its implementation:

func (p *Provider) GetTestVariants(header1, header2 string) map[string]string {
	p.lock.RLock()

	testVariantsA := p.testProviderA.GetTestVariants(header1)
	testVariantsB := p.testProviderB.GetTestVariants(header2)
        p.lock.RUnlock()

	testVariants := make(map[string]string)
	for group, variant := range testVariantsA {
		testVariants[group] = variant
	}
	for group, variant := range testVariantsB {
		testVariants[group] = variant
	}

	return testVariants
}

Observations:

  • The []byte slices returned by ctx.Request.Header.Peek are converted to strings immediately, and the strings are passed to GetTestVariants.

  • The GetTestVariants function is synchronous and completes before the handler returns.

  • The lifecycle of the []byte slices should be valid for the entire duration of the handler function, as the handler does not return until GetTestVariants completes.

Problem:

Despite the above, I am encountering a segmentation fault when GetTestVariants is called. The fault does not appear to be caused by the lifecycle of the []byte slices, as they are converted to strings immediately and used synchronously. Or maybe I'm wrong and converting []byte to string does not necessary involve allocation, and underlying []byte reuse may occur.

Questions:
Is there any known issue with ctx.Request.Header.Peek or its interaction with synchronous functions?

Are there any additional considerations or edge cases I should be aware of when using ctx.Request.Header.Peek in this way?

@erikdubbelboer
Copy link
Collaborator

If it's the call to GetTestVariants that is panicking like you say, then it's most likely a.testProvider is nil?

I don't see how any of the fasthttp related code you posted could cause a panic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants