Added memory check to ws2812 example

This commit is contained in:
Benjamin Vedder 2022-05-12 16:29:10 +02:00
parent 008cddc61d
commit cb5f1eb88c
1 changed files with 31 additions and 9 deletions

View File

@ -241,6 +241,10 @@ static lbm_value ext_init(lbm_value *args, lbm_uint argn) {
}
ws_cfg *cfg = VESC_IF->malloc(sizeof(ws_cfg));
bool ok = false;
if (cfg) {
cfg->num_leds = VESC_IF->lbm_dec_as_i32(args[0]);
cfg->is_ch2 = VESC_IF->lbm_dec_as_i32(args[1]);
cfg->is_tim4 = VESC_IF->lbm_dec_as_i32(args[2]);
@ -251,6 +255,24 @@ static lbm_value ext_init(lbm_value *args, lbm_uint argn) {
cfg->RGBdata = VESC_IF->malloc(sizeof(uint32_t) * cfg->ledbuf_len);
cfg->brightness = 100;
ok = cfg->bitbuffer != NULL && cfg->RGBdata != NULL;
}
if (!ok) {
if (cfg) {
if (cfg->bitbuffer) {
VESC_IF->free(cfg->bitbuffer);
}
if (cfg->RGBdata) {
VESC_IF->free(cfg->RGBdata);
}
VESC_IF->free(cfg);
}
VESC_IF->lbm_set_error_reason("Not enough memory");
return lbm_enc_sym(SYM_EERROR);
}
ws2812_init(cfg);
ARG = cfg;