Memory allocation returning NULL not checked


Issue with memory management in “Shadows” module when there is not enough memory in poll.

What MCU/Processor/Board and compiler are you using?

STM32F469I-DISCO, set to 16bit color.

What do you experience?

Was doing some tests to the port for the STM32F469I-DISCO after updating the example in GitHub for V7, the screen size is 800*480, and when running the “widgets_demo” there where artifacts on the screen, image in attachment.

With an LV_MEM_SIZE set to 32KB, there is a memory allocation problem when drawing shadows (i assume is for the message Box), but there is no check to prevent the code for attempting to draw the shadow if the returned buffer is NULL. And that creates “havoc” in the rest of the design process

Even using a memory monitor (like the one in the simulator) the used memory is only “18720”.

What do you expect?

Tricky… There is a log output in case the user has printf or something like that enabled. In this case if the pointer returned is “NULL” the design process related to that item should be canceled, i think. Or even if in “DEBUG” mode enter while(1) so the user can stop the debugger and check.

Code to reproduce

exception generated by file lv_Draw_rect.c in function draw_shadow at code

    if(sh_cache_size == corner_size && sh_cache_r == r_sh) {
        /*Use the cache if available*/
        sh_buf = _lv_mem_buf_get(corner_size * corner_size);
        _lv_memcpy(sh_buf, sh_cache, corner_size * corner_size);
    else {
        /*A larger buffer is required for calculation */
        sh_buf = _lv_mem_buf_get(corner_size * corner_size * sizeof(uint16_t));
        shadow_draw_corner_buf(&sh_rect_area, (uint16_t *)sh_buf, dsc->shadow_width, r_sh);

        /*Cache the corner if it fits into the cache size*/
        if(corner_size * corner_size < sizeof(sh_cache)) {
            _lv_memcpy(sh_cache, sh_buf, corner_size * corner_size);
            sh_cache_size = corner_size;
            sh_cache_r = r_sh;
    sh_buf = _lv_mem_buf_get(corner_size * corner_size * sizeof(uint16_t));
    shadow_draw_corner_buf(&sh_rect_area, (uint16_t *)sh_buf, dsc->shadow_width, r_sh);

Screenshot and/or video

After some interactions with the screen everything gets “juggled”, have more pictures if necessary

Thank You

Thanks for the report. I’ve added asserts in _lv_mem_buf_get. Now the program should halt if there if out of memory.