don't deref NULL
more NULL checks
mapped_idx is unsigned
avoid gcc warning

it is not used as uninit, it is just malloc that confuses gcc