Skip to content

Commit

Permalink
Check for negative dimensions in Image#constitute
Browse files Browse the repository at this point in the history
  • Loading branch information
mockdeep committed Oct 7, 2019
1 parent 78b30de commit ce95119
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ext/RMagick/rmimage.c
Expand Up @@ -4124,9 +4124,9 @@ Image_constitute(VALUE class ATTRIBUTE_UNUSED, VALUE width_arg, VALUE height_arg
width = NUM2ULONG(width_arg);
height = NUM2ULONG(height_arg);

if (width == 0 || height == 0)
if ((int)width <= 0 || (int)height <= 0)
{
rb_raise(rb_eArgError, "width and height must be non-zero");
rb_raise(rb_eArgError, "width and height must be greater than zero");
}

map = rm_str2cstr(map_arg, &map_l);
Expand Down
22 changes: 20 additions & 2 deletions spec/rmagick/image/constitute_spec.rb
Expand Up @@ -37,13 +37,31 @@
end

it 'raises an error when 0 is passed for columns' do
expected_message = 'width and height must be greater than zero'

expect { Magick::Image.constitute(0, img.rows, 'RGBA', pixels) }
.to raise_error(ArgumentError, 'width and height must be non-zero')
.to raise_error(ArgumentError, expected_message)
end

it 'raises an error when a negative number is passed for columns' do
expected_message = 'width and height must be greater than zero'

expect { Magick::Image.constitute(-3, img.rows, 'RGBA', pixels) }
.to raise_error(ArgumentError, expected_message)
end

it 'raises an error when 0 is passed for rows' do
expected_message = 'width and height must be greater than zero'

expect { Magick::Image.constitute(img.columns, 0, 'RGBA', pixels) }
.to raise_error(ArgumentError, 'width and height must be non-zero')
.to raise_error(ArgumentError, expected_message)
end

it 'raises an error when a negative number is passed for rows' do
expected_message = 'width and height must be greater than zero'

expect { Magick::Image.constitute(img.columns, -3, 'RGBA', pixels) }
.to raise_error(ArgumentError, expected_message)
end

it 'raises an error given the wrong number of array elements' do
Expand Down

0 comments on commit ce95119

Please sign in to comment.