Safely checks whether size_t overflow can be caused due to amount of pixels. This check is overcautious rather than precise. If this check indicates no overflow, you can safely compute in a size_t (but not an unsigned): -(size_t)w * (size_t)h * 8 -amount of bytes in IDAT (including filter, padding and Adam7 bytes) -amount of bytes in raw color model Returns 1 if overflow possible, 0 if not. */
| 2822 | Returns 1 if overflow possible, 0 if not. |
| 2823 | */ |
| 2824 | static int lodepng_pixel_overflow(unsigned w, unsigned h, |
| 2825 | const LodePNGColorMode* pngcolor, const LodePNGColorMode* rawcolor) { |
| 2826 | size_t bpp = LODEPNG_MAX(lodepng_get_bpp(pngcolor), lodepng_get_bpp(rawcolor)); |
| 2827 | size_t numpixels, total; |
| 2828 | size_t line; /* bytes per line in worst case */ |
| 2829 | |
| 2830 | if(lodepng_mulofl((size_t)w, (size_t)h, &numpixels)) return 1; |
| 2831 | if(lodepng_mulofl(numpixels, 8, &total)) return 1; /* bit pointer with 8-bit color, or 8 bytes per channel color */ |
| 2832 | |
| 2833 | /* Bytes per scanline with the expression "(w / 8u) * bpp) + ((w & 7u) * bpp + 7u) / 8u" */ |
| 2834 | if(lodepng_mulofl((size_t)(w / 8u), bpp, &line)) return 1; |
| 2835 | if(lodepng_addofl(line, ((w & 7u) * bpp + 7u) / 8u, &line)) return 1; |
| 2836 | |
| 2837 | if(lodepng_addofl(line, 5, &line)) return 1; /* 5 bytes overhead per line: 1 filterbyte, 4 for Adam7 worst case */ |
| 2838 | if(lodepng_mulofl(line, h, &total)) return 1; /* Total bytes in worst case */ |
| 2839 | |
| 2840 | return 0; /* no overflow */ |
| 2841 | } |
| 2842 | #endif /*LODEPNG_COMPILE_DECODER*/ |
| 2843 | #endif /*LODEPNG_COMPILE_PNG*/ |
| 2844 |
no test coverage detected