Skip to content

Commit 1262441

Browse files
committed
get rid of potential buffer overrun.
1 parent 8858081 commit 1262441

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

jpeg.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
*/
2727

2828
#include <ruby.h>
29-
#include <rubyio.h>
30-
#include <st.h>
29+
#include <ruby/io.h>
30+
#include <ruby/st.h>
3131

3232
#include <stdio.h>
3333

@@ -661,13 +661,16 @@ im_clip(int argc, VALUE *argv, VALUE self)
661661
dheight = y2 - y1 + 1;
662662
dest = rb_str_new(NULL, 0);
663663
rb_str_resize(dest, dwidth * dheight * components);
664+
memset(RSTRING_PTR(dest), 0xFF, dwidth * dheight * components);
664665

665-
for (y = y1; y <= y2; ++y) {
666+
for (y = y1; y <= y2 && y < height; ++y) {
666667
unsigned char *p = (unsigned char *)&RSTRING_PTR(src)[(x1 + y * width) * components];
667668
unsigned char *q = (unsigned char *)&RSTRING_PTR(dest)[(y - y1) * dwidth * components];
668669
memcpy(q, p, dwidth * components);
669670
}
670671

672+
RB_GC_GUARD(dest); /* need? */
673+
671674
jpeg = rb_class_new_instance(0, 0, cImage);
672675
rb_iv_set(jpeg, "raw_data", dest);
673676
rb_iv_set(jpeg, "width", LONG2NUM(dwidth));

0 commit comments

Comments
 (0)