From 978e8915da8fda85c094563db692398744d13996 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 1 Mar 2021 11:18:22 -0800 Subject: [PATCH] fix(style): handle the out of memory gracefully in lv_style_[set|remove]_prop (#2101) * fix(style): remove the wrong first break statement from lv_style_prop_get_default and correct the comment and style * fix(style): handle the out of memory gracefully in lv_style_[set|remove]_prop --- src/lv_misc/lv_style.c | 24 +++++++++++++----------- src/lv_misc/lv_style.h | 8 +++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lv_misc/lv_style.c b/src/lv_misc/lv_style.c index 827c7cc4c..c6049c645 100644 --- a/src/lv_misc/lv_style.c +++ b/src/lv_misc/lv_style.c @@ -8,7 +8,6 @@ *********************/ #include "lv_style.h" #include "../lv_misc/lv_mem.h" -#include "../lv_misc/lv_anim.h" /********************* * DEFINES @@ -50,7 +49,6 @@ void lv_style_init(lv_style_t * style) #if LV_USE_ASSERT_STYLE style->sentinel = LV_STYLE_SENTINEL_VALUE; #endif - } void lv_style_reset(lv_style_t * style) @@ -94,13 +92,14 @@ bool lv_style_remove_prop(lv_style_t * style, lv_style_prop_t prop) style->prop_cnt--; size_t size = style->prop_cnt * (sizeof(lv_style_value_t) + sizeof(uint16_t)); uint8_t * new_values_and_props = lv_mem_alloc(size); + if(new_values_and_props == NULL) return false; tmp = new_values_and_props + style->prop_cnt * sizeof(lv_style_value_t); uint16_t * new_props = (uint16_t *) tmp; lv_style_value_t * new_values = (lv_style_value_t *)new_values_and_props; uint32_t j; - for(j = 0; j < (uint32_t)style->prop_cnt + 1; j++) { /* +1: because prop_cnt already reduced but all the old props. needs to be checked. */ + for(j = 0; j <= style->prop_cnt; j++) { /* <=: because prop_cnt already reduced but all the old props. needs to be checked. */ if(props[j] != prop) { *new_values = values[j]; *new_props = props[j]; @@ -123,9 +122,6 @@ void lv_style_set_prop(lv_style_t * style, lv_style_prop_t prop, lv_style_value_ { LV_ASSERT_STYLE(style); - uint8_t group = _lv_style_get_prop_group(prop); - style->has_group |= 1 << group; - if(style->allocated) { uint8_t * tmp = style->v_p.values_and_props + style->prop_cnt * sizeof(lv_style_value_t); uint16_t * props = (uint16_t *) tmp; @@ -140,8 +136,9 @@ void lv_style_set_prop(lv_style_t * style, lv_style_prop_t prop, lv_style_value_ style->prop_cnt++; size_t size = style->prop_cnt * (sizeof(lv_style_value_t) + sizeof(uint16_t)); - style->v_p.values_and_props = lv_mem_realloc(style->v_p.values_and_props, size); - tmp = style->v_p.values_and_props + (style->prop_cnt - 1) * sizeof(lv_style_value_t); + uint8_t * values_and_props = lv_mem_realloc(style->v_p.values_and_props, size); + if(values_and_props == NULL) return; + tmp = values_and_props + (style->prop_cnt - 1) * sizeof(lv_style_value_t); props = (uint16_t *) tmp; /*Shift all props to make place for the value before them*/ for(i = style->prop_cnt - 2; i >= 0; i--) { @@ -149,13 +146,15 @@ void lv_style_set_prop(lv_style_t * style, lv_style_prop_t prop, lv_style_value_ } /*Go to the new position wit the props*/ - tmp = style->v_p.values_and_props + (style->prop_cnt) * sizeof(lv_style_value_t); + tmp = values_and_props + (style->prop_cnt) * sizeof(lv_style_value_t); props = (uint16_t *) tmp; - lv_style_value_t * values = (lv_style_value_t *)style->v_p.values_and_props; + lv_style_value_t * values = (lv_style_value_t *)values_and_props; /*Set the new property and value*/ props[style->prop_cnt - 1] = prop; values[style->prop_cnt - 1] = value; + + style->v_p.values_and_props = values_and_props; } else if(style->prop_cnt == 1) { if(style->prop1 == prop) { style->v_p.value1 = value; @@ -164,6 +163,7 @@ void lv_style_set_prop(lv_style_t * style, lv_style_prop_t prop, lv_style_value_ style->prop_cnt++; size_t size = style->prop_cnt * (sizeof(lv_style_value_t) + sizeof(uint16_t)); uint8_t * values_and_props = lv_mem_alloc(size); + if(values_and_props == NULL) return; uint8_t * tmp = values_and_props + (style->prop_cnt) * sizeof(lv_style_value_t); uint16_t * props = (uint16_t *) tmp; lv_style_value_t * values = (lv_style_value_t *)values_and_props; @@ -180,6 +180,9 @@ void lv_style_set_prop(lv_style_t * style, lv_style_prop_t prop, lv_style_value_ style->prop1 = prop; style->v_p.value1 = value; } + + uint8_t group = _lv_style_get_prop_group(prop); + style->has_group |= 1 << group; } bool lv_style_get_prop(lv_style_t * style, lv_style_prop_t prop, lv_style_value_t * value) @@ -200,7 +203,6 @@ lv_style_value_t lv_style_prop_get_default(lv_style_prop_t prop) { lv_style_value_t value; switch(prop) { - break; case LV_STYLE_TRANSFORM_ZOOM: value.num = LV_IMG_ZOOM_NONE; break; diff --git a/src/lv_misc/lv_style.h b/src/lv_misc/lv_style.h index 063464907..aa9bb96f1 100644 --- a/src/lv_misc/lv_style.h +++ b/src/lv_misc/lv_style.h @@ -26,7 +26,7 @@ extern "C" { * DEFINES *********************/ -#define LV_STYLE_SENTINEL_VALUE 0xAABBCCDD +#define LV_STYLE_SENTINEL_VALUE 0xAABBCCDD /** * Flags for style properties @@ -39,7 +39,7 @@ extern "C" { /** * Other constants */ -#define LV_IMG_ZOOM_NONE 256 /*Value for not zooming the image*/ +#define LV_IMG_ZOOM_NONE 256 /*Value for not zooming the image*/ LV_EXPORT_CONST_INT(LV_IMG_ZOOM_NONE); /********************** @@ -226,8 +226,6 @@ typedef enum { /** * Descriptor for style transitions */ -struct _lv_style_transiton_t; - typedef struct _lv_style_transiton_t{ const lv_style_prop_t * props; /**< An array with the properties to animate. */ const lv_anim_path_t * path; /**< A path for the animation.*/ @@ -292,7 +290,7 @@ void lv_style_reset(lv_style_t * style); * lv_style_value_t v = {.color = value}; lv_style_set_prop(style, MY_PROP, v); } * * ... - * MY_PROP = lv_style_register_prop; + * MY_PROP = lv_style_register_prop(); * ... * lv_style_set_my_prop(&style1, lv_color_red()); */