[lua] Fix crash using invalid width/height in Image()

This commit is contained in:
David Capello 2020-03-28 12:59:43 -03:00
parent 76ef344f49
commit c7b3e15d05
2 changed files with 11 additions and 1 deletions

View File

@ -124,7 +124,13 @@ int Image_new(lua_State* L)
spec.setHeight(h);
spec.setColorMode((doc::ColorMode)colorMode);
}
if (spec.width() < 1) spec.setWidth(1);
if (spec.height() < 1) spec.setHeight(1);
doc::Image* image = doc::Image::create(spec);
if (!image) {
// Invalid spec (e.g. width=0, height=0, etc.)
return 0;
}
doc::clear_image(image, spec.maskColor());
push_new<ImageObj>(L, image);
return 1;

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2018 Igara Studio S.A.
// Copyright (c) 2018-2020 Igara Studio S.A.
// Copyright (c) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
@ -56,6 +56,10 @@ Image* Image::create(PixelFormat format, int width, int height,
Image* Image::create(const ImageSpec& spec,
const ImageBufferPtr& buffer)
{
ASSERT(spec.width() >= 1 && spec.height() >= 1);
if (spec.width() < 1 || spec.height() < 1)
return nullptr;
switch (spec.colorMode()) {
case ColorMode::RGB: return new ImageImpl<RgbTraits>(spec, buffer);
case ColorMode::GRAYSCALE: return new ImageImpl<GrayscaleTraits>(spec, buffer);