1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

fix memory leak in obj create functions if ext alloc fails

This commit is contained in:
Gabor Kiss-Vamosi 2019-12-03 18:16:14 +01:00
parent 664a538e85
commit c7374948a1
36 changed files with 154 additions and 38 deletions

View File

@ -1578,14 +1578,17 @@ void lv_obj_set_design_cb(lv_obj_t * obj, lv_design_cb_t design_cb)
* Allocate a new ext. data for an object
* @param obj pointer to an object
* @param ext_size the size of the new ext. data
* @return Normal pointer to the allocated ext
* @return pointer to the allocated ext.
* If out of memory NULL is returned and the original ext is preserved
*/
void * lv_obj_allocate_ext_attr(lv_obj_t * obj, uint16_t ext_size)
{
LV_ASSERT_OBJ(obj, LV_OBJX_NAME);
obj->ext_attr = lv_mem_realloc(obj->ext_attr, ext_size);
void * new_ext = lv_mem_realloc(obj->ext_attr, ext_size);
if(new_ext == NULL) return NULL;
obj->ext_attr = new_ext;
return (void *)obj->ext_attr;
}

View File

@ -247,8 +247,12 @@ void * lv_mem_realloc(void * data_p, size_t new_size)
void * new_p;
new_p = lv_mem_alloc(new_size);
if(new_p == NULL) {
LV_LOG_WARN("Couldn't allocate memory");
return NULL;
}
if(new_p != NULL && data_p != NULL) {
if(data_p != NULL) {
/*Copy the old data to the new. Use the smaller size*/
if(old_size != 0) {
memcpy(new_p, data_p, LV_MATH_MIN(new_size, old_size));
@ -256,7 +260,6 @@ void * lv_mem_realloc(void * data_p, size_t new_size)
}
}
if(new_p == NULL) LV_LOG_WARN("Couldn't allocate memory");
return new_p;
}

View File

@ -62,7 +62,10 @@ lv_obj_t * lv_arc_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the arc type specific extended data*/
lv_arc_ext_t * ext = lv_obj_allocate_ext_attr(new_arc, sizeof(lv_arc_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_arc);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_arc);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_arc);

View File

@ -80,7 +80,10 @@ lv_obj_t * lv_bar_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object type specific extended data*/
lv_bar_ext_t * ext = lv_obj_allocate_ext_attr(new_bar, sizeof(lv_bar_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_bar);
return NULL;
}
ext->min_value = 0;
ext->start_value = 0;

View File

@ -87,7 +87,10 @@ lv_obj_t * lv_btn_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the extended data*/
lv_btn_ext_t * ext = lv_obj_allocate_ext_attr(new_btn, sizeof(lv_btn_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_btn);
return NULL;
}
ext->state = LV_BTN_STATE_REL;

View File

@ -80,7 +80,10 @@ lv_obj_t * lv_btnm_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object type specific extended data*/
lv_btnm_ext_t * ext = lv_obj_allocate_ext_attr(new_btnm, sizeof(lv_btnm_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_btnm);
return NULL;
}
ext->btn_cnt = 0;
ext->btn_id_pr = LV_BTNM_BTN_NONE;

View File

@ -85,7 +85,11 @@ lv_obj_t * lv_calendar_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the calendar type specific extended data*/
lv_calendar_ext_t * ext = lv_obj_allocate_ext_attr(new_calendar, sizeof(lv_calendar_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_calendar);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_calendar);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_calendar);

View File

@ -80,7 +80,11 @@ lv_obj_t * lv_canvas_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the canvas type specific extended data*/
lv_canvas_ext_t * ext = lv_obj_allocate_ext_attr(new_canvas, sizeof(lv_canvas_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_canvas);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_canvas);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_canvas);

View File

@ -65,7 +65,10 @@ lv_obj_t * lv_cb_create(lv_obj_t * par, const lv_obj_t * copy)
lv_cb_ext_t * ext = lv_obj_allocate_ext_attr(new_cb, sizeof(lv_cb_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_cb);
return NULL;
}
ext->bullet = NULL;
ext->label = NULL;

View File

@ -96,7 +96,10 @@ lv_obj_t * lv_chart_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object type specific extended data*/
lv_chart_ext_t * ext = lv_obj_allocate_ext_attr(new_chart, sizeof(lv_chart_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_chart);
return NULL;
}
lv_ll_init(&ext->series_ll, sizeof(lv_chart_series_t));

View File

