PR #26349 opened by Forgejo_Fairy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26349
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26349.patch

Fixes #26292.

A crop can advance `frame->data[0]` without changing its linesize. Passing 
`linesize * height` to `JxlEncoderAddImageFrame()` then includes padding beyond 
the final active row, which may lie outside the allocation. libjxl 0.11.1 
copies the supplied span and exposes this as an invalid read.

Compute the required span through the active pixels of the final row in the 
shared frame-preprocessing path. Both still and animated encoders use it. 
Reject invalid strides and size overflow.

Verification: on arm64 with libjxl 0.11.1, Valgrind reports an invalid read on 
unpatched master for `testsrc=size=1120x256:rate=1,crop=320:256:800:0` in both 
encoder paths. The patched build passes those runs and a cropped `vflip` run. 
Tested still and animated outputs match the unpatched outputs byte for byte. 
The reporter’s command also encodes with libjxl 0.7.0.


From a4c90e451d6c60973d9aec89e39d9704568ff856 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 25 Nov 2026 09:37:55 +0000
Subject: [PATCH] avcodec/libjxlenc: pass the actual cropped frame buffer size

A cropped frame can start inside its backing allocation while retaining the 
original stride. Passing stride times height makes libjxl copy beyond the 
allocation when the final row does not contain a full stride. Pass the required 
span through the active pixels of the final row for still and animated frames.

Assisted-by: Fairy
---
 libavcodec/libjxlenc.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index e5b09c0815e..da1b7d7f6ca 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -32,6 +32,7 @@
 #include "libavutil/display.h"
 #include "libavutil/error.h"
 #include "libavutil/frame.h"
+#include "libavutil/imgutils.h"
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
@@ -552,10 +553,12 @@ end:
  * instead. *data is just a pointer to a location in frame->data so it should 
not be
  * freed directly.
  */
-static int libjxl_preprocess_frame(AVCodecContext *avctx, const AVFrame 
*frame, const uint8_t **data)
+static int libjxl_preprocess_frame(AVCodecContext *avctx, const AVFrame *frame,
+                                   const uint8_t **data, size_t *size)
 {
     LibJxlEncodeContext *ctx = avctx->priv_data;
     JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
+    int row_size, ret;
 
     /* these shouldn't fail, libjxl bug notwithstanding */
     if (JxlEncoderFrameSettingsSetOption(ctx->options, 
JXL_ENC_FRAME_SETTING_EFFORT, ctx->effort)
@@ -580,6 +583,12 @@ static int libjxl_preprocess_frame(AVCodecContext *avctx, 
const AVFrame *frame,
         return AVERROR_EXTERNAL;
     }
 
+    row_size = av_image_get_linesize(frame->format, frame->width, 0);
+    if (row_size < 0)
+        return row_size;
+    if (frame->height <= 0 || frame->linesize[0] == INT_MIN)
+        return AVERROR(EINVAL);
+
     jxl_fmt->endianness = JXL_NATIVE_ENDIAN;
     if (frame->linesize[0] >= 0) {
         jxl_fmt->align = frame->linesize[0];
@@ -588,6 +597,17 @@ static int libjxl_preprocess_frame(AVCodecContext *avctx, 
const AVFrame *frame,
         jxl_fmt->align = -frame->linesize[0];
         *data = frame->data[0] + frame->linesize[0] * (frame->height - 1);
     }
+    if (jxl_fmt->align < row_size)
+        return AVERROR(EINVAL);
+
+    /* libjxl needs only the active pixels of the final row. A cropped frame
+     * may not have a full stride of allocated memory after that row. */
+    ret = av_size_mult(jxl_fmt->align, frame->height - 1, size);
+    if (ret < 0)
+        return ret;
+    if (*size > SIZE_MAX - row_size)
+        return AVERROR(EINVAL);
+    *size += row_size;
 
     return 0;
 }
@@ -646,7 +666,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 
     LibJxlEncodeContext *ctx = avctx->priv_data;
     int ret;
-    size_t bytes_written = 0;
+    size_t bytes_written = 0, input_size;
     const uint8_t *data;
 
     ret = libjxl_init_jxl_encoder(avctx);
@@ -659,11 +679,11 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
     if (ret < 0)
         return ret;
 
-    ret = libjxl_preprocess_frame(avctx, frame, &data);
+    ret = libjxl_preprocess_frame(avctx, frame, &data, &input_size);
     if (ret < 0)
         return ret;
 
-    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, 
ctx->jxl_fmt.align * frame->height)
+    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, input_size)
             != JXL_ENC_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n");
         return AVERROR_EXTERNAL;
@@ -703,7 +723,7 @@ static int libjxl_anim_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt)
     LibJxlEncodeContext *ctx = avctx->priv_data;
     int ret = 0;
     JxlFrameHeader frame_header;
-    size_t bytes_written = 0;
+    size_t bytes_written = 0, input_size;
     const uint8_t *data;
 
     if (!ctx->prev) {
@@ -743,11 +763,11 @@ static int libjxl_anim_encode_frame(AVCodecContext 
*avctx, AVPacket *pkt)
         return AVERROR_EXTERNAL;
     }
 
-    ret = libjxl_preprocess_frame(avctx, ctx->prev, &data);
+    ret = libjxl_preprocess_frame(avctx, ctx->prev, &data, &input_size);
     if (ret < 0)
         return ret;
 
-    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, 
ctx->jxl_fmt.align * ctx->prev->height)
+    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, input_size)
             != JXL_ENC_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n");
         return AVERROR_EXTERNAL;
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to