Consistently use x2realloc for buffer reallocation

* src/create.c (create_archive): Use x2realloc
* src/names.c (copy_name)
(add_hierarchy_to_namelist): Use x2realloc
This commit is contained in:
Sergey Poznyakoff
2019-08-25 14:41:17 +03:00
parent 2d3396c3ea
commit ea6f84dd40
2 changed files with 14 additions and 42 deletions

View File

@@ -1355,8 +1355,8 @@ create_archive (void)
if (incremental_option)
{
size_t buffer_size = 1000;
char *buffer = xmalloc (buffer_size);
size_t buffer_size = 0;
char *buffer = NULL;
const char *q;
collect_and_sort_names ();
@@ -1371,12 +1371,8 @@ create_archive (void)
{
struct tar_stat_info st;
size_t plen = strlen (p->name);
if (buffer_size <= plen)
{
while ((buffer_size *= 2) <= plen)
continue;
buffer = xrealloc (buffer, buffer_size);
}
while (buffer_size <= plen)
buffer = x2realloc (buffer, &buffer_size);
memcpy (buffer, p->name, plen);
if (! ISSLASH (buffer[plen - 1]))
buffer[plen++] = DIRECTORY_SEPARATOR;
@@ -1407,12 +1403,8 @@ create_archive (void)
}
st.orig_file_name = xstrdup (p->name);
}
if (buffer_size < plen + qlen)
{
while ((buffer_size *=2 ) < plen + qlen)
continue;
buffer = xrealloc (buffer, buffer_size);
}
while (buffer_size < plen + qlen)
buffer = x2realloc (buffer, &buffer_size);
strcpy (buffer + plen, q + 1);
dump_file (&st, q + 1, buffer);
}

View File

@@ -1058,19 +1058,8 @@ copy_name (struct name_elt *ep)
source = ep->v.name;
source_len = strlen (source);
if (name_buffer_length < source_len)
{
do
{
name_buffer_length *= 2;
if (! name_buffer_length)
xalloc_die ();
}
while (name_buffer_length < source_len);
free (name_buffer);
name_buffer = xmalloc(name_buffer_length + 2);
}
while (name_buffer_length <= source_len)
name_buffer = x2realloc(name_buffer, &name_buffer_length);
strcpy (name_buffer, source);
chopslash (name_buffer);
}
@@ -1591,9 +1580,8 @@ add_hierarchy_to_namelist (struct tar_stat_info *st, struct name *name)
size_t name_length = name->length;
size_t allocated_length = (name_length >= NAME_FIELD_SIZE
? name_length + NAME_FIELD_SIZE
: NAME_FIELD_SIZE);
char *namebuf = xmalloc (allocated_length + 1);
/* FIXME: + 2 above? */
: NAME_FIELD_SIZE) + 2;
char *namebuf = xmalloc (allocated_length);
const char *string;
size_t string_length;
int change_dir = name->change_dir;
@@ -1614,18 +1602,10 @@ add_hierarchy_to_namelist (struct tar_stat_info *st, struct name *name)
struct tar_stat_info subdir;
int subfd;
if (allocated_length <= name_length + string_length)
{
do
{
allocated_length *= 2;
if (! allocated_length)
xalloc_die ();
}
while (allocated_length <= name_length + string_length);
namebuf = xrealloc (namebuf, allocated_length + 1);
}
/* need to have at least string_length bytes above the
name_length, this includes the trailing null character */
while (allocated_length < name_length + string_length)
namebuf = x2realloc (namebuf, &allocated_length);
strcpy (namebuf + name_length, string + 1);
np = addname (namebuf, change_dir, false, name);
if (!child_head)