Skip to content

Commit

Permalink
fix buffer overflow for trim() with image wider than high
Browse files Browse the repository at this point in the history
Dumb error.

Fixes #534
  • Loading branch information
tonycoz committed Nov 14, 2024
1 parent 86a89d6 commit 7851737
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
40 changes: 28 additions & 12 deletions t/300-transform/210-trim.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,54 @@ use Test::More;
use Imager::Test qw(is_image);

{
my $im = Imager->new(channels => 4, xsize => 20, ysize => 20);
$im->box(filled => 1, xmin => 1, ymin => 2, xmax => 16, ymax => 15, color => "#FFF") or die;
my @rect = $im->trim_rect();
#$im->write(file => "trim1.png") or die;
note "@rect";
is_deeply(\@rect, [ 1, 2, 3, 4 ], "check alpha trim");
my $im8 = Imager->new(channels => 4, xsize => 200, ysize => 400);
$im8->box(filled => 1, xmin => 11, ymin => 23, xmax => 161, ymax => 257, color => "#FFF") or die;
my $im16 = $im8->to_rgb16;
for my $test ( [ $im8, 8 ], [ $im16, 16 ]) {
my ($im, $bits) = @$test;

{
my @rect = $im->trim_rect();
note "@rect";
is_deeply(\@rect, [ 11, 23, 38, 142 ], "check alpha trim ($bits)");
}

my $imr = $im->rotate(right => 90)
or die;
is($imr->bits, $bits, "rotated has expected bits ($bits)");

{
my @rect = $imr->trim_rect();
note "@rect";
is_deeply(\@rect, [ 142, 11, 23, 38 ], "check alpha trim ($bits)");
}
}
}

{
my $im = Imager->new(channels => 3, xsize => 20, ysize => 20);
my $im = Imager->new(channels => 3, xsize => 200, ysize => 300);
$im->box(filled => 1, color => "#F00");
$im->box(filled => 1, color => "#00F",
xmin => 1, ymin => 2, xmax => 16, ymax => 15);
xmin => 31, ymin => 42, xmax => 116, ymax => 251);
#$im->write(file => "trim2.png") or die;
{
my @rect = $im->trim_rect(colors => [ "#F00" ]);
note "@rect";
is_deeply(\@rect, [ 1, 2, 3, 4 ], "check simple color trim");
is_deeply(\@rect, [ 31, 42, 83, 48 ], "check simple color trim");
my $trimmed = $im->trim(colors => [ "#F00" ]);
ok($trimmed, "got a simple color trimmed image");
is_image($trimmed, $im->crop(left => 1, top => 2, right => 17, bottom => 16),
is_image($trimmed, $im->crop(left => 31, top => 42, right => 117, bottom => 252),
"check trimmed image is as expected");
}
{
# intrude into the border a little
$im->box(filled => 1, color => "#000", xmin => 16, xmax => 18, ymin => 8, ymax => 10);
my @rect = $im->trim_rect(colors => [ "#F00" ]);
is_deeply(\@rect, [ 1, 2, 1, 4 ],
is_deeply(\@rect, [ 16, 8, 83, 48 ],
"check simple color trim with intrusion");
my $trimmed = $im->trim(colors => [ "#F00" ]);
ok($trimmed, "got a simple color trimmed image");
is_image($trimmed, $im->crop(left => 1, top => 2, right => 19, bottom => 16),
is_image($trimmed, $im->crop(left => 16, top => 8, right => 117, bottom => 252),
"check simple with intrusion trimmed image is as expected");
}
}
Expand Down
2 changes: 1 addition & 1 deletion trim.im
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ trim_rect_simple(i_img *im, double transp_threshold, int color_count,
const int chan_count = check_alpha ? 4 : 3;
i_img_dim x, y;
#code im->bits <= 8
IM_SAMPLE_T *samps = mymalloc(sizeof(IM_SAMPLE_T) * im->ysize * chan_count);
IM_SAMPLE_T *samps = mymalloc(sizeof(IM_SAMPLE_T) * im->xsize * chan_count);
#if IM_EIGHT_BIT
const IM_WORK_T work_threshold = floor(IM_SAMPLE_MAX * transp_threshold);
#else
Expand Down

0 comments on commit 7851737

Please sign in to comment.