@ -79,7 +79,10 @@ lv_obj_t * lv_cont_create(lv_obj_t * par, const lv_obj_t * copy)
lv_obj_allocate_ext_attr(new_cont, sizeof(lv_cont_ext_t));
lv_cont_ext_t * ext = lv_obj_get_ext_attr(new_cont);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_cont);
return NULL;
}
LV_ASSERT_MEM(ext);
ext->fit_left = LV_FIT_NONE;

View File

@ -104,7 +104,10 @@ lv_obj_t * lv_cpicker_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the extended data*/
lv_cpicker_ext_t * ext = lv_obj_allocate_ext_attr(new_cpicker, sizeof(lv_cpicker_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_cpicker);
return NULL;
}
/*Initialize the allocated 'ext' */
ext->type = LV_CPICKER_DEF_TYPE;

View File

@ -87,7 +87,10 @@ lv_obj_t * lv_ddlist_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the drop down list type specific extended data*/
lv_ddlist_ext_t * ext = lv_obj_allocate_ext_attr(new_ddlist, sizeof(lv_ddlist_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_ddlist);
return NULL;
}
/*Initialize the allocated 'ext' */
ext->label = NULL;

View File

@ -73,7 +73,10 @@ lv_obj_t * lv_gauge_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the gauge type specific extended data*/
lv_gauge_ext_t * ext = lv_obj_allocate_ext_attr(new_gauge, sizeof(lv_gauge_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_gauge);
return NULL;
}
/*Initialize the allocated 'ext' */
ext->needle_count = 0;

View File

@ -72,7 +72,10 @@ lv_obj_t * lv_img_create(lv_obj_t * par, const lv_obj_t * copy)
/*Extend the basic object to image object*/
lv_img_ext_t * ext = lv_obj_allocate_ext_attr(new_img, sizeof(lv_img_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_img);
return NULL;
}
ext->src = NULL;
ext->src_type = LV_IMG_SRC_UNKNOWN;

View File

@ -62,7 +62,11 @@ lv_obj_t * lv_imgbtn_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the image button type specific extended data*/
lv_imgbtn_ext_t * ext = lv_obj_allocate_ext_attr(new_imgbtn, sizeof(lv_imgbtn_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_imgbtn);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_imgbtn);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_imgbtn);

View File

@ -108,7 +108,10 @@ lv_obj_t * lv_kb_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the keyboard type specific extended data*/
lv_kb_ext_t * ext = lv_obj_allocate_ext_attr(new_kb, sizeof(lv_kb_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_kb);
return NULL;
}
/*Initialize the allocated 'ext' */

View File

@ -88,7 +88,10 @@ lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy)
lv_label_ext_t * ext = lv_obj_get_ext_attr(new_label);
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_label);
return NULL;
}
ext->text = NULL;
ext->static_txt = 0;

View File

@ -68,7 +68,10 @@ lv_obj_t * lv_led_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object type specific extended data*/
lv_led_ext_t * ext = lv_obj_allocate_ext_attr(new_led, sizeof(lv_led_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_led);
return NULL;
}
ext->bright = LV_LED_BRIGHT_ON;

View File

@ -63,7 +63,10 @@ lv_obj_t * lv_line_create(lv_obj_t * par, const lv_obj_t * copy)
/*Extend the basic object to line object*/
lv_line_ext_t * ext = lv_obj_allocate_ext_attr(new_line, sizeof(lv_line_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_line);
return NULL;
}
ext->point_num = 0;
ext->point_array = NULL;

View File

@ -79,7 +79,10 @@ lv_obj_t * lv_list_create(lv_obj_t * par, const lv_obj_t * copy)
lv_list_ext_t * ext = lv_obj_allocate_ext_attr(new_list, sizeof(lv_list_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_list);
return NULL;
}
ext->style_img = NULL;
ext->styles_btn[LV_BTN_STATE_REL] = &lv_style_btn_rel;

View File

@ -64,7 +64,10 @@ lv_obj_t * lv_lmeter_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the line meter type specific extended data*/
lv_lmeter_ext_t * ext = lv_obj_allocate_ext_attr(new_lmeter, sizeof(lv_lmeter_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_lmeter);
return NULL;
}
/*Initialize the allocated 'ext' */
ext->min_value = 0;

View File

@ -78,7 +78,10 @@ lv_obj_t * lv_mbox_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the message box type specific extended data*/
lv_mbox_ext_t * ext = lv_obj_allocate_ext_attr(new_mbox, sizeof(lv_mbox_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_mbox);
return NULL;
}
ext->text = NULL;
ext->btnm = NULL;

