Cast dimensions to size_t before multiplication. Also made add_ibuf_size survive cases when image buffer allocation failed.