Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Yogeshwar Velingker via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Yogeshwar Velingker <yogi@velingker.com>
Subject: [FFmpeg-devel] [PATCH 2/2] avfilter/drawtext: fix memory bugs
Date: Sun,  2 Mar 2025 20:48:09 -0600
Message-ID: <20250303024809.1661352-2-yogi@velingker.com> (raw)
In-Reply-To: <20250303024809.1661352-1-yogi@velingker.com>

Check for malloc failures, and fix error paths that leak memory.

Signed-off-by: Yogeshwar Velingker <yogi@velingker.com>
---
 libavfilter/vf_drawtext.c | 47 +++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 17 deletions(-)

diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 8bf5a3cd49..62aeb272f3 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -1447,7 +1447,16 @@ continue_on_failed:
 
     s->line_count = line_count;
     s->lines = av_mallocz(line_count * sizeof(TextLine));
+    if (!s->lines) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+
     s->tab_clusters = av_mallocz(s->tab_count * sizeof(uint32_t));
+    if (!s->tab_clusters) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
     for (i = 0; i < s->tab_count; ++i) {
         s->tab_clusters[i] = -1;
     }
@@ -1732,6 +1741,10 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame)
         TextLine *line = &s->lines[l];
         HarfbuzzData *hb = &line->hb_data;
         line->glyphs = av_mallocz(hb->glyph_count * sizeof(GlyphInfo));
+        if (!line->glyphs) {
+            ret = AVERROR(ENOMEM);
+            goto done;
+        }
 
         for (int t = 0; t < hb->glyph_count; ++t) {
             GlyphInfo *g_info = &line->glyphs[t];
@@ -1747,9 +1760,9 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame)
             shift_y64 = ((4 - (((y64 + true_y) >> 4) & 0b0011)) & 0b0011) << 4;
 
             ret = load_glyph(ctx, &glyph, hb->glyph_info[t].codepoint, shift_x64, shift_y64);
-            if (ret != 0) {
-                return ret;
-            }
+            if (ret != 0)
+                goto done;
+
             g_info->code = hb->glyph_info[t].codepoint;
             g_info->x = (x64 + true_x) >> 6;
             g_info->y = ((y64 + true_y) >> 6) + (shift_y64 > 0 ? 1 : 0);
@@ -1809,31 +1822,31 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame)
 
         if (s->shadowx || s->shadowy) {
             if ((ret = draw_glyphs(s, frame, &shadowcolor, &metrics,
-                    s->shadowx, s->shadowy, s->borderw)) < 0) {
-                return ret;
-            }
+                    s->shadowx, s->shadowy, s->borderw)) < 0)
+                goto done;
         }
 
         if (s->borderw) {
             if ((ret = draw_glyphs(s, frame, &bordercolor, &metrics,
-                    0, 0, s->borderw)) < 0) {
-                return ret;
-            }
+                    0, 0, s->borderw)) < 0)
+                goto done;
         }
 
         if ((ret = draw_glyphs(s, frame, &fontcolor, &metrics, 0,
-                0, 0)) < 0) {
-            return ret;
-        }
+                0, 0)) < 0)
+            goto done;
     }
 
+done:
     // FREE data structures
-    for (int l = 0; l < s->line_count; ++l) {
-        TextLine *line = &s->lines[l];
-        av_freep(&line->glyphs);
-        hb_destroy(&line->hb_data);
+    if (s->lines) {
+        for (int l = 0; l < s->line_count; ++l) {
+            TextLine *line = &s->lines[l];
+            av_freep(&line->glyphs);
+            hb_destroy(&line->hb_data);
+        }
+        av_freep(&s->lines);
     }
-    av_freep(&s->lines);
     av_freep(&s->tab_clusters);
 
     return 0;
-- 
2.47.2

_______________________________________________
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".

  reply	other threads:[~2025-03-03  2:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-03  2:48 [FFmpeg-devel] [PATCH 1/2] avfilter/drawtext: support bitmap (including monochrome) fonts Yogeshwar Velingker via ffmpeg-devel
2025-03-03  2:48 ` Yogeshwar Velingker via ffmpeg-devel [this message]
2025-03-03  3:37 ` [FFmpeg-devel] [PATCH v2 " Yogeshwar Velingker via ffmpeg-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250303024809.1661352-2-yogi@velingker.com \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=yogi@velingker.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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