diff --git a/source/planar_functions.cc b/source/planar_functions.cc index c662cfef6..141db6efb 100644 --- a/source/planar_functions.cc +++ b/source/planar_functions.cc @@ -35,6 +35,9 @@ void CopyPlane(const uint8_t* src_y, int height) { int y; void (*CopyRow)(const uint8_t* src, uint8_t* dst, int width) = CopyRow_C; + if (width == 0 || height == 0) { + return; + } // Negative height means invert the image. if (height < 0) { height = -height; diff --git a/unit_test/planar_test.cc b/unit_test/planar_test.cc index 833091a10..8f5a33cb1 100644 --- a/unit_test/planar_test.cc +++ b/unit_test/planar_test.cc @@ -1484,6 +1484,41 @@ TEST_F(LibYUVPlanarTest, TestCopyPlane) { EXPECT_EQ(0, err); } +TEST_F(LibYUVPlanarTest, TestCopyPlaneZeroDimensionRegressionTest) { + // Regression test to verify copying a rect with a zero height or width does + // not lead to memory corruption. + uint8_t src = 42; + uint8_t dst = 0; + + // Disable all optimizations. + MaskCpuFlags(disable_cpu_flags_); + CopyPlane(&src, 0, &dst, 0, 0, 0); + EXPECT_EQ(src, 42); + EXPECT_EQ(dst, 0); + + CopyPlane(&src, 1, &dst, 1, 1, 0); + EXPECT_EQ(src, 42); + EXPECT_EQ(dst, 0); + + CopyPlane(&src, 1, &dst, 1, 0, 1); + EXPECT_EQ(src, 42); + EXPECT_EQ(dst, 0); + + // Enable optimizations. + MaskCpuFlags(benchmark_cpu_info_); + CopyPlane(&src, 0, &dst, 0, 0, 0); + EXPECT_EQ(src, 42); + EXPECT_EQ(dst, 0); + + CopyPlane(&src, 1, &dst, 1, 1, 0); + EXPECT_EQ(src, 42); + EXPECT_EQ(dst, 0); + + CopyPlane(&src, 1, &dst, 1, 0, 1); + EXPECT_EQ(src, 42); + EXPECT_EQ(dst, 0); +} + TEST_F(LibYUVPlanarTest, TestDetilePlane) { int i, j;