From 772ef5de19c8dff02da21dad2b9eba04656e2b16 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 5 Feb 2024 16:42:28 +0100 Subject: [PATCH] utils/sdp: improved string handling - use snprintf instead of strncat (better behavior - always appends \0 and no need to subtract 1 from len /and expecting that there is \0 at the end) - use actual string length with sizeof than STR_LEN (which may get removed because it is misleading in respect to global STR_LENi value) --- src/utils/sdp.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/utils/sdp.c b/src/utils/sdp.c index 360fc658f..035d32a7f 100644 --- a/src/utils/sdp.c +++ b/src/utils/sdp.c @@ -155,13 +155,14 @@ static struct sdp *new_sdp(bool ipv6, const char *receiver) { if (is_addr_multicast(receiver)) { connection_address = receiver; } - strncpy(sdp->version, "v=0\r\n", STR_LENGTH - 1); - snprintf(sdp->origin, STR_LENGTH, "o=- 0 0 IN IP%d %s\r\n", sdp->ip_version, - origin_address); - strncpy(sdp->session_name, "s=Ultragrid streams\r\n", STR_LENGTH - 1); - snprintf(sdp->connection, STR_LENGTH, "c=IN IP%d %s\r\n", sdp->ip_version, - connection_address); - strncpy(sdp->times, "t=0 0\r\n", STR_LENGTH - 1); + snprintf(sdp->version, sizeof sdp->version, "v=0\r\n"); + snprintf(sdp->origin, sizeof sdp->origin, "o=- 0 0 IN IP%d %s\r\n", + sdp->ip_version, origin_address); + snprintf(sdp->session_name, sizeof sdp->session_name, + "s=Ultragrid streams\r\n"); + snprintf(sdp->connection, sizeof sdp->connection, "c=IN IP%d %s\r\n", + sdp->ip_version, connection_address); + snprintf(sdp->times, sizeof sdp->times, "t=0 0\r\n"); #ifdef SDP_HTTP serverInit(&sdp->http_server); @@ -303,11 +304,13 @@ int sdp_add_video(bool ipv6, int port, codec_t codec, address_callback_t addr_ca if (index < 0) { return -1; } - snprintf(sdp_state->stream[index].media_info, STR_LENGTH, + snprintf(sdp_state->stream[index].media_info, + sizeof sdp_state->stream[index].media_info, "m=video %d RTP/AVP %d\r\n", port, codec == H264 ? PT_DynRTP_Type96 : PT_JPEG); if (codec == H264) { - snprintf(sdp_state->stream[index].rtpmap, STR_LENGTH, + snprintf(sdp_state->stream[index].rtpmap, + sizeof sdp_state->stream[index].rtpmap, "a=rtpmap:%d H264/90000\r\n", PT_DynRTP_Type96); }