jncronin / rpi-boot

A second stage bootloader for the Raspberry Pi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

wrong malloc size in ext2_init

gergaly opened this issue · comments

You allocate memory for the block group descriptor table with size ret->total_groups * sizeof(struct ext2_bgd) in ext2_init function in ext2.c. You round the size up later and read with the new size to a possible smaller memory space.

// Read the block group descriptor table
ret->bgdt = (struct ext2_bgd *)malloc(ret->total_groups * sizeof(struct ext2_bgd));
int bgdt_block = 1;
if(ret->b.block_size == 1024)
    bgdt_block = 2;

uint32_t bgdt_size = ret->total_groups * sizeof(struct ext2_bgd);
// round up to a multiple of block_size
if(bgdt_size % ret->b.block_size)
    bgdt_size = (bgdt_size / ret->b.block_size + 1) * ret->b.block_size;

block_read(parent, (uint8_t *)ret->bgdt, bgdt_size,
    get_sector_num(ret, bgdt_block));

Thank you. Fixed.