diff mbox series

[FFmpeg-devel,6/8] avcodec/movtextenc: Use av_fast_realloc_array

Message ID DB6PR0101MB2214C6BD3020B002047A3EB88F819@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com
State New
Headers show
Series [FFmpeg-devel,1/8] avutil/mem: Handle fast allocations near UINT_MAX properly | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt July 5, 2022, 8:26 p.m. UTC
It has the advantage of not overallocating beyond the maximum
amount of entries that can be potentially used (namely UINT16_MAX).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/movtextenc.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Tomas Härdin July 6, 2022, 3:06 p.m. UTC | #1
tis 2022-07-05 klockan 22:26 +0200 skrev Andreas Rheinhardt:
> 
> -        if (s->count + 1 > FFMIN(SIZE_MAX / sizeof(*s-
> >style_attributes), UINT16_MAX) ||
> -            !(tmp = av_fast_realloc(s->style_attributes,
> -                                    &s-
> >style_attributes_bytes_allocated,
> -                                    (s->count + 1) * sizeof(*s-
> >style_attributes)))) {
> +        ret = av_fast_realloc_array(&s->style_attributes, &s-
> >style_attributes_allocated,
> +                                    s->count + 1, UINT16_MAX,
> sizeof(*s->style_attributes));

Looks simple enough. Should ever s->count == UINT16_MAX then this will
fail, as it should

/Tomas
diff mbox series

Patch

diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c
index 728338f2cc..fb19b110b4 100644
--- a/libavcodec/movtextenc.c
+++ b/libavcodec/movtextenc.c
@@ -76,7 +76,7 @@  typedef struct {
     ASSStyle *ass_dialog_style;
     StyleBox *style_attributes;
     unsigned  count;
-    unsigned  style_attributes_bytes_allocated;
+    size_t  style_attributes_allocated;
     StyleBox  style_attributes_temp;
     AVBPrint buffer;
     HighlightBox hlit;
@@ -342,6 +342,8 @@  static av_cold int mov_text_encode_init(AVCodecContext *avctx)
 // Start a new style box if needed
 static int mov_text_style_start(MovTextContext *s)
 {
+    int ret;
+
     // there's an existing style entry
     if (s->style_attributes_temp.style_start == s->text_pos)
         // Still at same text pos, use same entry
@@ -353,10 +355,9 @@  static int mov_text_style_start(MovTextContext *s)
         StyleBox *tmp;
 
         // last style != defaults, end the style entry and start a new one
-        if (s->count + 1 > FFMIN(SIZE_MAX / sizeof(*s->style_attributes), UINT16_MAX) ||
-            !(tmp = av_fast_realloc(s->style_attributes,
-                                    &s->style_attributes_bytes_allocated,
-                                    (s->count + 1) * sizeof(*s->style_attributes)))) {
+        ret = av_fast_realloc_array(&s->style_attributes, &s->style_attributes_allocated,
+                                    s->count + 1, UINT16_MAX, sizeof(*s->style_attributes));
+        if (ret < 0) {
             mov_text_cleanup(s);
             av_bprint_clear(&s->buffer);
             s->box_flags &= ~STYL_BOX;