Skip to content

Commit

Permalink
Remove unneeded allocations checking
Browse files Browse the repository at this point in the history
Since commit 1f06765 ("Converted memory allocations to GLib ones, and
fixed a couple of leaks"), allocations are done with g_malloc() and
others. According to GLib documentation, these functions always return a
valid pointer, aborting the program when memory could not be allocated.
This effectively makes the checks useless, so remove them.
  • Loading branch information
Douglas Caetano dos Santos committed Jan 23, 2018
1 parent 9a9a14e commit 1875faf
Show file tree
Hide file tree
Showing 16 changed files with 8 additions and 165 deletions.
12 changes: 0 additions & 12 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,6 @@ janus_config *janus_config_parse(const char *config_file) {

janus_config *janus_config_create(const char *name) {
janus_config *jc = g_malloc0(sizeof(janus_config));
if(jc == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
if(name != NULL) {
jc->name = g_strdup(name);
}
Expand Down Expand Up @@ -258,10 +254,6 @@ janus_config_category *janus_config_add_category(janus_config *config, const cha
return c;
}
c = g_malloc0(sizeof(janus_config_category));
if(c == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
c->name = g_strdup(category);
config->categories = g_list_append(config->categories, c);
return c;
Expand Down Expand Up @@ -293,10 +285,6 @@ janus_config_item *janus_config_add_item(janus_config *config, const char *categ
if(item == NULL) {
/* Create it */
item = g_malloc0(sizeof(janus_config_item));
if(item == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
item->name = g_strdup(name);
item->value = g_strdup(value);
if(c != NULL) {
Expand Down
4 changes: 0 additions & 4 deletions dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,6 @@ janus_dtls_srtp *janus_dtls_srtp_create(void *ice_component, janus_dtls_role rol
return NULL;
}
janus_dtls_srtp *dtls = g_malloc0(sizeof(janus_dtls_srtp));
if(dtls == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
/* Create SSL context, at last */
dtls->srtp_valid = 0;
dtls->ssl = SSL_new(ssl_ctx);
Expand Down
8 changes: 0 additions & 8 deletions ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,6 @@ janus_ice_handle *janus_ice_handle_create(void *gateway_session, const char *opa
}
JANUS_LOG(LOG_INFO, "Creating new handle in session %"SCNu64": %"SCNu64"\n", session->session_id, handle_id);
janus_ice_handle *handle = (janus_ice_handle *)g_malloc0(sizeof(janus_ice_handle));
if(handle == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
handle->session = gateway_session;
if(opaque_id)
handle->opaque_id = g_strdup(opaque_id);
Expand Down Expand Up @@ -958,10 +954,6 @@ gint janus_ice_handle_attach_plugin(void *gateway_session, guint64 handle_id, ja
}
int error = 0;
janus_plugin_session *session_handle = g_malloc(sizeof(janus_plugin_session));
if(session_handle == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return JANUS_ERROR_UNKNOWN; /* FIXME Do we need something like "Internal Server Error"? */
}
session_handle->gateway_handle = handle;
session_handle->plugin_handle = NULL;
session_handle->stopped = 0;
Expand Down
4 changes: 0 additions & 4 deletions janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,6 @@ janus_session *janus_session_create(guint64 session_id) {
}
JANUS_LOG(LOG_INFO, "Creating new session: %"SCNu64"\n", session_id);
janus_session *session = (janus_session *)g_malloc(sizeof(janus_session));
if(session == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
session->session_id = session_id;
session->source = NULL;
g_atomic_int_set(&session->destroy, 0);
Expand Down
18 changes: 8 additions & 10 deletions plugins/janus_audiobridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -4107,16 +4107,14 @@ static void *janus_audiobridge_mixer_thread(void *data) {
outBuffer[i] = sumBuffer[i];
/* Enqueue this mixed frame for encoding in the participant thread */
janus_audiobridge_rtp_relay_packet *mixedpkt = g_malloc(sizeof(janus_audiobridge_rtp_relay_packet));
if(mixedpkt != NULL) {
mixedpkt->data = g_malloc(samples*2);
memcpy(mixedpkt->data, outBuffer, samples*2);
mixedpkt->length = samples; /* We set the number of samples here, not the data length */
mixedpkt->timestamp = ts;
mixedpkt->seq_number = seq;
mixedpkt->ssrc = audiobridge->room_id;
mixedpkt->silence = FALSE;
g_async_queue_push(p->outbuf, mixedpkt);
}
mixedpkt->data = g_malloc(samples*2);
memcpy(mixedpkt->data, outBuffer, samples*2);
mixedpkt->length = samples; /* We set the number of samples here, not the data length */
mixedpkt->timestamp = ts;
mixedpkt->seq_number = seq;
mixedpkt->ssrc = audiobridge->room_id;
mixedpkt->silence = FALSE;
g_async_queue_push(p->outbuf, mixedpkt);
if(pkt) {
if(pkt->data)
g_free(pkt->data);
Expand Down
28 changes: 0 additions & 28 deletions plugins/janus_voicemail.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,12 +709,6 @@ static void *janus_voicemail_handler(void *data) {
goto error;
}
session->stream = g_malloc0(sizeof(ogg_stream_state));
if(session->stream == NULL) {
JANUS_LOG(LOG_ERR, "Couldn't allocate stream struct\n");
error_code = JANUS_VOICEMAIL_ERROR_UNKNOWN_ERROR;
g_snprintf(error_cause, 512, "Couldn't allocate stream struct");
goto error;
}
if(ogg_stream_init(session->stream, rand()) < 0) {
JANUS_LOG(LOG_ERR, "Couldn't initialize Ogg stream state\n");
error_code = JANUS_VOICEMAIL_ERROR_LIBOGG_ERROR;
Expand Down Expand Up @@ -891,15 +885,6 @@ ogg_packet *op_opushead(void) {
unsigned char *data = g_malloc(size);
ogg_packet *op = g_malloc(sizeof(*op));

if(!data) {
JANUS_LOG(LOG_ERR, "Couldn't allocate data buffer...\n");
return NULL;
}
if(!op) {
JANUS_LOG(LOG_ERR, "Couldn't allocate Ogg packet...\n");
return NULL;
}

memcpy(data, "OpusHead", 8); /* identifier */
data[8] = 1; /* version */
data[9] = 2; /* channels */
Expand All @@ -926,15 +911,6 @@ ogg_packet *op_opustags(void) {
unsigned char *data = g_malloc(size);
ogg_packet *op = g_malloc(sizeof(*op));

if(!data) {
JANUS_LOG(LOG_ERR, "Couldn't allocate data buffer...\n");
return NULL;
}
if(!op) {
JANUS_LOG(LOG_ERR, "Couldn't allocate Ogg packet...\n");
return NULL;
}

memcpy(data, identifier, 8);
le32(data + 8, strlen(vendor));
memcpy(data + 12, vendor, strlen(vendor));
Expand All @@ -953,10 +929,6 @@ ogg_packet *op_opustags(void) {
/* Allocate an ogg_packet */
ogg_packet *op_from_pkt(const unsigned char *pkt, int len) {
ogg_packet *op = g_malloc(sizeof(*op));
if(!op) {
JANUS_LOG(LOG_ERR, "Couldn't allocate Ogg packet.\n");
return NULL;
}

op->packet = (unsigned char *)pkt;
op->bytes = len;
Expand Down
2 changes: 0 additions & 2 deletions plugins/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
janus_plugin_result *janus_plugin_result_new(janus_plugin_result_type type, const char *text, json_t *content) {
JANUS_LOG(LOG_HUGE, "Creating plugin result...\n");
janus_plugin_result *result = (janus_plugin_result *)g_malloc(sizeof(janus_plugin_result));
if(result == NULL)
return NULL;
result->type = type;
result->text = text;
result->content = content;
Expand Down
8 changes: 0 additions & 8 deletions postprocessing/janus-pp-rec.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,6 @@ int main(int argc, char *argv[])
len -= sizeof(gint64);
/* Generate frame packet and insert in the ordered list */
janus_pp_frame_packet *p = g_malloc(sizeof(janus_pp_frame_packet));
if(p == NULL) {
JANUS_LOG(LOG_ERR, "Memory error!\n");
return -1;
}
p->seq = 0;
/* We "abuse" the timestamp field for the timing info */
p->ts = when-c_time;
Expand Down Expand Up @@ -474,10 +470,6 @@ int main(int argc, char *argv[])
}
/* Generate frame packet and insert in the ordered list */
janus_pp_frame_packet *p = g_malloc0(sizeof(janus_pp_frame_packet));
if(p == NULL) {
JANUS_LOG(LOG_ERR, "Memory error!\n");
return -1;
}
p->seq = ntohs(rtp->seq_number);
p->pt = rtp->type;
/* Due to resets, we need to mess a bit with the original timestamps */
Expand Down
26 changes: 0 additions & 26 deletions postprocessing/pp-opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ int ogg_flush(void);

int janus_pp_opus_create(char *destination) {
stream = g_malloc0(sizeof(ogg_stream_state));
if(stream == NULL) {
JANUS_LOG(LOG_ERR, "Couldn't allocate stream struct\n");
return -1;
}
if(ogg_stream_init(stream, rand()) < 0) {
JANUS_LOG(LOG_ERR, "Couldn't initialize Ogg stream state\n");
return -1;
Expand Down Expand Up @@ -164,15 +160,6 @@ ogg_packet *op_opushead(void) {
unsigned char *data = g_malloc(size);
ogg_packet *op = g_malloc(sizeof(*op));

if(!data) {
JANUS_LOG(LOG_ERR, "Couldn't allocate data buffer...\n");
return NULL;
}
if(!op) {
JANUS_LOG(LOG_ERR, "Couldn't allocate Ogg packet...\n");
return NULL;
}

memcpy(data, "OpusHead", 8); /* identifier */
data[8] = 1; /* version */
data[9] = 2; /* channels */
Expand All @@ -199,15 +186,6 @@ ogg_packet *op_opustags(void) {
unsigned char *data = g_malloc(size);
ogg_packet *op = g_malloc(sizeof(*op));

if(!data) {
JANUS_LOG(LOG_ERR, "Couldn't allocate data buffer...\n");
return NULL;
}
if(!op) {
JANUS_LOG(LOG_ERR, "Couldn't allocate Ogg packet...\n");
return NULL;
}

memcpy(data, identifier, 8);
le32(data + 8, strlen(vendor));
memcpy(data + 12, vendor, strlen(vendor));
Expand All @@ -226,10 +204,6 @@ ogg_packet *op_opustags(void) {
/* Allocate an ogg_packet */
ogg_packet *op_from_pkt(const unsigned char *pkt, int len) {
ogg_packet *op = g_malloc(sizeof(*op));
if(!op) {
JANUS_LOG(LOG_ERR, "Couldn't allocate Ogg packet.\n");
return NULL;
}

op->packet = (unsigned char *)pkt;
op->bytes = len;
Expand Down
4 changes: 0 additions & 4 deletions record.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ janus_recorder *janus_recorder_create(const char *dir, const char *codec, const
}
/* Create the recorder */
janus_recorder *rc = g_malloc0(sizeof(janus_recorder));
if(rc == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
rc->dir = NULL;
rc->filename = NULL;
rc->file = NULL;
Expand Down
14 changes: 0 additions & 14 deletions sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ janus_sctp_association *janus_sctp_association_create(void *dtls, uint64_t handl
* usrsctp itself... as such, we make use of the AF_CONN approach */

janus_sctp_association *sctp = (janus_sctp_association *)g_malloc0(sizeof(janus_sctp_association));
if(sctp == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
sctp->dtls = dtls;
sctp->handle_id = handle_id;
sctp->local_port = 5000; /* FIXME We always use this one */
Expand Down Expand Up @@ -712,9 +708,6 @@ void janus_sctp_send_outgoing_stream_reset(janus_sctp_association *sctp) {
}
len = sizeof(sctp_assoc_t) + (2 + sctp->stream_buffer_counter) * sizeof(uint16_t);
srs = (struct sctp_reset_streams *)g_malloc0(len);
if(srs == NULL) {
return;
}
srs->srs_flags = SCTP_STREAM_RESET_OUTGOING;
srs->srs_number_streams = sctp->stream_buffer_counter;
for(i = 0; i < sctp->stream_buffer_counter; i++) {
Expand Down Expand Up @@ -1346,14 +1339,7 @@ janus_sctp_message *janus_sctp_message_create(gboolean incoming, char *buffer, s
if(buffer == NULL || length == 0)
return NULL;
janus_sctp_message *message = g_malloc(sizeof(janus_sctp_message));
if(message == NULL)
return NULL;
message->buffer = g_malloc(length);
if(message->buffer == NULL) {
g_free(message);
message = NULL;
return NULL;
}
memcpy(message->buffer, buffer, length);
message->length = length;
message->incoming = incoming;
Expand Down
4 changes: 0 additions & 4 deletions text2pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ janus_text2pcap *janus_text2pcap_create(const char *dir, const char *filename, i
return NULL;
/* Create the text2pcap instance */
janus_text2pcap *tp = g_malloc0(sizeof(janus_text2pcap));
if(tp == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
tp->filename = NULL;
tp->file = NULL;
tp->truncate = truncate;
Expand Down
24 changes: 0 additions & 24 deletions transports/janus_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1122,12 +1122,6 @@ int janus_http_handler(void *cls, struct MHD_Connection *connection, const char
JANUS_LOG(LOG_DBG, "Got a HTTP %s request on %s...\n", method, url);
JANUS_LOG(LOG_DBG, " ... Just parsing headers for now...\n");
msg = g_malloc0(sizeof(janus_http_msg));
if(msg == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
ret = MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
MHD_destroy_response(response);
goto done;
}
msg->connection = connection;
janus_mutex_init(&msg->wait_mutex);
janus_condition_init(&msg->wait_cond);
Expand Down Expand Up @@ -1245,12 +1239,6 @@ int janus_http_handler(void *cls, struct MHD_Connection *connection, const char
msg->payload = g_malloc0(*upload_data_size+1);
else
msg->payload = g_realloc(msg->payload, msg->len+*upload_data_size+1);
if(msg->payload == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
ret = MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
MHD_destroy_response(response);
goto done;
}
memcpy(msg->payload+msg->len, upload_data, *upload_data_size);
msg->len += *upload_data_size;
memset(msg->payload + msg->len, '\0', 1);
Expand Down Expand Up @@ -1485,12 +1473,6 @@ int janus_http_admin_handler(void *cls, struct MHD_Connection *connection, const
JANUS_LOG(LOG_VERB, "Got an admin/monitor HTTP %s request on %s...\n", method, url);
JANUS_LOG(LOG_DBG, " ... Just parsing headers for now...\n");
msg = g_malloc0(sizeof(janus_http_msg));
if(msg == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
ret = MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
MHD_destroy_response(response);
goto done;
}
msg->connection = connection;
janus_mutex_init(&msg->wait_mutex);
janus_condition_init(&msg->wait_cond);
Expand Down Expand Up @@ -1610,12 +1592,6 @@ int janus_http_admin_handler(void *cls, struct MHD_Connection *connection, const
msg->payload = g_malloc0(*upload_data_size+1);
else
msg->payload = g_realloc(msg->payload, msg->len+*upload_data_size+1);
if(msg->payload == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
ret = MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
MHD_destroy_response(response);
goto done;
}
memcpy(msg->payload+msg->len, upload_data, *upload_data_size);
msg->len += *upload_data_size;
memset(msg->payload + msg->len, '\0', 1);
Expand Down
4 changes: 0 additions & 4 deletions transports/janus_rabbitmq.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,6 @@ int janus_rabbitmq_init(janus_transport_callbacks *callback, const char *config_
} else {
/* FIXME We currently support a single application, create a new janus_rabbitmq_client instance */
rmq_client = g_malloc0(sizeof(janus_rabbitmq_client));
if(rmq_client == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
goto error;
}
/* Connect */
rmq_client->rmq_conn = amqp_new_connection();
amqp_socket_t *socket = NULL;
Expand Down
5 changes: 0 additions & 5 deletions turnrest.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ static size_t janus_turnrest_callback(void *payload, size_t size, size_t nmemb,
janus_turnrest_buffer *buf = (struct janus_turnrest_buffer *)data;
/* (Re)allocate if needed */
buf->buffer = g_realloc(buf->buffer, buf->size+realsize+1);
if(buf->buffer == NULL) {
/* Memory error! */
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return 0;
}
/* Update the buffer */
memcpy(&(buf->buffer[buf->size]), payload, realsize);
buf->size += realsize;
Expand Down
8 changes: 0 additions & 8 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ char *janus_string_replace(char *message, const char *old_string, const char *ne
uint16_t old_stringlen = strlen(outgoing)+1, new_stringlen = old_stringlen + diff*counter;
if(diff > 0) { /* Resize now */
tmp = g_realloc(outgoing, new_stringlen);
if(!tmp) {
g_free(outgoing);
return NULL;
}
outgoing = tmp;
}
/* Replace string */
Expand All @@ -187,10 +183,6 @@ char *janus_string_replace(char *message, const char *old_string, const char *ne
}
if(diff < 0) { /* We skipped the resize previously (shrinking memory) */
tmp = g_realloc(outgoing, new_stringlen);
if(!tmp) {
g_free(outgoing);
return NULL;
}
outgoing = tmp;
}
outgoing[strlen(outgoing)] = '\0';
Expand Down

0 comments on commit 1875faf

Please sign in to comment.