Fix use-after-free of padded grid cells with Sample Transforms (#3419)

avifEncoderAddImageInternal() pads a smaller right-most or bottom-most
grid cell into cellImagePlaceholder and points cellImage at it. With a
Sample Transform recipe, the placeholder was destroyed before
avifEncoderCreateBitDepthExtensionImage() read cellImage. Create the
bit depth extension image first, then destroy the padded copy.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f5ad788..fd1b203 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -63,6 +63,8 @@
 * Fix a crash in avifDecoderReset() when a progressive AVIF is parsed with
   colour and then reset with colour excluded: only inspect the first colour
   tile if colour tiles exist.
+* Fix a use-after-free in avifEncoderAddImageGrid() with a Sample Transform
+  recipe when the right-most or bottom-most cells are smaller than the others.
 
 ## [1.4.2] - 2026-05-26
 
diff --git a/src/write.c b/src/write.c
index 7843e8b..711ba07 100644
--- a/src/write.c
+++ b/src/write.c
@@ -2295,13 +2295,13 @@
                 // Replace cellImage by the first or second input to the AVIF_ITEM_SAMPLE_TRANSFORM derived image item.
                 const avifBool itemWillBeEncodedLosslessly = (quality == AVIF_QUALITY_LOSSLESS);
                 avifImage * sampleTransformedImage = NULL;
+                // cellImage may point to cellImagePlaceholder (padded cell), so only destroy it once it was read.
+                const avifResult sampleTransformResult =
+                    avifEncoderCreateBitDepthExtensionImage(encoder, item, itemWillBeEncodedLosslessly, cellImage, &sampleTransformedImage);
                 if (cellImagePlaceholder) {
                     avifImageDestroy(cellImagePlaceholder); // Replaced by sampleTransformedImage.
-                    cellImagePlaceholder = NULL;
                 }
-                AVIF_CHECKRES(
-                    avifEncoderCreateBitDepthExtensionImage(encoder, item, itemWillBeEncodedLosslessly, cellImage, &sampleTransformedImage));
-                assert(cellImagePlaceholder == NULL);
+                AVIF_CHECKRES(sampleTransformResult);
                 cellImagePlaceholder = sampleTransformedImage; // Transfer ownership.
                 cellImage = cellImagePlaceholder;
             }
diff --git a/tests/gtest/avif16bittest.cc b/tests/gtest/avif16bittest.cc
index 48d5a54..3453eb5 100644
--- a/tests/gtest/avif16bittest.cc
+++ b/tests/gtest/avif16bittest.cc
@@ -4,6 +4,7 @@
 #include <cstring>
 #include <filesystem>
 #include <fstream>
+#include <vector>
 
 #include "avif/avif.h"
 #include "avif/avif_cxx.h"
@@ -456,6 +457,78 @@
 
 //------------------------------------------------------------------------------
 
+class SampleTransformGridTest
+    : public testing::TestWithParam<avifSampleTransformRecipe> {};
+
+TEST_P(SampleTransformGridTest, SmallerLastCells) {
+  const avifSampleTransformRecipe recipe = GetParam();
+
+  const ImagePtr image =
+      testutil::ReadImage(data_path, "weld_16bit.png", AVIF_PIXEL_FORMAT_YUV444,
+                          /*requested_depth=*/16);
+  ASSERT_NE(image, nullptr);
+  // Speed test up.
+  image->width = 100;
+  image->height = 100;
+  // Simulate alpha plane with a view on luma.
+  image->alphaPlane = image->yuvPlanes[AVIF_CHAN_Y];
+  image->alphaRowBytes = image->yuvRowBytes[AVIF_CHAN_Y];
+  image->imageOwnsAlphaPlane = false;
+
+  // The right-most column and the bottom-most row are smaller than the other
+  // cells, so they are padded by the encoder.
+  const uint32_t kGridCols = 2, kGridRows = 2;
+  const uint32_t cell_width = 64, cell_height = 64;
+  std::vector<ImagePtr> cells;
+  std::vector<const avifImage*> cell_ptrs;
+  for (uint32_t row = 0; row < kGridRows; ++row) {
+    for (uint32_t col = 0; col < kGridCols; ++col) {
+      const avifCropRect rect{
+          col * cell_width, row * cell_height,
+          col + 1 < kGridCols ? cell_width : image->width - col * cell_width,
+          row + 1 < kGridRows ? cell_height
+                              : image->height - row * cell_height};
+      cells.emplace_back(avifImageCreateEmpty());
+      ASSERT_NE(cells.back(), nullptr);
+      ASSERT_EQ(avifImageSetViewRect(cells.back().get(), image.get(), &rect),
+                AVIF_RESULT_OK);
+      cell_ptrs.push_back(cells.back().get());
+    }
+  }
+
+  EncoderPtr encoder(avifEncoderCreate());
+  ASSERT_NE(encoder, nullptr);
+  encoder->speed = AVIF_SPEED_FASTEST;
+  encoder->quality = AVIF_QUALITY_LOSSLESS;
+  encoder->qualityAlpha = AVIF_QUALITY_LOSSLESS;
+  encoder->sampleTransformRecipe = recipe;
+  testutil::AvifRwData encoded;
+  ASSERT_EQ(
+      avifEncoderAddImageGrid(encoder.get(), kGridCols, kGridRows,
+                              cell_ptrs.data(), AVIF_ADD_IMAGE_FLAG_SINGLE),
+      AVIF_RESULT_OK);
+  ASSERT_EQ(avifEncoderFinish(encoder.get(), &encoded), AVIF_RESULT_OK);
+
+  ImagePtr decoded(avifImageCreateEmpty());
+  ASSERT_NE(decoded, nullptr);
+  DecoderPtr decoder(avifDecoderCreate());
+  ASSERT_NE(decoder, nullptr);
+  decoder->imageContentToDecode |= AVIF_IMAGE_CONTENT_SAMPLE_TRANSFORMS;
+  ASSERT_EQ(avifDecoderReadMemory(decoder.get(), decoded.get(), encoded.data,
+                                  encoded.size),
+            AVIF_RESULT_OK);
+  EXPECT_TRUE(testutil::AreImagesEqual(*image, *decoded));
+}
+
+INSTANTIATE_TEST_SUITE_P(
+    Recipes, SampleTransformGridTest,
+    testing::Values(
+        AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_8B_8B,
+        AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_4B,
+        AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_8B_OVERLAP_4B));
+
+//------------------------------------------------------------------------------
+
 }  // namespace
 }  // namespace avif