Skip to content

Commit 0bf96cb

Browse files
authored
fix(grid): fix segfault when row/column screen descriptor is missing (lvgl#9790)
1 parent f9c0597 commit 0bf96cb

2 files changed

Lines changed: 119 additions & 8 deletions

File tree

src/layouts/grid/lv_grid.c

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ static void grid_update(lv_obj_t * cont, void * user_data)
192192

193193
lv_grid_calc_t c;
194194
lv_result_t res = calc(cont, &c);
195-
if(res != LV_RESULT_OK) return;
195+
if(res != LV_RESULT_OK) {
196+
calc_free(&c);
197+
return;
198+
}
196199

197200
item_repos_hint_t hint;
198201
lv_memzero(&hint, sizeof(hint));
@@ -230,8 +233,8 @@ static void grid_update(lv_obj_t * cont, void * user_data)
230233
*/
231234
static lv_result_t calc(lv_obj_t * cont, lv_grid_calc_t * calc_out)
232235
{
236+
lv_memzero(calc_out, sizeof(lv_grid_calc_t));
233237
if(lv_obj_get_child(cont, 0) == NULL) {
234-
lv_memzero(calc_out, sizeof(lv_grid_calc_t));
235238
return LV_RESULT_INVALID;
236239
}
237240

@@ -282,12 +285,19 @@ static lv_result_t calc_cols(lv_obj_t * cont, lv_grid_calc_t * c)
282285

283286
const int32_t * col_templ;
284287
col_templ = get_col_dsc(cont);
288+
289+
/*If there is no descriptor check if it's a subgrid*/
285290
bool subgrid = false;
286291
if(col_templ == NULL) {
287292
lv_obj_t * parent = lv_obj_get_parent(cont);
293+
if(parent == NULL) {
294+
LV_LOG_WARN("No column descriptor, and there is no parent for a screen to process subgrid");
295+
return LV_RESULT_INVALID;
296+
}
297+
288298
col_templ = get_col_dsc(parent);
289299
if(col_templ == NULL) {
290-
LV_LOG_WARN("No col descriptor found even on the parent");
300+
LV_LOG_WARN("No column descriptor found even on the parent");
291301
return LV_RESULT_INVALID;
292302
}
293303

@@ -375,9 +385,16 @@ static lv_result_t calc_rows(lv_obj_t * cont, lv_grid_calc_t * c)
375385
{
376386
const int32_t * row_templ;
377387
row_templ = get_row_dsc(cont);
388+
389+
/*If there is no descriptor check if it's a subgrid*/
378390
bool subgrid = false;
379391
if(row_templ == NULL) {
380392
lv_obj_t * parent = lv_obj_get_parent(cont);
393+
if(parent == NULL) {
394+
LV_LOG_WARN("No row descriptor, and there is no parent for a screen to process subgrid");
395+
return LV_RESULT_INVALID;
396+
}
397+
381398
row_templ = get_row_dsc(parent);
382399
if(row_templ == NULL) {
383400
LV_LOG_WARN("No row descriptor found even on the parent");
@@ -471,14 +488,53 @@ static lv_result_t calc_rows(lv_obj_t * cont, lv_grid_calc_t * c)
471488
static void item_repos(lv_obj_t * item, lv_grid_calc_t * c, item_repos_hint_t * hint)
472489
{
473490
if(lv_obj_has_flag_any(item, LV_OBJ_FLAG_IGNORE_LAYOUT | LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_FLOATING)) return;
474-
uint32_t col_span = get_col_span(item);
475-
uint32_t row_span = get_row_span(item);
476-
if(row_span == 0 || col_span == 0) return;
491+
492+
int32_t col_span = get_col_span(item);
493+
if(col_span <= 0) {
494+
LV_LOG_WARN("Column span was %" LV_PRId32 ", setting it to 1", col_span);
495+
col_span = 1;
496+
}
497+
498+
int32_t row_span = get_row_span(item);
499+
if(row_span <= 0) {
500+
LV_LOG_WARN("Row span was %" LV_PRId32 ", setting it to 1", row_span);
501+
row_span = 1;
502+
}
477503

478504
bool rev = lv_obj_get_style_base_dir(lv_obj_get_parent(item), LV_PART_MAIN) == LV_BASE_DIR_RTL;
479505

480-
uint32_t col_pos = get_col_pos(item);
481-
uint32_t row_pos = get_row_pos(item);
506+
int32_t col_pos = get_col_pos(item);
507+
if(col_pos < 0) {
508+
LV_LOG_WARN("Column position was %" LV_PRId32 ", setting it to 0", col_pos);
509+
col_pos = 0;
510+
}
511+
512+
if(col_pos >= (int32_t)c->col_num) {
513+
LV_LOG_WARN("Column position was %" LV_PRId32 ", setting to %" LV_PRId32, col_pos, c->col_num - 1);
514+
col_pos = c->col_num - 1;
515+
}
516+
517+
int32_t row_pos = get_row_pos(item);
518+
if(row_pos < 0) {
519+
LV_LOG_WARN("Row position was %" LV_PRId32 ", setting it to 0", row_pos);
520+
row_pos = 0;
521+
}
522+
523+
if(row_pos >= (int32_t)c->row_num) {
524+
LV_LOG_WARN("Row position was %" LV_PRId32 ", setting to %" LV_PRId32, row_pos, c->row_num - 1);
525+
row_pos = c->row_num - 1;
526+
}
527+
528+
if(col_pos + col_span > (int32_t)c->col_num) {
529+
col_span = c->col_num - col_pos;
530+
LV_LOG_WARN("Column span is too large, limiting it to %" LV_PRId32, col_span);
531+
}
532+
533+
if(row_pos + row_span > (int32_t)c->row_num) {
534+
row_span = c->row_num - row_pos;
535+
LV_LOG_WARN("Row span is too large, limiting it to %" LV_PRId32, row_span);
536+
}
537+
482538
lv_grid_align_t col_align = get_cell_col_align(item);
483539
lv_grid_align_t row_align = get_cell_row_align(item);
484540

tests/src/test_cases/test_grid.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,59 @@ void test_grid_rtl(void)
122122
TEST_ASSERT_EQUAL_SCREENSHOT("grid_rtl.png");
123123
}
124124

125+
126+
void test_grid_no_crash_on_invalid_settings(void)
127+
{
128+
/*Should't crash because of these*/
129+
130+
/*No col/row descriptors on screen*/
131+
lv_obj_t * scr = lv_obj_create(NULL);
132+
lv_screen_load(scr);
133+
lv_obj_set_style_layout(scr, LV_LAYOUT_GRID, 0);
134+
lv_refr_now(NULL);
135+
136+
/*No col/row descriptors on a widget*/
137+
lv_obj_t * cont = lv_obj_create(scr);
138+
lv_obj_set_style_layout(cont, LV_LAYOUT_GRID, 0);
139+
lv_refr_now(NULL);
140+
141+
/*Set a cell without having row/col descriptor on the parent*/
142+
lv_obj_set_grid_cell(cont, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
143+
lv_refr_now(NULL);
144+
145+
/*Add grid descriptors*/
146+
const int32_t col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
147+
const int32_t row_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
148+
lv_obj_set_grid_dsc_array(cont, col_dsc, row_dsc);
149+
lv_refr_now(NULL);
150+
151+
lv_obj_t * label = lv_label_create(cont);
152+
153+
/*Zero span*/
154+
lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 0, 0, LV_GRID_ALIGN_CENTER, 0, 0);
155+
lv_refr_now(NULL);
156+
157+
/*Too large span*/
158+
lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 0, 20, LV_GRID_ALIGN_CENTER, 0, 30);
159+
lv_refr_now(NULL);
160+
161+
/*Negative span*/
162+
lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 0, -50, LV_GRID_ALIGN_CENTER, 0, -20);
163+
lv_refr_now(NULL);
164+
165+
/*Too large position*/
166+
lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 30, 1, LV_GRID_ALIGN_CENTER, 20, 1);
167+
lv_refr_now(NULL);
168+
169+
/*Negative position*/
170+
lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, -100, 1, LV_GRID_ALIGN_CENTER, -20, 1);
171+
lv_refr_now(NULL);
172+
173+
/*Valid settings*/
174+
lv_obj_set_grid_cell(label, LV_GRID_ALIGN_CENTER, 1, 2, LV_GRID_ALIGN_CENTER, 0, 1);
175+
lv_refr_now(NULL);
176+
TEST_PASS();
177+
}
178+
179+
125180
#endif

0 commit comments

Comments
 (0)