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

Panic when invalid config is passed #9

Open
lukasmalkmus opened this issue Mar 1, 2018 · 0 comments
Open

Panic when invalid config is passed #9

lukasmalkmus opened this issue Mar 1, 2018 · 0 comments

Comments

@lukasmalkmus
Copy link
Contributor

Hi @mcuadros. A while back I blindly submitted a PR which I prepared in my head. My idea was to catch panics which were caused by an invalid config or not running as root. But I just noticed that this doesn't work as expected.

The reason why the code panics is located in these two lines.

m := C.led_matrix_create_from_options(config.toC(), nil, nil)
b := C.led_matrix_create_offscreen_canvas(m)
// Some other code ...
if m == nil {
	return nil, fmt.Errorf("unable to allocate memory")
}

If the application is not running as root, m will benil. Why this case is handled, m is still passed to b := C.led_matrix_create_offscreen_canvas(m) without checking that m is not nil! This causes the panic.

A simple solution could look like this:

m := C.led_matrix_create_from_options(config.toC(), nil, nil)
if m == nil {
	return nil, fmt.Errorf("unable to allocate memory for matrix")
}
b := C.led_matrix_create_offscreen_canvas(m)
if b == nil {
	return nil, fmt.Errorf("unable to allocate memory for offscreen canvas")
}
// Do some other stuff ...

Feel free to suggest a different solution or drop me line if you want me to prepare a PR.

jcrd added a commit to jcrd/go-rpi-rgb-led-matrix that referenced this issue Mar 9, 2021
jcrd added a commit to jcrd/go-rpi-rgb-led-matrix that referenced this issue Mar 17, 2021
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

1 participant