1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
From: Sergey Poznyakoff <gray@gnu.org>
Date: Wed, 18 Aug 2021 09:41:39 +0300
Subject: Fix dynamic string reallocations
Origin: https://git.savannah.gnu.org/cgit/cpio.git/commit/?id=236684f6deb3178043fe72a8e2faca538fa2aae1
Bug: https://lists.gnu.org/archive/html/bug-cpio/2021-08/msg00005.html
Bug-Debian: https://bugs.debian.org/992192
* src/dstring.c (ds_resize): Take additional argument: number of
bytes to leave available after ds_idx. All uses changed.
---
src/dstring.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/src/dstring.c b/src/dstring.c
index b7e0bb5b5ec1..fd4e03067c25 100644
--- a/src/dstring.c
+++ b/src/dstring.c
@@ -49,9 +49,9 @@ ds_free (dynamic_string *string)
/* Expand dynamic string STRING, if necessary. */
void
-ds_resize (dynamic_string *string)
+ds_resize (dynamic_string *string, size_t len)
{
- if (string->ds_idx == string->ds_size)
+ while (len + string->ds_idx >= string->ds_size)
{
string->ds_string = x2nrealloc (string->ds_string, &string->ds_size,
1);
@@ -63,8 +63,7 @@ ds_resize (dynamic_string *string)
void
ds_reset (dynamic_string *s, size_t len)
{
- while (len > s->ds_size)
- s->ds_string = x2nrealloc (s->ds_string, &s->ds_size, 1);
+ ds_resize (s, len);
s->ds_idx = len;
}
@@ -86,10 +85,10 @@ ds_fgetstr (FILE *f, dynamic_string *s, char eos)
/* Read the input string. */
while ((next_ch = getc (f)) != eos && next_ch != EOF)
{
- ds_resize (s);
+ ds_resize (s, 0);
s->ds_string[s->ds_idx++] = next_ch;
}
- ds_resize (s);
+ ds_resize (s, 0);
s->ds_string[s->ds_idx] = '\0';
if (s->ds_idx == 0 && next_ch == EOF)
@@ -101,12 +100,12 @@ ds_fgetstr (FILE *f, dynamic_string *s, char eos)
void
ds_append (dynamic_string *s, int c)
{
- ds_resize (s);
+ ds_resize (s, 0);
s->ds_string[s->ds_idx] = c;
if (c)
{
s->ds_idx++;
- ds_resize (s);
+ ds_resize (s, 0);
s->ds_string[s->ds_idx] = 0;
}
}
@@ -115,8 +114,7 @@ void
ds_concat (dynamic_string *s, char const *str)
{
size_t len = strlen (str);
- while (len + 1 > s->ds_size)
- s->ds_string = x2nrealloc (s->ds_string, &s->ds_size, 1);
+ ds_resize (s, len);
memcpy (s->ds_string + s->ds_idx, str, len);
s->ds_idx += len;
s->ds_string[s->ds_idx] = 0;
--
2.33.0
|