core: fix bugs with calls to realloc

v2.8-utf8proc
Sebastien Helleu 2011-08-28 15:25:30 +02:00
parent e411d14b7a
commit f843f904bc
16 changed files with 259 additions and 106 deletions

View File

@ -1039,7 +1039,7 @@ char **
string_split_command (const char *command, char separator)
{
int nb_substr, arr_idx, str_idx, type;
char **array;
char **array, **array2;
char *buffer, *p;
const char *ptr;
@ -1105,12 +1105,18 @@ string_split_command (const char *command, char separator)
array[arr_idx++] = strdup (p);
array[arr_idx] = NULL;
free (buffer);
array = realloc (array, (arr_idx + 1) * sizeof(array[0]));
return array;
array2 = realloc (array, (arr_idx + 1) * sizeof(array[0]));
if (!array2)
{
if (array)
free (array);
return NULL;
}
return array2;
}
/*
@ -1611,7 +1617,7 @@ string_replace_with_hashtable (const char *string,
int *errors)
{
int length, length_value, index_string, index_result;
char *result, *key;
char *result, *result2, *key;
const char *pos_end_name, *ptr_value;
*errors = 0;
@ -1651,12 +1657,15 @@ string_replace_with_hashtable (const char *string,
{
length_value = strlen (ptr_value);
length += length_value;
result = realloc (result, length);
if (!result)
result2 = realloc (result, length);
if (!result2)
{
if (result)
free (result);
free (key);
return NULL;
}
result = result2;
strcpy (result + index_result, ptr_value);
index_result += length_value;
index_string += pos_end_name - string -

View File

@ -369,25 +369,41 @@ util_search_full_lib_name (const char *filename, const char *sys_directory)
char *
util_file_get_content (const char *filename)
{
char *buffer;
char *buffer, *buffer2;
FILE *f;
size_t count, fp;
buffer = NULL;
fp = 0;
f = fopen(filename, "r");
if (f) {
while(!feof(f)) {
buffer = (char *) realloc(buffer, (fp + 1024*sizeof(char)));
count = fread(&buffer[fp], sizeof(char), 1024, f);
f = fopen (filename, "r");
if (f)
{
while (!feof (f))
{
buffer2 = (char *) realloc (buffer, (fp + (1024 * sizeof (char))));
if (!buffer2)
{
if (buffer)
free (buffer);
return NULL;
}
buffer = buffer2;
count = fread (&buffer[fp], sizeof(char), 1024, f);
fp += count;
}
buffer = (char *) realloc(buffer, fp + sizeof(char));
buffer2 = (char *) realloc (buffer, fp + sizeof (char));
if (!buffer2)
{
if (buffer)
free (buffer);
return NULL;
}
buffer = buffer2;
buffer[fp] = '\0';
fclose(f);
fclose (f);
}
return buffer;
}

View File

@ -616,7 +616,7 @@ gui_bar_window_content_get_with_filling (struct t_gui_bar_window *bar_window,
struct t_gui_window *window)
{
enum t_gui_bar_filling filling;
char *ptr_content, *content, str_reinit_color[32];
char *ptr_content, *content, *content2, str_reinit_color[32];
char str_reinit_color_space[32], str_reinit_color_space_start_line[32];
char str_start_item[32];
char *item_value, *item_value2, ****split_items, **linear_items;
@ -694,7 +694,14 @@ gui_bar_window_content_get_with_filling (struct t_gui_bar_window *bar_window,
content_length += ((filling == GUI_BAR_FILLING_HORIZONTAL) ? length_start_item : 0) +
length_reinit_color_space +
strlen ((item_value) ? item_value : ptr_content);
content = realloc (content, content_length);
content2 = realloc (content, content_length);
if (!content2)
{
if (content)
free (content);
return NULL;
}
content = content2;
if (at_least_one_item && first_sub_item)
{
/* first sub item: insert space after last item */
@ -722,9 +729,15 @@ gui_bar_window_content_get_with_filling (struct t_gui_bar_window *bar_window,
if (filling == GUI_BAR_FILLING_HORIZONTAL)
{
content_length += length_start_item;
content = realloc (content, content_length);
if (content)
strcat (content, str_start_item);
content2 = realloc (content, content_length);
if (!content2)
{
if (content)
free (content);
return NULL;
}
content = content2;
strcat (content, str_start_item);
}
}
}
@ -732,9 +745,15 @@ gui_bar_window_content_get_with_filling (struct t_gui_bar_window *bar_window,
if (filling == GUI_BAR_FILLING_HORIZONTAL)
{
content_length += length_start_item;
content = realloc (content, content_length);
if (content)
strcat (content, str_start_item);
content2 = realloc (content, content_length);
if (!content2)
{
if (content)
free (content);
return NULL;
}
content = content2;
strcat (content, str_start_item);
}
break;
case GUI_BAR_FILLING_COLUMNS_HORIZONTAL: /* items in columns, with horizontal filling */
@ -822,49 +841,53 @@ gui_bar_window_content_get_with_filling (struct t_gui_bar_window *bar_window,
content_length = 1 + (lines *
((columns *
(max_length + max_length_screen + length_reinit_color_space)) + 1));
content = realloc (content, content_length);
if (content)
content2 = realloc (content, content_length);
if (!content2)
{
content[0] = '\0';
index_content = 0;
for (i = 0; i < lines; i++)
if (content)
free (content);
return NULL;
}
content = content2;
content[0] = '\0';
index_content = 0;
for (i = 0; i < lines; i++)
{
for (j = 0; j < columns; j++)
{
for (j = 0; j < columns; j++)
if (filling == GUI_BAR_FILLING_COLUMNS_HORIZONTAL)
index = (i * columns) + j;
else
index = (j * lines) + i;
if (index >= total_items)
{
if (filling == GUI_BAR_FILLING_COLUMNS_HORIZONTAL)
index = (i * columns) + j;
else
index = (j * lines) + i;
if (index >= total_items)
for (k = 0; k < max_length_screen; k++)
{
for (k = 0; k < max_length_screen; k++)
{
content[index_content++] = ' ';
}
}
else
{
strcpy (content + index_content, linear_items[index]);
index_content += strlen (linear_items[index]);
length = max_length_screen -
gui_chat_strlen_screen (linear_items[index]);
for (k = 0; k < length; k++)
{
content[index_content++] = ' ';
}
}
if (j < columns - 1)
{
strcpy (content + index_content,
str_reinit_color_space);
index_content += length_reinit_color_space;
content[index_content++] = ' ';
}
}
content[index_content++] = '\n';
else
{
strcpy (content + index_content, linear_items[index]);
index_content += strlen (linear_items[index]);
length = max_length_screen -
gui_chat_strlen_screen (linear_items[index]);
for (k = 0; k < length; k++)
{
content[index_content++] = ' ';
}
}
if (j < columns - 1)
{
strcpy (content + index_content,
str_reinit_color_space);
index_content += length_reinit_color_space;
}
}
content[index_content] = '\0';
content[index_content++] = '\n';
}
content[index_content] = '\0';
free (linear_items);
}
@ -905,6 +928,8 @@ gui_bar_window_coords_add (struct t_gui_bar_window *bar_window,
int index_item, int index_subitem, int index_line,
int x, int y)
{
struct t_gui_bar_window_coords **coords2;
if (!bar_window->coords)
{
bar_window->coords_count = 1;
@ -913,8 +938,19 @@ gui_bar_window_coords_add (struct t_gui_bar_window *bar_window,
else
{
bar_window->coords_count++;
bar_window->coords = realloc (bar_window->coords,
bar_window->coords_count * sizeof (*(bar_window->coords)));
coords2 = realloc (bar_window->coords,
bar_window->coords_count * sizeof (*(bar_window->coords)));
if (!coords2)
{
if (bar_window->coords)
{
free (bar_window->coords);
bar_window->coords = NULL;
}
bar_window->coords_count = 0;
return;
}
bar_window->coords = coords2;
}
bar_window->coords[bar_window->coords_count - 1] = malloc (sizeof (*(bar_window->coords[bar_window->coords_count - 1])));
bar_window->coords[bar_window->coords_count - 1]->item = index_item;

View File

@ -607,7 +607,7 @@ gui_buffer_string_replace_local_var (struct t_gui_buffer *buffer,
const char *string)
{
int length, length_var, index_string, index_result;
char *result, *local_var;
char *result, *result2, *local_var;
const char *pos_end_name, *ptr_value;
if (!string)
@ -646,12 +646,15 @@ gui_buffer_string_replace_local_var (struct t_gui_buffer *buffer,
{
length_var = strlen (ptr_value);
length += length_var;
result = realloc (result, length);
if (!result)
result2 = realloc (result, length);
if (!result2)
{
if (result)
free (result);
free (local_var);
return NULL;
}
result = result2;
strcpy (result + index_result, ptr_value);
index_result += length_var;
index_string += strlen (local_var) + 1;

View File

@ -576,7 +576,7 @@ char *
gui_color_string_replace_colors (const char *string)
{
int length, length_color, index_string, index_result;
char *result, *color_name;
char *result, *result2, *color_name;
const char *pos_end_name, *ptr_color;
if (!string)
@ -611,12 +611,15 @@ gui_color_string_replace_colors (const char *string)
{
length_color = strlen (ptr_color);
length += length_color;
result = realloc (result, length);
if (!result)
result2 = realloc (result, length);
if (!result2)
{
if (result)
free (result);
free (color_name);
return NULL;
}
result = result2;
strcpy (result + index_result, ptr_color);
index_result += length_color;
index_string += pos_end_name - string -

View File

@ -59,6 +59,7 @@ void
gui_input_optimize_size (struct t_gui_buffer *buffer)
{
int optimal_size;
char *input_buffer2;
if (buffer->input)
{
@ -67,7 +68,17 @@ gui_input_optimize_size (struct t_gui_buffer *buffer)
if (buffer->input_buffer_alloc != optimal_size)
{
buffer->input_buffer_alloc = optimal_size;
buffer->input_buffer = realloc (buffer->input_buffer, optimal_size);
input_buffer2 = realloc (buffer->input_buffer, optimal_size);
if (!input_buffer2)
{
if (buffer->input_buffer)
{
free (buffer->input_buffer);
buffer->input_buffer = NULL;
}
return;
}
buffer->input_buffer = input_buffer2;
}
}
}

View File

@ -1323,7 +1323,7 @@ gui_key_free_all (struct t_gui_key **keys, struct t_gui_key **last_key,
void
gui_key_buffer_optimize ()
{
int optimal_size;
int optimal_size, *gui_key_buffer2;
optimal_size = (((gui_key_buffer_size * sizeof (int)) /
GUI_KEY_BUFFER_BLOCK_SIZE) *
@ -1333,7 +1333,17 @@ gui_key_buffer_optimize ()
if (gui_key_buffer_alloc != optimal_size)
{
gui_key_buffer_alloc = optimal_size;
gui_key_buffer = realloc (gui_key_buffer, optimal_size);
gui_key_buffer2 = realloc (gui_key_buffer, optimal_size);
if (!gui_key_buffer2)
{
if (gui_key_buffer)
{
free (gui_key_buffer);
gui_key_buffer = NULL;
}
return;
}
gui_key_buffer = gui_key_buffer2;
}
}

View File

@ -97,7 +97,8 @@ void
alias_string_add_word (char **alias, int *length, const char *word)
{
int length_word;
char *alias2;
if (!word)
return;
@ -112,7 +113,17 @@ alias_string_add_word (char **alias, int *length, const char *word)
}
else
{
*alias = realloc (*alias, strlen (*alias) + length_word + 1);
alias2 = realloc (*alias, strlen (*alias) + length_word + 1);
if (!alias2)
{
if (*alias)
{
free (*alias);
*alias = NULL;
}
return;
}
*alias = alias2;
strcat (*alias, word);
}
*length += length_word;

View File

@ -176,7 +176,7 @@ irc_mode_channel_set (struct t_irc_server *server,
void
irc_mode_user_add (struct t_irc_server *server, char mode)
{
char str_mode[2];
char str_mode[2], *nick_modes2;
str_mode[0] = mode;
str_mode[1] = '\0';
@ -185,8 +185,18 @@ irc_mode_user_add (struct t_irc_server *server, char mode)
{
if (!strchr (server->nick_modes, mode))
{
server->nick_modes = realloc (server->nick_modes,
strlen (server->nick_modes) + 1 + 1);
nick_modes2 = realloc (server->nick_modes,
strlen (server->nick_modes) + 1 + 1);
if (!nick_modes2)
{
if (server->nick_modes)
{
free (server->nick_modes);
server->nick_modes = NULL;
}
return;
}
server->nick_modes = nick_modes2;
strcat (server->nick_modes, str_mode);
weechat_bar_item_update ("input_prompt");
}
@ -206,7 +216,7 @@ irc_mode_user_add (struct t_irc_server *server, char mode)
void
irc_mode_user_remove (struct t_irc_server *server, char mode)
{
char *pos;
char *pos, *nick_modes2;
int new_size;
if (server->nick_modes)
@ -216,7 +226,9 @@ irc_mode_user_remove (struct t_irc_server *server, char mode)
{
new_size = strlen (server->nick_modes);
memmove (pos, pos + 1, strlen (pos + 1) + 1);
server->nick_modes = realloc (server->nick_modes, new_size);
nick_modes2 = realloc (server->nick_modes, new_size);
if (nick_modes2)
server->nick_modes = nick_modes2;
weechat_bar_item_update ("input_prompt");
}
}

View File

@ -118,7 +118,7 @@ irc_notify_search (struct t_irc_server *server, const char *nick)
void
irc_notify_set_server_option (struct t_irc_server *server)
{
char *str;
char *str, *str2;
struct t_irc_notify *ptr_notify;
int total_length, length;
@ -143,7 +143,14 @@ irc_notify_set_server_option (struct t_irc_server *server)
else
{
total_length += length;
str = realloc (str, total_length);
str2 = realloc (str, total_length);
if (!str2)
{
if (str)
free (str);
return;
}
str = str2;
}
if (str)
{
@ -754,7 +761,8 @@ irc_notify_timer_ison_cb (void *data, int remaining_calls)
message2 = realloc (message, total_length);
if (!message2)
{
free (message);
if (message)
free (message);
message = NULL;
break;
}

View File

@ -1908,7 +1908,7 @@ IRC_PROTOCOL_CALLBACK(001)
IRC_PROTOCOL_CALLBACK(005)
{
char *pos, *pos2, *pos_start, *error;
char *pos, *pos2, *pos_start, *error, *isupport2;
int length_isupport, length, nick_max_length;
/*
@ -1961,11 +1961,14 @@ IRC_PROTOCOL_CALLBACK(005)
if (server->isupport)
{
length_isupport = strlen (server->isupport);
server->isupport = realloc (server->isupport,
length_isupport + /* existing */
1 + length + 1); /* new */
if (server->isupport)
isupport2 = realloc (server->isupport,
length_isupport + /* existing */
1 + length + 1); /* new */
if (isupport2)
{
server->isupport = isupport2;
pos_start = server->isupport + length_isupport;
}
}
else
{

View File

@ -647,6 +647,8 @@ void
irc_redirect_message_add (struct t_irc_redirect *redirect, const char *message,
const char *command)
{
char *output2;
/*
* if command is not for output, then don't add message
* (it is silently ignored)
@ -659,9 +661,16 @@ irc_redirect_message_add (struct t_irc_redirect *redirect, const char *message,
if (redirect->output)
{
redirect->output_size += strlen("\n") + strlen (message);
redirect->output = realloc (redirect->output, redirect->output_size);
if (redirect->output)
strcat (redirect->output, "\n");
output2 = realloc (redirect->output, redirect->output_size);
if (!output2)
{
free (redirect->output);
redirect->output = NULL;
redirect->output_size = 0;
return;
}
redirect->output = output2;
strcat (redirect->output, "\n");
}
else
{

View File

@ -1888,23 +1888,28 @@ irc_server_msgq_add_msg (struct t_irc_server *server, const char *msg)
void
irc_server_msgq_add_unterminated (struct t_irc_server *server, const char *string)
{
char *unterminated_message2;
if (!string[0])
return;
if (server->unterminated_message)
{
server->unterminated_message =
unterminated_message2 =
realloc (server->unterminated_message,
(strlen (server->unterminated_message) +
strlen (string) + 1));
if (!server->unterminated_message)
if (!unterminated_message2)
{
weechat_printf (server->buffer,
_("%s%s: not enough memory for received message"),
weechat_prefix ("error"), IRC_PLUGIN_NAME);
free (server->unterminated_message);
server->unterminated_message = NULL;
return;
}
else
strcat (server->unterminated_message, string);
server->unterminated_message = unterminated_message2;
strcat (server->unterminated_message, string);
}
else
{

View File

@ -469,7 +469,7 @@ void
relay_client_irc_send_join (struct t_relay_client *client,
const char *channel)
{
char *infolist_name, *nicks;
char *infolist_name, *nicks, *nicks2;
const char *nick, *prefix, *topic;
char *host;
int length, length_nicks;
@ -540,7 +540,14 @@ relay_client_irc_send_join (struct t_relay_client *client,
length_nicks += strlen (nick) + 1 + 1;
if (nicks)
{
nicks = realloc (nicks, length_nicks);
nicks2 = realloc (nicks, length_nicks);
if (!nicks2)
{
if (nicks)
free (nicks);
return;
}
nicks = nicks2;
strcat (nicks, " ");
}
else

View File

@ -130,7 +130,7 @@ char *
rmodifier_replace_groups (const char *string, regmatch_t regex_match[],
const char *groups)
{
char *result, *str_group, *string_to_add;
char *result, *result2, *str_group, *string_to_add;
const char *ptr_groups;
int length, num_group;
@ -161,9 +161,14 @@ rmodifier_replace_groups (const char *string, regmatch_t regex_match[],
if (string_to_add)
{
length += strlen (string_to_add);
result = realloc (result, length);
if (!result)
result2 = realloc (result, length);
if (!result2)
{
if (result)
free (result);
return NULL;
}
result = result2;
strcat (result, string_to_add);
free (string_to_add);
}

View File

@ -817,7 +817,8 @@ void
script_action_add (char **action_list, const char *name)
{
int length;
char *action_list2;
length = strlen (name);
if (!(*action_list))
@ -828,13 +829,17 @@ script_action_add (char **action_list, const char *name)
}
else
{
*action_list = realloc (*action_list,
action_list2 = realloc (*action_list,
strlen (*action_list) + 1 + length + 1);
if (*action_list)
if (!action_list2)
{
strcat (*action_list, ",");
strcat (*action_list, name);
free (*action_list);
*action_list = NULL;
return;
}
*action_list = action_list2;
strcat (*action_list, ",");
strcat (*action_list, name);
}
}