Don't assume that pixbuf_len == number of pixels

This commit is contained in:
Nigel Tao committed 2018-09-08 09:21:24 +10:00
1 parent fbbdc412d1
commit 29986823e5
1 file changed
+11 -8
+11 -8
View File
@@ -225,12 +225,14 @@ size_t print_color_art(wuffs_base__pixel_buffer* pb) {
// ---- // ----
const char* allocate(wuffs_base__image_config* ic) { const char* allocate(wuffs_base__image_config* ic) {
image_len = wuffs_base__pixel_config__pixbuf_len(&ic->pixcfg); uint32_t width = wuffs_base__pixel_config__width(&ic->pixcfg);
if (image_len > (SIZE_MAX / sizeof(wuffs_base__color_u32argb))) { uint32_t height = wuffs_base__pixel_config__height(&ic->pixcfg);
uint64_t num_pixels = ((uint64_t)width) * ((uint64_t)height);
if (num_pixels > (SIZE_MAX / sizeof(wuffs_base__color_u32argb))) {
return "could not allocate dst buffer"; return "could not allocate dst buffer";
} }
dst_len = image_len * sizeof(wuffs_base__color_u32argb); dst_len = num_pixels * sizeof(wuffs_base__color_u32argb);
dst_buffer = (wuffs_base__color_u32argb*)malloc(dst_len); dst_buffer = (wuffs_base__color_u32argb*)malloc(dst_len);
if (!dst_buffer) { if (!dst_buffer) {
return "could not allocate dst buffer"; return "could not allocate dst buffer";
@@ -243,6 +245,7 @@ const char* allocate(wuffs_base__image_config* ic) {
return "could not allocate prev-dst buffer"; return "could not allocate prev-dst buffer";
} }
image_len = wuffs_base__pixel_config__pixbuf_len(&ic->pixcfg);
image_buffer = malloc(image_len); image_buffer = malloc(image_len);
if (!image_buffer) { if (!image_buffer) {
free(prev_dst_buffer); free(prev_dst_buffer);
@@ -267,8 +270,6 @@ const char* allocate(wuffs_base__image_config* ic) {
return "could not allocate image buffer"; return "could not allocate image buffer";
} }
uint32_t width = wuffs_base__pixel_config__width(&ic->pixcfg);
uint32_t height = wuffs_base__pixel_config__height(&ic->pixcfg);
uint64_t plen = 1 + ((uint64_t)(width) + 1) * (uint64_t)(height); uint64_t plen = 1 + ((uint64_t)(width) + 1) * (uint64_t)(height);
uint64_t bytes_per_print_pixel = color_flag ? BYTES_PER_COLOR_PIXEL : 1; uint64_t bytes_per_print_pixel = color_flag ? BYTES_PER_COLOR_PIXEL : 1;
if (plen <= ((uint64_t)SIZE_MAX) / bytes_per_print_pixel) { if (plen <= ((uint64_t)SIZE_MAX) / bytes_per_print_pixel) {
@@ -326,9 +327,11 @@ const char* play() {
if (msg) { if (msg) {
return msg; return msg;
} }
z = wuffs_base__pixel_buffer__set_from_slice( z = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg,
&pb, &ic.pixcfg, ((wuffs_base__slice_u8){
((wuffs_base__slice_u8){.ptr = image_buffer, .len = image_len})); .ptr = image_buffer,
.len = image_len,
}));
if (z) { if (z) {
return z; return z;
} }