Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,11 @@ static avifResult avifImageCopyProperties(avifImage * dstImage, const avifImage
dstImage->numProperties = 0;

if (srcImage->numProperties != 0) {
dstImage->properties = (avifImageItemProperty *)avifAlloc(srcImage->numProperties * sizeof(srcImage->properties[0]));
AVIF_CHECKERR(srcImage->numProperties <= SIZE_MAX / sizeof(srcImage->properties[0]), AVIF_RESULT_INVALID_ARGUMENT);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not necessary. The API contract requires that srcImage points to a valid avifImage.

const size_t propertiesSize = srcImage->numProperties * sizeof(srcImage->properties[0]);
dstImage->properties = (avifImageItemProperty *)avifAlloc(propertiesSize);
AVIF_CHECKERR(dstImage->properties != NULL, AVIF_RESULT_OUT_OF_MEMORY);
memset(dstImage->properties, 0, srcImage->numProperties * sizeof(srcImage->properties[0]));
memset(dstImage->properties, 0, propertiesSize);
dstImage->numProperties = srcImage->numProperties;
for (size_t i = 0; i < srcImage->numProperties; ++i) {
memcpy(dstImage->properties[i].boxtype, srcImage->properties[i].boxtype, sizeof(srcImage->properties[i].boxtype));
Expand Down
22 changes: 22 additions & 0 deletions tests/gtest/avifimagetest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,27 @@ TEST(AvifImageTest, WriteImage) {
image.get(), (testing::TempDir() + "/avifimagetest.png").c_str()));
}

TEST(AvifImageTest, CopyRejectsTooManyProperties) {
ImagePtr src(avifImageCreateEmpty());
ImagePtr dst(avifImageCreateEmpty());
ASSERT_NE(src, nullptr);
ASSERT_NE(dst, nullptr);

const uint8_t property_type[4] = {'a', 'b', 'c', 'd'};
const uint8_t property_payload = 0;
ASSERT_EQ(avifImageAddOpaqueProperty(src.get(), property_type,
&property_payload,
sizeof(property_payload)),
AVIF_RESULT_OK);

src->numProperties =
std::numeric_limits<size_t>::max() / sizeof(avifImageItemProperty) + 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes src to an invalid avifImage. (This is why the test has to change src->numProperties back to 1 at line 69 before the destructor of src is called.) This violates the API contract that src points to a valid avifImage.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To drive home the necessity of this kind of API contract (whether explicit or implicit), suppose we change this line to the following instead:

    src->numProperties = 2;

It will also make src an invalid avifImage, but the avifImageCopy() function cannot possibly detect this kind of invalid input. So at some point a function has to require that the caller pass a valid input.

EXPECT_EQ(avifImageCopy(dst.get(), src.get(), AVIF_PLANES_ALL),
AVIF_RESULT_INVALID_ARGUMENT);
src->numProperties = 1;
EXPECT_EQ(dst->properties, nullptr);
EXPECT_EQ(dst->numProperties, 0u);
}

} // namespace
} // namespace avif