View File

@ -60,7 +60,11 @@ lv_obj_t * lv_objmask_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object mask type specific extended data*/
lv_objmask_ext_t * ext = lv_obj_allocate_ext_attr(new_objmask, sizeof(lv_objmask_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_objmask);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_objmask);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_objmask);

View File

@ -68,7 +68,11 @@ lv_obj_t * lv_templ_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the template type specific extended data*/
lv_templ_ext_t * ext = lv_obj_allocate_ext_attr(new_templ, sizeof(lv_templ_ext_t));
lv_mem_assert(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_templ);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_templ);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_templ);

View File

@ -89,7 +89,10 @@ lv_obj_t * lv_page_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object type specific extended data*/
lv_page_ext_t * ext = lv_obj_allocate_ext_attr(new_page, sizeof(lv_page_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_page);
return NULL;
}
ext->scrl = NULL;
ext->sb.hor_draw = 0;

View File

@ -75,7 +75,10 @@ lv_obj_t * lv_preload_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the pre loader type specific extended data*/
lv_preload_ext_t * ext = lv_obj_allocate_ext_attr(new_preload, sizeof(lv_preload_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_preload);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_preload);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_preload);

View File

@ -77,7 +77,10 @@ lv_obj_t * lv_roller_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the roller type specific extended data*/
lv_roller_ext_t * ext = lv_obj_allocate_ext_attr(new_roller, sizeof(lv_roller_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_roller);
return NULL;
}
ext->mode = LV_ROLLER_MODE_NORMAL;

View File

@ -70,7 +70,10 @@ lv_obj_t * lv_slider_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the slider type specific extended data*/
lv_slider_ext_t * ext = lv_obj_allocate_ext_attr(new_slider, sizeof(lv_slider_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_slider);
return NULL;
}
/*Initialize the allocated 'ext' */
ext->style_knob = &lv_style_pretty;

View File

@ -61,7 +61,11 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the spinbox type specific extended data*/
lv_spinbox_ext_t * ext = lv_obj_allocate_ext_attr(new_spinbox, sizeof(lv_spinbox_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_spinbox);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_spinbox);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_spinbox);

View File

@ -72,7 +72,10 @@ lv_obj_t * lv_sw_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the switch type specific extended data*/
lv_sw_ext_t * ext = lv_obj_allocate_ext_attr(new_sw, sizeof(lv_sw_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_sw);
return NULL;
}
/*Initialize the allocated 'ext' */
ext->changed = 0;

View File

@ -98,7 +98,10 @@ lv_obj_t * lv_ta_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object type specific extended data*/
lv_ta_ext_t * ext = lv_obj_allocate_ext_attr(new_ta, sizeof(lv_ta_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_ta);
return NULL;
}
ext->cursor.state = 1;
ext->pwd_mode = 0;

View File

@ -64,7 +64,11 @@ lv_obj_t * lv_table_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the table type specific extended data*/
lv_table_ext_t * ext = lv_obj_allocate_ext_attr(new_table, sizeof(lv_table_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_table);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_table);
if(ancestor_scrl_design == NULL) ancestor_scrl_design = lv_obj_get_design_cb(new_table);

View File

@ -81,7 +81,10 @@ lv_obj_t * lv_tabview_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the tab type specific extended data*/
lv_tabview_ext_t * ext = lv_obj_allocate_ext_attr(new_tabview, sizeof(lv_tabview_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_tabview);
return NULL;
}
/*Initialize the allocated 'ext' */
ext->tab_cur = 0;

View File

@ -74,7 +74,11 @@ lv_obj_t * lv_tileview_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the tileview type specific extended data*/
lv_tileview_ext_t * ext = lv_obj_allocate_ext_attr(new_tileview, sizeof(lv_tileview_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_tileview);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_tileview);
if(ancestor_scrl_signal == NULL) ancestor_scrl_signal = lv_obj_get_signal_cb(lv_page_get_scrl(new_tileview));
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_tileview);

View File

@ -61,7 +61,10 @@ lv_obj_t * lv_win_create(lv_obj_t * par, const lv_obj_t * copy)
/*Allocate the object type specific extended data*/
lv_win_ext_t * ext = lv_obj_allocate_ext_attr(new_win, sizeof(lv_win_ext_t));
LV_ASSERT_MEM(ext);
if(ext == NULL) return NULL;
if(ext == NULL) {
lv_obj_del(new_win);
return NULL;
}
ext->page = NULL;
ext->header = NULL;