Commit 96424ce2 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

fix some points of the code which does not check if alloc memory was done successful

parent 656ea427
...@@ -160,7 +160,10 @@ ngx_http_push_stream_send_response_all_channels_info_summarized(ngx_http_request ...@@ -160,7 +160,10 @@ ngx_http_push_stream_send_response_all_channels_info_summarized(ngx_http_request
len = (subtype->format_summarized_worker_item->len > subtype->format_summarized_worker_last_item->len) ? subtype->format_summarized_worker_item->len : subtype->format_summarized_worker_last_item->len; len = (subtype->format_summarized_worker_item->len > subtype->format_summarized_worker_last_item->len) ? subtype->format_summarized_worker_item->len : subtype->format_summarized_worker_last_item->len;
len = used_slots * (2*NGX_INT_T_LEN + len - 5); //minus 5 sprintf len = used_slots * (2*NGX_INT_T_LEN + len - 5); //minus 5 sprintf
subscribers_by_workers = ngx_pcalloc(r->pool, len); if ((subscribers_by_workers = ngx_pcalloc(r->pool, len)) == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate memory to write workers statistics.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memset(subscribers_by_workers, '\0', len); ngx_memset(subscribers_by_workers, '\0', len);
start = subscribers_by_workers; start = subscribers_by_workers;
for (i = 0, j = 0; (i < used_slots) && (j < NGX_MAX_PROCESSES); j++) { for (i = 0, j = 0; (i < used_slots) && (j < NGX_MAX_PROCESSES); j++) {
...@@ -253,8 +256,10 @@ ngx_http_push_stream_send_response_all_channels_info_detailed(ngx_http_request_t ...@@ -253,8 +256,10 @@ ngx_http_push_stream_send_response_all_channels_info_detailed(ngx_http_request_t
} }
format = (next != &queue_channel_info) ? subtype->format_group_item : subtype->format_group_last_item; format = (next != &queue_channel_info) ? subtype->format_group_item : subtype->format_group_last_item;
if ((chain->buf = ngx_http_push_stream_channel_info_formatted(r->pool, format, &channel_info->id, channel_info->published_messages, channel_info->stored_messages, channel_info->subscribers)) == NULL) {
chain->buf = ngx_http_push_stream_channel_info_formatted(r->pool, format, &channel_info->id, channel_info->published_messages, channel_info->stored_messages, channel_info->subscribers); ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory to format channel info");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
chain->buf->last_buf = 0; chain->buf->last_buf = 0;
content_len += ngx_buf_size(chain->buf); content_len += ngx_buf_size(chain->buf);
......
...@@ -502,6 +502,10 @@ ngx_http_push_stream_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ...@@ -502,6 +502,10 @@ ngx_http_push_stream_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
// append crlf to templates // append crlf to templates
if (conf->header_template.len > 0) { if (conf->header_template.len > 0) {
ngx_str_t * aux = ngx_http_push_stream_get_formatted_chunk(conf->header_template.data, conf->header_template.len, cf->pool); ngx_str_t * aux = ngx_http_push_stream_get_formatted_chunk(conf->header_template.data, conf->header_template.len, cf->pool);
if (aux == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to format header template");
return NGX_CONF_ERROR;
}
conf->header_template.data = aux->data; conf->header_template.data = aux->data;
conf->header_template.len = aux->len; conf->header_template.len = aux->len;
} }
......
...@@ -142,21 +142,18 @@ ngx_http_push_stream_convert_char_to_msg_on_shared_locked(u_char *data, size_t l ...@@ -142,21 +142,18 @@ ngx_http_push_stream_convert_char_to_msg_on_shared_locked(u_char *data, size_t l
ngx_http_push_stream_msg_t *msg; ngx_http_push_stream_msg_t *msg;
int i = 0; int i = 0;
msg = ngx_slab_alloc_locked(shpool, sizeof(ngx_http_push_stream_msg_t)); if ((msg = ngx_slab_alloc_locked(shpool, sizeof(ngx_http_push_stream_msg_t))) == NULL) {
if (msg == NULL) {
return NULL; return NULL;
} }
msg->formatted_messages = NULL; msg->formatted_messages = NULL;
msg->buf = ngx_slab_alloc_locked(shpool, sizeof(ngx_buf_t)); if ((msg->buf = ngx_slab_alloc_locked(shpool, sizeof(ngx_buf_t))) == NULL) {
if (msg->buf == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg); ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL; return NULL;
} }
msg->buf->start = ngx_slab_alloc_locked(shpool, len + 1); if ((msg->buf->start = ngx_slab_alloc_locked(shpool, len + 1)) == NULL) {
if (msg->buf->start == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg); ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL; return NULL;
} }
...@@ -177,14 +174,20 @@ ngx_http_push_stream_convert_char_to_msg_on_shared_locked(u_char *data, size_t l ...@@ -177,14 +174,20 @@ ngx_http_push_stream_convert_char_to_msg_on_shared_locked(u_char *data, size_t l
msg->raw.data = msg->buf->start; msg->raw.data = msg->buf->start;
msg->raw.len = len; msg->raw.len = len;
msg->formatted_messages = ngx_slab_alloc_locked(shpool, sizeof(ngx_str_t)*ngx_http_push_stream_module_main_conf->qtd_templates); if ((msg->formatted_messages = ngx_slab_alloc_locked(shpool, sizeof(ngx_str_t)*ngx_http_push_stream_module_main_conf->qtd_templates)) == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL;
}
while ((cur = (ngx_http_push_stream_template_queue_t *) ngx_queue_next(&cur->queue)) != sentinel) { while ((cur = (ngx_http_push_stream_template_queue_t *) ngx_queue_next(&cur->queue)) != sentinel) {
ngx_str_t *aux = ngx_http_push_stream_format_message(channel, msg, cur->template, temp_pool); ngx_str_t *aux = ngx_http_push_stream_format_message(channel, msg, cur->template, temp_pool);
if (aux == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL;
}
ngx_str_t *chunk = ngx_http_push_stream_get_formatted_chunk(aux->data, aux->len, temp_pool); ngx_str_t *chunk = ngx_http_push_stream_get_formatted_chunk(aux->data, aux->len, temp_pool);
ngx_str_t *formmated = (msg->formatted_messages + i); ngx_str_t *formmated = (msg->formatted_messages + i);
formmated->data = ngx_slab_alloc_locked(shpool, chunk->len + 1); if ((chunk == NULL) || ((formmated->data = ngx_slab_alloc_locked(shpool, chunk->len + 1)) == NULL)) {
if (formmated->data == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg); ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL; return NULL;
} }
...@@ -317,7 +320,11 @@ ngx_http_push_stream_delete_channel(ngx_str_t *id, ngx_pool_t *temp_pool) { ...@@ -317,7 +320,11 @@ ngx_http_push_stream_delete_channel(ngx_str_t *id, ngx_pool_t *temp_pool) {
ngx_http_push_stream_ensure_qtd_of_messages_locked(channel, 0, 0); ngx_http_push_stream_ensure_qtd_of_messages_locked(channel, 0, 0);
// apply channel deleted message text to message template // apply channel deleted message text to message template
channel->channel_deleted_message = ngx_http_push_stream_convert_char_to_msg_on_shared_locked(ngx_http_push_stream_module_main_conf->channel_deleted_message_text.data, ngx_http_push_stream_module_main_conf->channel_deleted_message_text.len, channel, NGX_HTTP_PUSH_STREAM_CHANNEL_DELETED_MESSAGE_ID, temp_pool); if ((channel->channel_deleted_message = ngx_http_push_stream_convert_char_to_msg_on_shared_locked(ngx_http_push_stream_module_main_conf->channel_deleted_message_text.data, ngx_http_push_stream_module_main_conf->channel_deleted_message_text.len, channel, NGX_HTTP_PUSH_STREAM_CHANNEL_DELETED_MESSAGE_ID, temp_pool)) == NULL) {
ngx_shmtx_unlock(&(shpool)->mutex);
ngx_log_error(NGX_LOG_ERR, temp_pool->log, 0, "push stream module: unable to allocate memory to channel deleted message");
return;
}
// send signal to each worker with subscriber to this channel // send signal to each worker with subscriber to this channel
cur = &channel->workers_with_subscribers; cur = &channel->workers_with_subscribers;
...@@ -663,6 +670,10 @@ ngx_http_push_stream_buffer_timer_wake_handler(ngx_event_t *ev) ...@@ -663,6 +670,10 @@ ngx_http_push_stream_buffer_timer_wake_handler(ngx_event_t *ev)
static u_char * static u_char *
ngx_http_push_stream_str_replace(u_char *org, u_char *find, u_char *replace, ngx_uint_t offset, ngx_pool_t *pool) ngx_http_push_stream_str_replace(u_char *org, u_char *find, u_char *replace, ngx_uint_t offset, ngx_pool_t *pool)
{ {
if (org == NULL) {
return NULL;
}
ngx_uint_t len_org = ngx_strlen(org); ngx_uint_t len_org = ngx_strlen(org);
ngx_uint_t len_find = ngx_strlen(find); ngx_uint_t len_find = ngx_strlen(find);
ngx_uint_t len_replace = ngx_strlen(replace); ngx_uint_t len_replace = ngx_strlen(replace);
...@@ -670,9 +681,14 @@ ngx_http_push_stream_str_replace(u_char *org, u_char *find, u_char *replace, ngx ...@@ -670,9 +681,14 @@ ngx_http_push_stream_str_replace(u_char *org, u_char *find, u_char *replace, ngx
u_char *result = org; u_char *result = org;
if (len_find > 0) { if (len_find > 0) {
u_char *ret = (u_char *) ngx_strstr(org + offset, find); u_char *ret = (u_char *) ngx_strstr(org + offset, find);
if (ret != NULL) { if (ret != NULL) {
u_char *tmp = ngx_pcalloc(pool, len_org + len_replace + len_find + 1); u_char *tmp = ngx_pcalloc(pool, len_org + len_replace + len_find + 1);
if (tmp == NULL) {
ngx_log_error(NGX_LOG_ERR, pool->log, 0, "push stream module: unable to allocate memory to apply text replace");
return NULL;
}
ngx_memset(tmp, '\0', len_org + len_replace + len_find + 1); ngx_memset(tmp, '\0', len_org + len_replace + len_find + 1);
u_int len_found = ret-org; u_int len_found = ret-org;
...@@ -722,7 +738,10 @@ ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx ...@@ -722,7 +738,10 @@ ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx
if (message != NULL) { if (message != NULL) {
message_id = message->id; message_id = message->id;
len = ngx_buf_size(message->buf); len = ngx_buf_size(message->buf);
msg = ngx_pcalloc(pool, len + 1); if ((msg = ngx_pcalloc(pool, len + 1)) == NULL) {
ngx_log_error(NGX_LOG_ERR, pool->log, 0, "push stream module: unable to allocate memory to copy message text");
return NULL;
}
ngx_memset(msg, '\0', len + 1); ngx_memset(msg, '\0', len + 1);
ngx_memcpy(msg, message->buf->pos, len); ngx_memcpy(msg, message->buf->pos, len);
} }
...@@ -733,10 +752,17 @@ ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx ...@@ -733,10 +752,17 @@ ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx
txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_CHANNEL.data, channel_id, 0, pool); txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_CHANNEL.data, channel_id, 0, pool);
txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_TEXT.data, msg, 0, pool); txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_TEXT.data, msg, 0, pool);
len = ngx_strlen(txt); if (txt == NULL) {
str = ngx_pcalloc(pool, sizeof(ngx_str_t)); ngx_log_error(NGX_LOG_ERR, pool->log, 0, "push stream module: unable to allocate memory to replace message values on template");
return NULL;
}
if ((str = ngx_pcalloc(pool, sizeof(ngx_str_t))) == NULL) {
ngx_log_error(NGX_LOG_ERR, pool->log, 0, "push stream module: unable to allocate memory to return message applied to template");
return NULL;
}
str->data = txt; str->data = txt;
str->len = len; str->len = ngx_strlen(txt);
return str; return str;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment