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

Not checking for nil might lead to nil pointer dereference #190

Open
rzajac opened this issue Mar 9, 2022 · 2 comments
Open

Not checking for nil might lead to nil pointer dereference #190

rzajac opened this issue Mar 9, 2022 · 2 comments
Labels

Comments

@rzajac
Copy link
Contributor

rzajac commented Mar 9, 2022

schema/encoder.go

Lines 167 to 173 in 8285576

f := typeEncoder(t.Elem(), reg)
return func(v reflect.Value) string {
if v.IsNil() {
return "null"
}
return f(v.Elem())
}

The f should be checked for nil before it is used in closure.

@zak905
Copy link
Contributor

zak905 commented Mar 26, 2022

Hi @rzajac, do you have an example that triggers a panic ? I think that if f is nil than v will be nil as well, and therefore the closure will return always "null" value, so f(v.Elem()) statement will not be reached

I tried to trigger the case you mentionned but I was not able to

func TestNilPointerDeference(t *testing.T) {
	type C struct {
		R int `schema:"R"`
	}

	type B struct {
		C *C `schema:"C"`
	}

	b := B{C: (*C)(nil)}

	encoder := NewEncoder()

	vals := url.Values{}

	if err := encoder.Encode(b, vals); err != nil {
		t.Fatalf(err.Error())
	}
}

The test is passing without errors.

@zak905
Copy link
Contributor

zak905 commented Jul 3, 2022

@elithrar this can be closed I think

@coreydaley coreydaley added the bug label Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants