@@ -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 */
231234static 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)
471488static 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
0 commit comments