gorilla/schema

Not checking for nil might lead to nil pointer dereference

rzajac opened this issue ยท 4 comments

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.

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.

@elithrar this can be closed I think

Thanks heaps @zak905 closing

sure thing!