1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-28 13:56:37 +02:00

core: fix bugs with calls to realloc

This commit is contained in:
Sebastien Helleu
2011-08-28 15:25:30 +02:00
parent e411d14b7a
commit f843f904bc
16 changed files with 259 additions and 106 deletions
+81 -45
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;
+6 -3
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;
+6 -3
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 -
+12 -1
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;
}
}
}
+12 -2
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;
}
}