LibWeb: Use Checked<T> when creating <canvas> bitmap buffers

This commit is contained in:
Andreas Kling 2020-04-15 16:55:36 +02:00
parent f3fc294ac8
commit 1b610ac2b6
Notes: sideshowbarker 2024-07-19 07:34:19 +09:00

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Checked.h>
#include <LibGfx/Bitmap.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/CanvasRenderingContext2D.h>
@ -89,12 +90,15 @@ static Gfx::Size bitmap_size_for_canvas(const HTMLCanvasElement& canvas)
dbg() << "Refusing to create canvas with negative size";
return {};
}
int area = 0;
if (__builtin_mul_overflow(width, height, &area)) {
Checked<size_t> area = width;
area *= height;
if (area.has_overflow()) {
dbg() << "Refusing to create " << width << "x" << height << " canvas (overflow)";
return {};
}
if (area > max_canvas_area) {
if (area.value() > max_canvas_area) {
dbg() << "Refusing to create " << width << "x" << height << " canvas (exceeds maximum size)";
return {};
}
@ -110,7 +114,7 @@ bool HTMLCanvasElement::create_bitmap()
}
if (!m_bitmap || m_bitmap->size() != size)
m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, size);
return true;
return m_bitmap;
}
}