veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.2k stars 218 forks source link

Fix #383: safe-guard dereferencing first slice indices #385

Closed gonutz closed 5 years ago

gonutz commented 5 years ago

This is a fix for issue 383. Whenever a slice is passed to a function and it is to be passed down to CGo, we need to give it the address of the first slice element and the length of the slice. This is done with code like

C.someFunc(unsafe.Pointer(&x[0]))

The problem with this is that the slice might be empty, thus x[0] will panic at runtime. To go around this problem I have added checks for x == nil at all places that I found where this pattern is used.

veeableful commented 5 years ago

Thanks @gonutz, I'll merge this PR.