Skip to content

Commit

Permalink
utils/sdp: improved string handling
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
MartinPulec committed Feb 5, 2024
1 parent 6b03287 commit 772ef5d
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/utils/sdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 772ef5d

Please sign in to comment.