Skip to content

Commit 2a93aba

Browse files
radarherehugovk
authored andcommitted
Use strncpy to avoid buffer overflow
1 parent 2237677 commit 2a93aba

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

Tests/icc/sGrey-v2-nano.icc

290 Bytes
Binary file not shown.

Tests/test_imagecms.py

+5
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,11 @@ def test_auxiliary_channels_isolated() -> None:
661661
assert_image_equal(test_image.convert(dst_format[2]), reference_image)
662662

663663

664+
def test_long_modes() -> None:
665+
p = ImageCms.getOpenProfile("Tests/icc/sGrey-v2-nano.icc")
666+
ImageCms.buildTransform(p, p, "ABCDEFGHI", "ABCDEFGHI")
667+
668+
664669
@pytest.mark.parametrize("mode", ("RGB", "RGBA", "RGBX"))
665670
def test_rgb_lab(mode: str) -> None:
666671
im = Image.new(mode, (1, 1))

src/_imagingcms.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ cms_transform_new(cmsHTRANSFORM transform, char *mode_in, char *mode_out) {
201201

202202
self->transform = transform;
203203

204-
strcpy(self->mode_in, mode_in);
205-
strcpy(self->mode_out, mode_out);
204+
strncpy(self->mode_in, mode_in, 8);
205+
strncpy(self->mode_out, mode_out, 8);
206206

207207
return (PyObject *)self;
208208
}
@@ -242,10 +242,9 @@ findLCMStype(char *PILmode) {
242242
// LabX equivalent like ALab, but not reversed -- no #define in lcms2
243243
return (COLORSPACE_SH(PT_LabV2) | CHANNELS_SH(3) | BYTES_SH(1) | EXTRA_SH(1));
244244
}
245-
246245
else {
247-
/* take a wild guess... but you probably should fail instead. */
248-
return TYPE_GRAY_8; /* so there's no buffer overrun... */
246+
/* take a wild guess... */
247+
return TYPE_GRAY_8;
249248
}
250249
}
251250

0 commit comments

Comments
 (0)