xref: /OK3568_Linux_fs/buildroot/package/cpio/0002-Rewrite-dynamic-string-support.patch (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1From dd96882877721703e19272fe25034560b794061b Mon Sep 17 00:00:00 2001
2From: Sergey Poznyakoff <gray@gnu.org>
3Date: Sat, 7 Aug 2021 12:52:21 +0300
4Subject: Rewrite dynamic string support.
5
6* src/dstring.c (ds_init): Take a single argument.
7(ds_free): New function.
8(ds_resize): Take a single argument.  Use x2nrealloc to expand
9the storage.
10(ds_reset,ds_append,ds_concat,ds_endswith): New function.
11(ds_fgetstr): Rewrite.  In particular, this fixes integer overflow.
12* src/dstring.h (dynamic_string): Keep both the allocated length
13(ds_size) and index of the next free byte in the string (ds_idx).
14(ds_init,ds_resize): Change signature.
15(ds_len): New macro.
16(ds_free,ds_reset,ds_append,ds_concat,ds_endswith): New protos.
17* src/copyin.c: Use new ds_ functions.
18* src/copyout.c: Likewise.
19* src/copypass.c: Likewise.
20* src/util.c: Likewise.
21
22[Retrieved from:
23https://git.savannah.gnu.org/cgit/cpio.git/commit/?id=dd96882877721703e19272fe25034560b794061b]
24Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
25---
26 src/copyin.c   | 40 +++++++++++++-------------
27 src/copyout.c  | 16 ++++-------
28 src/copypass.c | 34 +++++++++++------------
29 src/dstring.c  | 88 ++++++++++++++++++++++++++++++++++++++++++----------------
30 src/dstring.h  | 31 ++++++++++-----------
31 src/util.c     |  6 ++--
32 6 files changed, 123 insertions(+), 92 deletions(-)
33
34diff --git a/src/copyin.c b/src/copyin.c
35index a096048..4fb14af 100644
36--- a/src/copyin.c
37+++ b/src/copyin.c
38@@ -55,11 +55,12 @@ query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
39   char *str_res;		/* Result for string function.  */
40   static dynamic_string new_name;	/* New file name for rename option.  */
41   static int initialized_new_name = false;
42+
43   if (!initialized_new_name)
44-  {
45-    ds_init (&new_name, 128);
46-    initialized_new_name = true;
47-  }
48+    {
49+      ds_init (&new_name);
50+      initialized_new_name = true;
51+    }
52
53   if (rename_flag)
54     {
55@@ -780,37 +781,36 @@ long_format (struct cpio_file_stat *file_hdr, char const *link_name)
56    already in `save_patterns' (from the command line) are preserved.  */
57
58 static void
59-read_pattern_file ()
60+read_pattern_file (void)
61 {
62-  int max_new_patterns;
63-  char **new_save_patterns;
64-  int new_num_patterns;
65+  char **new_save_patterns = NULL;
66+  size_t max_new_patterns;
67+  size_t new_num_patterns;
68   int i;
69-  dynamic_string pattern_name;
70+  dynamic_string pattern_name = DYNAMIC_STRING_INITIALIZER;
71   FILE *pattern_fp;
72
73   if (num_patterns < 0)
74     num_patterns = 0;
75-  max_new_patterns = 1 + num_patterns;
76-  new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
77   new_num_patterns = num_patterns;
78-  ds_init (&pattern_name, 128);
79+  max_new_patterns = num_patterns;
80+  new_save_patterns = xcalloc (max_new_patterns, sizeof (new_save_patterns[0]));
81
82   pattern_fp = fopen (pattern_file_name, "r");
83   if (pattern_fp == NULL)
84     open_fatal (pattern_file_name);
85   while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
86     {
87-      if (new_num_patterns >= max_new_patterns)
88-	{
89-	  max_new_patterns += 1;
90-	  new_save_patterns = (char **)
91-	    xrealloc ((char *) new_save_patterns,
92-		      max_new_patterns * sizeof (char *));
93-	}
94+      if (new_num_patterns == max_new_patterns)
95+	new_save_patterns = x2nrealloc (new_save_patterns,
96+					&max_new_patterns,
97+					sizeof (new_save_patterns[0]));
98       new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
99       ++new_num_patterns;
100     }
101+
102+  ds_free (&pattern_name);
103+
104   if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
105     close_error (pattern_file_name);
106
107@@ -1210,7 +1210,7 @@ swab_array (char *ptr, int count)
108    in the file system.  */
109
110 void
111-process_copy_in ()
112+process_copy_in (void)
113 {
114   FILE *tty_in = NULL;		/* Interactive file for rename option.  */
115   FILE *tty_out = NULL;		/* Interactive file for rename option.  */
116diff --git a/src/copyout.c b/src/copyout.c
117index 5ca587f..ca6798c 100644
118--- a/src/copyout.c
119+++ b/src/copyout.c
120@@ -594,9 +594,10 @@ assign_string (char **pvar, char *value)
121    The format of the header depends on the compatibility (-c) flag.  */
122
123 void
124-process_copy_out ()
125+process_copy_out (void)
126 {
127-  dynamic_string input_name;	/* Name of file read from stdin.  */
128+  dynamic_string input_name = DYNAMIC_STRING_INITIALIZER;
129+                                /* Name of file read from stdin.  */
130   struct stat file_stat;	/* Stat record for file.  */
131   struct cpio_file_stat file_hdr = CPIO_FILE_STAT_INITIALIZER;
132                                 /* Output header information.  */
133@@ -605,7 +606,6 @@ process_copy_out ()
134   char *orig_file_name = NULL;
135
136   /* Initialize the copy out.  */
137-  ds_init (&input_name, 128);
138   file_hdr.c_magic = 070707;
139
140   /* Check whether the output file might be a tape.  */
141@@ -657,14 +657,9 @@ process_copy_out ()
142 	    {
143 	      if (file_hdr.c_mode & CP_IFDIR)
144 		{
145-		  int len = strlen (input_name.ds_string);
146 		  /* Make sure the name ends with a slash */
147-		  if (input_name.ds_string[len-1] != '/')
148-		    {
149-		      ds_resize (&input_name, len + 2);
150-		      input_name.ds_string[len] = '/';
151-		      input_name.ds_string[len+1] = 0;
152-		    }
153+		  if (!ds_endswith (&input_name, '/'))
154+		    ds_append (&input_name, '/');
155 		}
156 	    }
157
158@@ -875,6 +870,7 @@ process_copy_out ()
159 			 (unsigned long) blocks), (unsigned long) blocks);
160     }
161   cpio_file_stat_free (&file_hdr);
162+  ds_free (&input_name);
163 }
164
165
166diff --git a/src/copypass.c b/src/copypass.c
167index 5d5e939..23ee687 100644
168--- a/src/copypass.c
169+++ b/src/copypass.c
170@@ -48,10 +48,12 @@ set_copypass_perms (int fd, const char *name, struct stat *st)
171    If `link_flag', link instead of copying.  */
172
173 void
174-process_copy_pass ()
175+process_copy_pass (void)
176 {
177-  dynamic_string input_name;	/* Name of file from stdin.  */
178-  dynamic_string output_name;	/* Name of new file.  */
179+  dynamic_string input_name = DYNAMIC_STRING_INITIALIZER;
180+                                /* Name of file from stdin.  */
181+  dynamic_string output_name = DYNAMIC_STRING_INITIALIZER;
182+                                /* Name of new file.  */
183   size_t dirname_len;		/* Length of `directory_name'.  */
184   int res;			/* Result of functions.  */
185   char *slash;			/* For moving past slashes in input name.  */
186@@ -65,25 +67,18 @@ process_copy_pass ()
187 				   created files  */
188
189   /* Initialize the copy pass.  */
190-  ds_init (&input_name, 128);
191
192   dirname_len = strlen (directory_name);
193   if (change_directory_option && !ISSLASH (directory_name[0]))
194     {
195       char *pwd = xgetcwd ();
196-
197-      dirname_len += strlen (pwd) + 1;
198-      ds_init (&output_name, dirname_len + 2);
199-      strcpy (output_name.ds_string, pwd);
200-      strcat (output_name.ds_string, "/");
201-      strcat (output_name.ds_string, directory_name);
202+
203+      ds_concat (&output_name, pwd);
204+      ds_append (&output_name, '/');
205     }
206-  else
207-    {
208-      ds_init (&output_name, dirname_len + 2);
209-      strcpy (output_name.ds_string, directory_name);
210-    }
211-  output_name.ds_string[dirname_len] = '/';
212+  ds_concat (&output_name, directory_name);
213+  ds_append (&output_name, '/');
214+  dirname_len = ds_len (&output_name);
215   output_is_seekable = true;
216
217   change_dir ();
218@@ -116,8 +111,8 @@ process_copy_pass ()
219       /* Make the name of the new file.  */
220       for (slash = input_name.ds_string; *slash == '/'; ++slash)
221 	;
222-      ds_resize (&output_name, dirname_len + strlen (slash) + 2);
223-      strcpy (output_name.ds_string + dirname_len + 1, slash);
224+      ds_reset (&output_name, dirname_len);
225+      ds_concat (&output_name, slash);
226
227       existing_dir = false;
228       if (lstat (output_name.ds_string, &out_file_stat) == 0)
229@@ -333,6 +328,9 @@ process_copy_pass ()
230 			 (unsigned long) blocks),
231 	       (unsigned long) blocks);
232     }
233+
234+  ds_free (&input_name);
235+  ds_free (&output_name);
236 }
237
238 /* Try and create a hard link from FILE_NAME to another file
239diff --git a/src/dstring.c b/src/dstring.c
240index b261d5a..692d3e7 100644
241--- a/src/dstring.c
242+++ b/src/dstring.c
243@@ -20,8 +20,8 @@
244 #if defined(HAVE_CONFIG_H)
245 # include <config.h>
246 #endif
247-
248 #include <stdio.h>
249+#include <stdlib.h>
250 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
251 #include <string.h>
252 #else
253@@ -33,24 +33,41 @@
254 /* Initialiaze dynamic string STRING with space for SIZE characters.  */
255
256 void
257-ds_init (dynamic_string *string, int size)
258+ds_init (dynamic_string *string)
259+{
260+  memset (string, 0, sizeof *string);
261+}
262+
263+/* Free the dynamic string storage. */
264+
265+void
266+ds_free (dynamic_string *string)
267 {
268-  string->ds_length = size;
269-  string->ds_string = (char *) xmalloc (size);
270+  free (string->ds_string);
271 }
272
273-/* Expand dynamic string STRING, if necessary, to hold SIZE characters.  */
274+/* Expand dynamic string STRING, if necessary.  */
275
276 void
277-ds_resize (dynamic_string *string, int size)
278+ds_resize (dynamic_string *string)
279 {
280-  if (size > string->ds_length)
281+  if (string->ds_idx == string->ds_size)
282     {
283-      string->ds_length = size;
284-      string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
285+      string->ds_string = x2nrealloc (string->ds_string, &string->ds_size,
286+				      1);
287     }
288 }
289
290+/* Reset the index of the dynamic string S to LEN. */
291+
292+void
293+ds_reset (dynamic_string *s, size_t len)
294+{
295+  while (len > s->ds_size)
296+    ds_resize (s);
297+  s->ds_idx = len;
298+}
299+
300 /* Dynamic string S gets a string terminated by the EOS character
301    (which is removed) from file F.  S will increase
302    in size during the function if the string from F is longer than
303@@ -61,34 +78,50 @@ ds_resize (dynamic_string *string, int size)
304 char *
305 ds_fgetstr (FILE *f, dynamic_string *s, char eos)
306 {
307-  int insize;			/* Amount needed for line.  */
308-  int strsize;			/* Amount allocated for S.  */
309   int next_ch;
310
311   /* Initialize.  */
312-  insize = 0;
313-  strsize = s->ds_length;
314+  s->ds_idx = 0;
315
316   /* Read the input string.  */
317-  next_ch = getc (f);
318-  while (next_ch != eos && next_ch != EOF)
319+  while ((next_ch = getc (f)) != eos && next_ch != EOF)
320     {
321-      if (insize >= strsize - 1)
322-	{
323-	  ds_resize (s, strsize * 2 + 2);
324-	  strsize = s->ds_length;
325-	}
326-      s->ds_string[insize++] = next_ch;
327-      next_ch = getc (f);
328+      ds_resize (s);
329+      s->ds_string[s->ds_idx++] = next_ch;
330     }
331-  s->ds_string[insize++] = '\0';
332+  ds_resize (s);
333+  s->ds_string[s->ds_idx] = '\0';
334
335-  if (insize == 1 && next_ch == EOF)
336+  if (s->ds_idx == 0 && next_ch == EOF)
337     return NULL;
338   else
339     return s->ds_string;
340 }
341
342+void
343+ds_append (dynamic_string *s, int c)
344+{
345+  ds_resize (s);
346+  s->ds_string[s->ds_idx] = c;
347+  if (c)
348+    {
349+      s->ds_idx++;
350+      ds_resize (s);
351+      s->ds_string[s->ds_idx] = 0;
352+    }
353+}
354+
355+void
356+ds_concat (dynamic_string *s, char const *str)
357+{
358+  size_t len = strlen (str);
359+  while (len + 1 > s->ds_size)
360+    ds_resize (s);
361+  memcpy (s->ds_string + s->ds_idx, str, len);
362+  s->ds_idx += len;
363+  s->ds_string[s->ds_idx] = 0;
364+}
365+
366 char *
367 ds_fgets (FILE *f, dynamic_string *s)
368 {
369@@ -100,3 +133,10 @@ ds_fgetname (FILE *f, dynamic_string *s)
370 {
371   return ds_fgetstr (f, s, '\0');
372 }
373+
374+/* Return true if the dynamic string S ends with character C. */
375+int
376+ds_endswith (dynamic_string *s, int c)
377+{
378+  return (s->ds_idx > 0 && s->ds_string[s->ds_idx - 1] == c);
379+}
380diff --git a/src/dstring.h b/src/dstring.h
381index 5d24181..ca7a5f1 100644
382--- a/src/dstring.h
383+++ b/src/dstring.h
384@@ -17,10 +17,6 @@
385    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
386    Boston, MA 02110-1301 USA.  */
387
388-#ifndef NULL
389-#define NULL 0
390-#endif
391-
392 /* A dynamic string consists of record that records the size of an
393    allocated string and the pointer to that string.  The actual string
394    is a normal zero byte terminated string that can be used with the
395@@ -30,22 +26,25 @@
396
397 typedef struct
398 {
399-  int ds_length;		/* Actual amount of storage allocated.  */
400-  char *ds_string;		/* String.  */
401+  size_t ds_size;   /* Actual amount of storage allocated.  */
402+  size_t ds_idx;    /* Index of the next free byte in the string. */
403+  char *ds_string;  /* String storage. */
404 } dynamic_string;
405
406+#define DYNAMIC_STRING_INITIALIZER { 0, 0, NULL }
407
408-/* Macros that look similar to the original string functions.
409-   WARNING:  These macros work only on pointers to dynamic string records.
410-   If used with a real record, an "&" must be used to get the pointer.  */
411-#define ds_strlen(s)		strlen ((s)->ds_string)
412-#define ds_strcmp(s1, s2)	strcmp ((s1)->ds_string, (s2)->ds_string)
413-#define ds_strncmp(s1, s2, n)	strncmp ((s1)->ds_string, (s2)->ds_string, n)
414-#define ds_index(s, c)		index ((s)->ds_string, c)
415-#define ds_rindex(s, c)		rindex ((s)->ds_string, c)
416+void ds_init (dynamic_string *string);
417+void ds_free (dynamic_string *string);
418+void ds_reset (dynamic_string *s, size_t len);
419
420-void ds_init (dynamic_string *string, int size);
421-void ds_resize (dynamic_string *string, int size);
422+/* All functions below guarantee that s->ds_string[s->ds_idx] == '\0' */
423 char *ds_fgetname (FILE *f, dynamic_string *s);
424 char *ds_fgets (FILE *f, dynamic_string *s);
425 char *ds_fgetstr (FILE *f, dynamic_string *s, char eos);
426+void ds_append (dynamic_string *s, int c);
427+void ds_concat (dynamic_string *s, char const *str);
428+
429+#define ds_len(s) ((s)->ds_idx)
430+
431+int ds_endswith (dynamic_string *s, int c);
432+
433diff --git a/src/util.c b/src/util.c
434index 996d4fa..ff2746d 100644
435--- a/src/util.c
436+++ b/src/util.c
437@@ -846,11 +846,9 @@ get_next_reel (int tape_des)
438   FILE *tty_out;		/* File for interacting with user.  */
439   int old_tape_des;
440   char *next_archive_name;
441-  dynamic_string new_name;
442+  dynamic_string new_name = DYNAMIC_STRING_INITIALIZER;
443   char *str_res;
444
445-  ds_init (&new_name, 128);
446-
447   /* Open files for interactive communication.  */
448   tty_in = fopen (TTY_NAME, "r");
449   if (tty_in == NULL)
450@@ -925,7 +923,7 @@ get_next_reel (int tape_des)
451     error (PAXEXIT_FAILURE, 0, _("internal error: tape descriptor changed from %d to %d"),
452 	   old_tape_des, tape_des);
453
454-  free (new_name.ds_string);
455+  ds_free (&new_name);
456   fclose (tty_in);
457   fclose (tty_out);
458 }
459--
460cgit v1.2.1
461
462