[PR] avformat/whip: Avoid allocation (PR #24152)

mkver via ffmpeg-devel <[email protected]>
Newsgroups gmane.comp.video.ffmpeg.devel
Message-ID <178674345120.59.11865771524818657065@29965ddac10e>
PR #24152 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24152
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24152.patch


>From 08d6b10bdf912f366932a72eb37ce929cabca77d Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Fri, 14 Aug 2026 22:40:52 +0200
Subject: [PATCH 1/2] avformat/whip: Avoid calling strlen multiple times

Also avoid zeroing the buffer unnecessarily, ff_data_to_hex()
also initialized it (including the terminating \0).

Signed-off-by: Andreas Rheinhardt <[email protected]>
---
 libavformat/whip.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/libavformat/whip.c b/libavformat/whip.c
index ae2c116a4d..a845f0c37c 100644
--- a/libavformat/whip.c
+++ b/libavformat/whip.c
@@ -791,7 +791,7 @@ static int exchange_sdp(AVFormatContext *s)
         goto end;
     }
 
-    if (!whip->sdp_offer || !strlen(whip->sdp_offer)) {
+    if (!whip->sdp_offer || !*whip->sdp_offer) {
         av_log(whip, AV_LOG_ERROR, "No offer to exchange\n");
         ret = AVERROR(EINVAL);
         goto end;
@@ -812,12 +812,13 @@ static int exchange_sdp(AVFormatContext *s)
     if (whip->timeout >= 0)
         av_dict_set_int(&opts, "timeout", whip->timeout, 0);
 
-    hex_data = av_mallocz(2 * strlen(whip->sdp_offer) + 1);
+    const size_t sdp_offer_len = strlen(whip->sdp_offer);
+    hex_data = av_malloc(2 * sdp_offer_len + 1);
     if (!hex_data) {
         ret = AVERROR(ENOMEM);
         goto end;
     }
-    ff_data_to_hex(hex_data, whip->sdp_offer, strlen(whip->sdp_offer), 0);
+    ff_data_to_hex(hex_data, whip->sdp_offer, sdp_offer_len, 0);
     av_dict_set(&opts, "post_data", hex_data, 0);
 
     ret = ffurl_open_whitelist(&whip_uc, s->url, AVIO_FLAG_READ_WRITE, &s->interrupt_callback,
-- 
2.52.0


>From 6d2344e78067f6f842bfed1ddd39239484254557 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <[email protected]>
Date: Fri, 14 Aug 2026 22:48:13 +0200
Subject: [PATCH 2/2] avformat/whip: Avoid allocation

Signed-off-by: Andreas Rheinhardt <[email protected]>
---
 libavformat/whip.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libavformat/whip.c b/libavformat/whip.c
index a845f0c37c..cea9941878 100644
--- a/libavformat/whip.c
+++ b/libavformat/whip.c
@@ -819,7 +819,7 @@ static int exchange_sdp(AVFormatContext *s)
         goto end;
     }
     ff_data_to_hex(hex_data, whip->sdp_offer, sdp_offer_len, 0);
-    av_dict_set(&opts, "post_data", hex_data, 0);
+    av_dict_set(&opts, "post_data", hex_data, AV_DICT_DONT_STRDUP_VAL);
 
     ret = ffurl_open_whitelist(&whip_uc, s->url, AVIO_FLAG_READ_WRITE, &s->interrupt_callback,
         &opts, s->protocol_whitelist, s->protocol_blacklist, NULL);
@@ -877,7 +877,6 @@ end:
     ffurl_closep(&whip_uc);
     av_bprint_finalize(&bp, NULL);
     av_dict_free(&opts);
-    av_freep(&hex_data);
     return ret;
 }
 
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.