Skip to content

Commit

Permalink
Convert strlen calls to strnlen, to optimize string length checks (#3061
Browse files Browse the repository at this point in the history
)

(cherry-picked from 11247a6)
  • Loading branch information
tmatth committed Sep 9, 2022
1 parent 8429630 commit 739c6c8
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion events/janus_wsevh.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ int janus_wsevh_init(const char *config_path) {
goto error;
}
path[0] = '/';
if(strlen(p) > 1)
if(strnlen(p, 1 + 1) > 1)
g_strlcpy(path + 1, p, sizeof(path)-2);
/* Before connecting, let's check if the server expects a subprotocol */
item = janus_config_get(config, config_general, janus_config_type_item, "subprotocol");
Expand Down
2 changes: 1 addition & 1 deletion plugins/janus_sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -5540,7 +5540,7 @@ void janus_sip_sofia_callback(nua_event_t event, int status, char const *phrase,
break;
}
const char *from_value = (const char *)from->t_value;
if(from_value == NULL || strlen(from_value) < 2) {
if(from_value == NULL || strnlen(from_value, 2) < 2) {
JANUS_LOG(LOG_WARN, "Invalid 'siptag_from_str' value '%s'\n", from_value);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/janus_streaming.c
Original file line number Diff line number Diff line change
Expand Up @@ -6731,7 +6731,7 @@ static int janus_streaming_rtsp_connect_to_server(janus_streaming_mountpoint *mp
index++;
continue;
}
if(strlen(line) < 3) {
if(strnlen(line, 3) < 3) {
JANUS_LOG(LOG_ERR, "Invalid RTSP line (%zu bytes): %s\n", strlen(line), line);
success = FALSE;
break;
Expand Down Expand Up @@ -6909,7 +6909,7 @@ static int janus_streaming_rtsp_connect_to_server(janus_streaming_mountpoint *mp
index++;
continue;
}
if(strlen(line) < 3) {
if(strnlen(line, 3) < 3) {
JANUS_LOG(LOG_ERR, "Invalid RTSP line (%zu bytes): %s\n", strlen(line), line);
success = FALSE;
break;
Expand Down
2 changes: 1 addition & 1 deletion plugins/janus_videoroom.c
Original file line number Diff line number Diff line change
Expand Up @@ -8023,7 +8023,7 @@ static void *janus_videoroom_handler(void *data) {
}
}
char *tmp = strchr(a->value, ' ');
if(tmp && strlen(tmp) > 1) {
if(tmp && strnlen(tmp, 1 + 1) > 1) {
tmp++;
g_free(audio_fmtp);
audio_fmtp = g_strdup(tmp);
Expand Down
2 changes: 1 addition & 1 deletion postprocessing/janus-pp-rec.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static char *janus_pp_extensions_string(const char **allowed, char *supported, s
janus_strlcat(supported, "[", suplen);
const char **ext = allowed;
while(*ext != NULL) {
if(strlen(supported) > 1)
if(strnlen(supported, 1 + 1) > 1)
janus_strlcat(supported, ", ", suplen);
janus_strlcat(supported, *ext, suplen);
ext++;
Expand Down
4 changes: 2 additions & 2 deletions sdp-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ janus_sdp *janus_sdp_parse(const char *sdp, char *error, size_t errlen) {
index++;
continue;
}
if(strlen(line) < 3) {
if(strnlen(line, 3) < 3) {
if(error)
g_snprintf(error, errlen, "Invalid line (%zu bytes): %s", strlen(line), line);
success = FALSE;
Expand Down Expand Up @@ -411,7 +411,7 @@ janus_sdp *janus_sdp_parse(const char *sdp, char *error, size_t errlen) {
/* Start with media type, port and protocol */
char type[32];
char proto[64];
if(strlen(line) > 200) {
if(strnlen(line, 200 + 1) > 200) {
janus_sdp_mline_destroy(m);
if(error)
g_snprintf(error, errlen, "Invalid m= line (too long): %zu", strlen(line));
Expand Down
8 changes: 4 additions & 4 deletions sdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ janus_sdp *janus_sdp_preparse(void *ice_handle, const char *jsep_sdp, char *erro
}
if(m->type == JANUS_SDP_AUDIO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Audio mid: %s\n", handle->handle_id, a->value);
if(strlen(a->value) > 16) {
if(strnlen(a->value, 16 + 1) > 16) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Audio mid too large: (%zu > 16)\n", handle->handle_id, strlen(a->value));
janus_sdp_destroy(parsed_sdp);
return NULL;
Expand All @@ -108,7 +108,7 @@ janus_sdp *janus_sdp_preparse(void *ice_handle, const char *jsep_sdp, char *erro
handle->stream_mid = handle->audio_mid;
} else if(m->type == JANUS_SDP_VIDEO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Video mid: %s\n", handle->handle_id, a->value);
if(strlen(a->value) > 16) {
if(strnlen(a->value, 16 + 1) > 16) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Video mid too large: (%zu > 16)\n", handle->handle_id, strlen(a->value));
janus_sdp_destroy(parsed_sdp);
return NULL;
Expand Down Expand Up @@ -363,7 +363,7 @@ int janus_sdp_process(void *ice_handle, janus_sdp *remote_sdp, gboolean rids_hml
/* Found mid attribute */
if(m->type == JANUS_SDP_AUDIO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Audio mid: %s\n", handle->handle_id, a->value);
if(strlen(a->value) > 16) {
if(strnlen(a->value, 16 + 1) > 16) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Audio mid too large: (%zu > 16)\n", handle->handle_id, strlen(a->value));
return -2;
}
Expand All @@ -373,7 +373,7 @@ int janus_sdp_process(void *ice_handle, janus_sdp *remote_sdp, gboolean rids_hml
handle->stream_mid = handle->audio_mid;
} else if(m->type == JANUS_SDP_VIDEO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Video mid: %s\n", handle->handle_id, a->value);
if(strlen(a->value) > 16) {
if(strnlen(a->value, 16 + 1) > 16) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Video mid too large: (%zu > 16)\n", handle->handle_id, strlen(a->value));
return -2;
}
Expand Down
8 changes: 4 additions & 4 deletions transports/janus_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ int janus_http_init(janus_transport_callbacks *callback, const char *config_path
return -1;
}
ws_path = g_strdup(item->value);
if(strlen(ws_path) > 1 && ws_path[strlen(ws_path)-1] == '/') {
if(strnlen(ws_path, 1 + 1) > 1 && ws_path[strlen(ws_path)-1] == '/') {
/* Remove the trailing slash, it makes things harder when we parse requests later */
ws_path[strlen(ws_path)-1] = '\0';
}
Expand All @@ -700,7 +700,7 @@ int janus_http_init(janus_transport_callbacks *callback, const char *config_path
return -1;
}
admin_ws_path = g_strdup(item->value);
if(strlen(admin_ws_path) > 1 && admin_ws_path[strlen(admin_ws_path)-1] == '/') {
if(strnlen(admin_ws_path, 1 + 1) > 1 && admin_ws_path[strlen(admin_ws_path)-1] == '/') {
/* Remove the trailing slash, it makes things harder when we parse requests later */
admin_ws_path[strlen(admin_ws_path)-1] = '\0';
}
Expand Down Expand Up @@ -1423,7 +1423,7 @@ static MHD_Result janus_http_handler(void *cls, struct MHD_Connection *connectio
}
/* Get path components */
if(strcasecmp(url, ws_path)) {
if(strlen(ws_path) > 1) {
if(strnlen(ws_path, 1 + 1) > 1) {
basepath = g_strsplit(url, ws_path, -1);
} else {
/* The base path is the web server too itself, we process the url itself */
Expand Down Expand Up @@ -1821,7 +1821,7 @@ static MHD_Result janus_http_admin_handler(void *cls, struct MHD_Connection *con
}
/* Get path components */
if(strcasecmp(url, admin_ws_path)) {
if(strlen(admin_ws_path) > 1) {
if(strnlen(admin_ws_path, 1 + 1) > 1) {
basepath = g_strsplit(url, admin_ws_path, -1);
} else {
/* The base path is the web server too itself, we process the url itself */
Expand Down
2 changes: 1 addition & 1 deletion transports/janus_pfunix.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static int janus_pfunix_create_socket(char *pfname, gboolean use_dgram) {
if(pfname == NULL)
return -1;
int fd = -1;
if(strlen(pfname) > UNIX_PATH_MAX) {
if(strnlen(pfname, UNIX_PATH_MAX + 1) > UNIX_PATH_MAX) {
JANUS_LOG(LOG_WARN, "The provided path name (%s) is longer than %lu characters, it will be truncated\n", pfname, UNIX_PATH_MAX);
pfname[UNIX_PATH_MAX] = '\0';
}
Expand Down

0 comments on commit 739c6c8

Please sign in to comment.