mcuadros/go-rpi-rgb-led-matrix

Panic when invalid config is passed

lukasmalkmus opened this issue · 0 comments

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.