Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold
@ 2023-06-21 20:46 James Almer
  2023-06-21 20:46 ` [FFmpeg-devel] [PATCH 2/3] avcodec: use AV_PKT_MAX_PAYLOAD_SIZE James Almer
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: James Almer @ 2023-06-21 20:46 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
TODO: Version bump and APIchanges entry.

 libavcodec/packet.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libavcodec/packet.h b/libavcodec/packet.h
index f28e7e7011..f7dd687c23 100644
--- a/libavcodec/packet.h
+++ b/libavcodec/packet.h
@@ -418,6 +418,11 @@ typedef struct AVPacket {
     AVRational time_base;
 } AVPacket;
 
+/**
+ * Max size for an AVPacket data buffer.
+ */
+#define AV_PKT_MAX_PAYLOAD_SIZE ((size_t)(INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE))
+
 #if FF_API_INIT_PACKET
 attribute_deprecated
 typedef struct AVPacketList {
-- 
2.41.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [FFmpeg-devel] [PATCH 2/3] avcodec: use AV_PKT_MAX_PAYLOAD_SIZE
  2023-06-21 20:46 [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold James Almer
@ 2023-06-21 20:46 ` James Almer
  2023-06-21 20:46 ` [FFmpeg-devel] [PATCH 3/3] avformat: " James Almer
  2023-06-27  7:23 ` [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold Anton Khirnov
  2 siblings, 0 replies; 5+ messages in thread
From: James Almer @ 2023-06-21 20:46 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/avpacket.c             |  6 +++---
 libavcodec/dvdsub_parser.c        |  2 +-
 libavcodec/encode.c               | 10 +++++-----
 libavcodec/ffv1enc.c              |  4 ++--
 libavcodec/h264_mp4toannexb_bsf.c |  2 +-
 libavcodec/libopenjpegenc.c       |  6 +++---
 6 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index 5fef65e97a..cdf7edc23c 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -82,7 +82,7 @@ void av_packet_free(AVPacket **pkt)
 static int packet_alloc(AVBufferRef **buf, int size)
 {
     int ret;
-    if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+    if (size < 0 || size >= AV_PKT_MAX_PAYLOAD_SIZE)
         return AVERROR(EINVAL);
 
     ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
@@ -120,7 +120,7 @@ void av_shrink_packet(AVPacket *pkt, int size)
 int av_grow_packet(AVPacket *pkt, int grow_by)
 {
     int new_size;
-    av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
+    av_assert0((unsigned)pkt->size <= AV_PKT_MAX_PAYLOAD_SIZE);
     if ((unsigned)grow_by >
         INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
         return AVERROR(ENOMEM);
@@ -170,7 +170,7 @@ int av_grow_packet(AVPacket *pkt, int grow_by)
 
 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
 {
-    if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+    if (size >= AV_PKT_MAX_PAYLOAD_SIZE)
         return AVERROR(EINVAL);
 
     pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
diff --git a/libavcodec/dvdsub_parser.c b/libavcodec/dvdsub_parser.c
index 8871b6a383..1589a543df 100644
--- a/libavcodec/dvdsub_parser.c
+++ b/libavcodec/dvdsub_parser.c
@@ -52,7 +52,7 @@ static int dvdsub_parse(AVCodecParserContext *s,
         if (pc->packet_len == 0) /* HD-DVD subpicture packet */
             pc->packet_len = AV_RB32(buf+2);
         av_freep(&pc->packet);
-        if ((unsigned)pc->packet_len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
+        if ((unsigned)pc->packet_len > AV_PKT_MAX_PAYLOAD_SIZE) {
             av_log(avctx, AV_LOG_ERROR, "packet length %d is invalid\n", pc->packet_len);
             return buf_size;
         }
diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index ab5f889615..7bc14c7fc5 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -34,9 +34,9 @@
 
 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
 {
-    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
-               size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
+    if (size < 0 || size > AV_PKT_MAX_PAYLOAD_SIZE) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %"SIZE_SPECIFIER")\n",
+               size, AV_PKT_MAX_PAYLOAD_SIZE);
         return AVERROR(EINVAL);
     }
 
@@ -58,7 +58,7 @@ int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, in
 {
     int ret;
 
-    if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+    if (avpkt->size < 0 || avpkt->size > AV_PKT_MAX_PAYLOAD_SIZE)
         return AVERROR(EINVAL);
 
     if (avpkt->data || avpkt->buf) {
@@ -80,7 +80,7 @@ int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, i
 {
     int ret;
 
-    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+    if (size < 0 || size > AV_PKT_MAX_PAYLOAD_SIZE)
         return AVERROR(EINVAL);
 
     av_assert0(!avpkt->data && !avpkt->buf);
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 746f717568..b1c9f19e83 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -1152,9 +1152,9 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     if (f->version > 3)
         maxsize = AV_INPUT_BUFFER_MIN_SIZE + avctx->width*avctx->height*3LL*4;
 
-    if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
+    if (maxsize > AV_PKT_MAX_PAYLOAD_SIZE - 32) {
         av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
-        maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
+        maxsize = AV_PKT_MAX_PAYLOAD_SIZE - 32;
     }
 
     if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c
index d11be455c2..20142b90aa 100644
--- a/libavcodec/h264_mp4toannexb_bsf.c
+++ b/libavcodec/h264_mp4toannexb_bsf.c
@@ -269,7 +269,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt)
         } while (buf < buf_end);
 
         if (!j) {
-            if (out_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
+            if (out_size > AV_PKT_MAX_PAYLOAD_SIZE) {
                 ret = AVERROR_INVALIDDATA;
                 goto fail;
             }
diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c
index 009c7a4377..c88c3d0102 100644
--- a/libavcodec/libopenjpegenc.c
+++ b/libavcodec/libopenjpegenc.c
@@ -73,7 +73,7 @@ static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user
     int remaining = packet->size - writer->pos;
     if (nb_bytes > remaining) {
         OPJ_SIZE_T needed = nb_bytes - remaining;
-        int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
+        int max_growth = AV_PKT_MAX_PAYLOAD_SIZE - packet->size;
         if (needed > max_growth) {
             return (OPJ_SIZE_T)-1;
         }
@@ -101,7 +101,7 @@ static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
         int remaining = packet->size - writer->pos;
         if (nb_bytes > remaining) {
             OPJ_SIZE_T needed = nb_bytes - remaining;
-            int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
+            int max_growth = AV_PKT_MAX_PAYLOAD_SIZE - packet->size;
             if (needed > max_growth) {
                 return (OPJ_SIZE_T)-1;
             }
@@ -122,7 +122,7 @@ static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
         return OPJ_FALSE;
     }
     if (nb_bytes > packet->size) {
-        if (nb_bytes > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
+        if (nb_bytes > AV_PKT_MAX_PAYLOAD_SIZE ||
             av_grow_packet(packet, (int)nb_bytes - packet->size)) {
             return OPJ_FALSE;
         }
-- 
2.41.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [FFmpeg-devel] [PATCH 3/3] avformat: use AV_PKT_MAX_PAYLOAD_SIZE
  2023-06-21 20:46 [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold James Almer
  2023-06-21 20:46 ` [FFmpeg-devel] [PATCH 2/3] avcodec: use AV_PKT_MAX_PAYLOAD_SIZE James Almer
@ 2023-06-21 20:46 ` James Almer
  2023-06-27  7:23 ` [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold Anton Khirnov
  2 siblings, 0 replies; 5+ messages in thread
From: James Almer @ 2023-06-21 20:46 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/4xm.c          | 2 +-
 libavformat/flac_picture.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavformat/4xm.c b/libavformat/4xm.c
index fdf6e4b84b..1a35299254 100644
--- a/libavformat/4xm.c
+++ b/libavformat/4xm.c
@@ -328,7 +328,7 @@ static int fourxm_read_packet(AVFormatContext *s,
         case cfr2_TAG:
             /* allocate 8 more bytes than 'size' to account for fourcc
              * and size */
-            if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 8)
+            if (size > AV_PKT_MAX_PAYLOAD_SIZE - 8)
                 return AVERROR_INVALIDDATA;
             if (fourxm->video_stream_index < 0)
                 return AVERROR_INVALIDDATA;
diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c
index b33fee75b4..16a053c6ea 100644
--- a/libavformat/flac_picture.c
+++ b/libavformat/flac_picture.c
@@ -120,7 +120,7 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size,
 
     left = bytestream2_get_bytes_left(&g);
     if (len <= 0 || len > left) {
-        if (len > MAX_TRUNC_PICTURE_SIZE || len >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
+        if (len > MAX_TRUNC_PICTURE_SIZE || len >= AV_PKT_MAX_PAYLOAD_SIZE) {
             av_log(s, AV_LOG_ERROR, "Attached picture metadata block too big %u\n", len);
             if (s->error_recognition & AV_EF_EXPLODE)
                 return AVERROR_INVALIDDATA;
-- 
2.41.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold
  2023-06-21 20:46 [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold James Almer
  2023-06-21 20:46 ` [FFmpeg-devel] [PATCH 2/3] avcodec: use AV_PKT_MAX_PAYLOAD_SIZE James Almer
  2023-06-21 20:46 ` [FFmpeg-devel] [PATCH 3/3] avformat: " James Almer
@ 2023-06-27  7:23 ` Anton Khirnov
  2023-06-27 14:39   ` James Almer
  2 siblings, 1 reply; 5+ messages in thread
From: Anton Khirnov @ 2023-06-27  7:23 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Quoting James Almer (2023-06-21 22:46:56)
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> TODO: Version bump and APIchanges entry.
> 
>  libavcodec/packet.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/libavcodec/packet.h b/libavcodec/packet.h
> index f28e7e7011..f7dd687c23 100644
> --- a/libavcodec/packet.h
> +++ b/libavcodec/packet.h
> @@ -418,6 +418,11 @@ typedef struct AVPacket {
>      AVRational time_base;
>  } AVPacket;
>  
> +/**
> + * Max size for an AVPacket data buffer.
> + */
> +#define AV_PKT_MAX_PAYLOAD_SIZE ((size_t)(INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE))

Maybe just AV_PKT_SIZE_MAX to be consistent with other FOO_MAX in C?

Otherwise the set seems like a good idea.

-- 
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold
  2023-06-27  7:23 ` [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold Anton Khirnov
@ 2023-06-27 14:39   ` James Almer
  0 siblings, 0 replies; 5+ messages in thread
From: James Almer @ 2023-06-27 14:39 UTC (permalink / raw)
  To: ffmpeg-devel

On 6/27/2023 4:23 AM, Anton Khirnov wrote:
> Quoting James Almer (2023-06-21 22:46:56)
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>> TODO: Version bump and APIchanges entry.
>>
>>   libavcodec/packet.h | 5 +++++
>>   1 file changed, 5 insertions(+)
>>
>> diff --git a/libavcodec/packet.h b/libavcodec/packet.h
>> index f28e7e7011..f7dd687c23 100644
>> --- a/libavcodec/packet.h
>> +++ b/libavcodec/packet.h
>> @@ -418,6 +418,11 @@ typedef struct AVPacket {
>>       AVRational time_base;
>>   } AVPacket;
>>   
>> +/**
>> + * Max size for an AVPacket data buffer.
>> + */
>> +#define AV_PKT_MAX_PAYLOAD_SIZE ((size_t)(INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE))
> 
> Maybe just AV_PKT_SIZE_MAX to be consistent with other FOO_MAX in C?
> 
> Otherwise the set seems like a good idea.

Will apply with that change then. Thanks.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-06-27 14:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-21 20:46 [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold James Almer
2023-06-21 20:46 ` [FFmpeg-devel] [PATCH 2/3] avcodec: use AV_PKT_MAX_PAYLOAD_SIZE James Almer
2023-06-21 20:46 ` [FFmpeg-devel] [PATCH 3/3] avformat: " James Almer
2023-06-27  7:23 ` [FFmpeg-devel] [PATCH 1/3] avcodec/packet: add a define for the max buffer size a packet can hold Anton Khirnov
2023-06-27 14:39   ` James Almer

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git