da5b55
From be54882039632c791493d3657042f7ea9d6f4a20 Mon Sep 17 00:00:00 2001
da5b55
From: Ondrej Dubaj <odubaj@redhat.com>
da5b55
Date: Tue, 21 Sep 2021 11:42:02 +0200
da5b55
Subject: [PATCH] * src/dstring.c (ds_init): Take a single argument. 
da5b55
 (ds_free):  New function. (ds_resize): Take a single argument.  Use 
da5b55
 x2nrealloc to expand  the storage. 
da5b55
 (ds_reset,ds_append,ds_concat,ds_endswith): New function.  (ds_fgetstr): 
da5b55
 Rewrite.  In particular, this fixes integer overflow.  (ds_resize): Take 
da5b55
 additional argument: number of  bytes to leave available after ds_idx.  All 
da5b55
 uses changed. * src/dstring.h (dynamic_string): Keep both the allocated 
da5b55
 length (ds_size) and  index of the  next free byte in the string (ds_idx). 
da5b55
 (ds_init,ds_resize):  Change  signature. (ds_len): New macro. 
da5b55
 (ds_free,ds_reset,ds_append,ds_concat,ds_endswith): New protos. * 
da5b55
 src/copyin.c: Use new ds_ functions. (read_name_from_file): Handle len == 0. 
da5b55
 (read_name_from_file): Print error message and skip file if its name is not 
da5b55
 nul-terminated. (long_format): Cast rdev numbers to unsigned long * 
da5b55
 src/copyout.c: Likewise. * src/copypass.c: Likewise. * src/util.c: Likewise. 
da5b55
 (tape_empty_output_buffer): Fix condition. * src/idcache.c 
da5b55
 (getuser,getgroup): Use umaxtostr instead of sprintf. * src/userspec.c 
da5b55
 (parse_user_spec): Likewise.
da5b55
da5b55
---
da5b55
 configure.ac   |   4 +-
da5b55
 src/copyin.c   | 228 ++++++++++++++++++-------------------------------
da5b55
 src/copyout.c  |  77 +++++++++--------
da5b55
 src/copypass.c |  34 ++++----
da5b55
 src/cpiohdr.h  |   9 +-
da5b55
 src/dstring.c  |  89 +++++++++++++------
da5b55
 src/dstring.h  |  30 +++----
da5b55
 src/extern.h   |  22 +++--
da5b55
 src/idcache.c  |  11 ++-
da5b55
 src/makepath.c |   2 +-
da5b55
 src/userspec.c |   9 +-
da5b55
 src/util.c     |  53 +++++++++---
da5b55
 12 files changed, 294 insertions(+), 274 deletions(-)
da5b55
da5b55
diff --git a/configure.ac b/configure.ac
da5b55
index c68bd44..49eaacd 100644
da5b55
--- a/configure.ac
da5b55
+++ b/configure.ac
da5b55
@@ -21,8 +21,8 @@ AC_INIT([GNU cpio], [2.12], [bug-cpio@gnu.org],,
da5b55
 AC_CONFIG_SRCDIR(src/cpio.h)
da5b55
 AC_CONFIG_AUX_DIR([build-aux])
da5b55
 AC_CONFIG_HEADERS([config.h])
da5b55
-AC_PREREQ([2.63])
da5b55
-AM_INIT_AUTOMAKE([1.11.1 gnits tar-ustar dist-bzip2 std-options silent-rules])
da5b55
+AC_PREREQ([2.64])
da5b55
+AM_INIT_AUTOMAKE([1.15 gnits tar-ustar dist-bzip2 std-options silent-rules])
da5b55
 
da5b55
 # Enable silent rules by default:
da5b55
 AM_SILENT_RULES([yes])
da5b55
diff --git a/src/copyin.c b/src/copyin.c
da5b55
index 267ed4b..2f9da73 100644
da5b55
--- a/src/copyin.c
da5b55
+++ b/src/copyin.c
da5b55
@@ -56,10 +56,10 @@ query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
da5b55
   static dynamic_string new_name;	/* New file name for rename option.  */
da5b55
   static int initialized_new_name = false;
da5b55
   if (!initialized_new_name)
da5b55
-  {
da5b55
-    ds_init (&new_name, 128);
da5b55
-    initialized_new_name = true;
da5b55
-  }
da5b55
+    {
da5b55
+      ds_init (&new_name);
da5b55
+      initialized_new_name = true;
da5b55
+    }
da5b55
 
da5b55
   if (rename_flag)
da5b55
     {
da5b55
@@ -76,28 +76,7 @@ query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
da5b55
       return -1;
da5b55
     }
da5b55
   else
da5b55
-  /* Debian hack: file_hrd.c_name is sometimes set to
da5b55
-     point to static memory by code in tar.c.  This
da5b55
-     causes a segfault.  This has been fixed and an
da5b55
-     additional check to ensure that the file name
da5b55
-     is not too long has been added.  (Reported by
da5b55
-     Horst Knobloch.)  This bug has been reported to
da5b55
-     "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
da5b55
-    {
da5b55
-      if (archive_format != arf_tar && archive_format != arf_ustar)
da5b55
-	{
da5b55
-	  free (file_hdr->c_name);
da5b55
-	  file_hdr->c_name = xstrdup (new_name.ds_string);
da5b55
-	}
da5b55
-      else
da5b55
-	{
da5b55
-	  if (is_tar_filename_too_long (new_name.ds_string))
da5b55
-	    error (0, 0, _("%s: file name too long"),
da5b55
-		   new_name.ds_string);
da5b55
-	  else
da5b55
-	    strcpy (file_hdr->c_name, new_name.ds_string);
da5b55
-	}
da5b55
-    }
da5b55
+    cpio_set_c_name (file_hdr, new_name.ds_string);
da5b55
   return 0;
da5b55
 }
da5b55
 
da5b55
@@ -173,10 +152,8 @@ list_file (struct cpio_file_stat* file_hdr, int in_file_des)
da5b55
     }
da5b55
   else
da5b55
     {
da5b55
-      /* Debian hack: Modified to print a list of filenames
da5b55
-	 terminiated by a null character when the -t and -0
da5b55
-	 flags are used.  This has been submitted as a
da5b55
-	 suggestion to "bug-gnu-utils@prep.ai.mit.edu".  -BEM */
da5b55
+      /* Print out the name as it is.  The name_end delimiter is normally
da5b55
+	 '\n', but can be reset to '\0' by the -0 option. */
da5b55
       printf ("%s%c", file_hdr->c_name, name_end);
da5b55
     }
da5b55
 
da5b55
@@ -201,7 +178,7 @@ list_file (struct cpio_file_stat* file_hdr, int in_file_des)
da5b55
 
da5b55
 static int
da5b55
 try_existing_file (struct cpio_file_stat* file_hdr, int in_file_des,
da5b55
-		   int *existing_dir)
da5b55
+		   bool *existing_dir)
da5b55
 {
da5b55
   struct stat file_stat;
da5b55
 
da5b55
@@ -344,8 +321,7 @@ create_defered_links_to_skipped (struct cpio_file_stat *file_hdr,
da5b55
 	    d_prev->next = d->next;
da5b55
 	  else
da5b55
 	    deferments = d->next;
da5b55
-	  free (file_hdr->c_name);
da5b55
-	  file_hdr->c_name = xstrdup(d->header.c_name);
da5b55
+	  cpio_set_c_name (file_hdr, d->header.c_name);
da5b55
 	  free_deferment (d);
da5b55
 	  copyin_regular_file(file_hdr, in_file_des);
da5b55
 	  return 0;
da5b55
@@ -697,7 +673,7 @@ copyin_link (struct cpio_file_stat *file_hdr, int in_file_des)
da5b55
 static void
da5b55
 copyin_file (struct cpio_file_stat *file_hdr, int in_file_des)
da5b55
 {
da5b55
-  int existing_dir;
da5b55
+  bool existing_dir = false;
da5b55
 
da5b55
   if (!to_stdout_option
da5b55
       && try_existing_file (file_hdr, in_file_des, &existing_dir) < 0)
da5b55
@@ -748,7 +724,7 @@ static time_t current_time;
da5b55
    this file is a symbolic link to.  */
da5b55
 
da5b55
 void
da5b55
-long_format (struct cpio_file_stat *file_hdr, char *link_name)
da5b55
+long_format (struct cpio_file_stat *file_hdr, char const *link_name)
da5b55
 {
da5b55
   char mbuf[11];
da5b55
   char tbuf[40];
da5b55
@@ -780,92 +756,42 @@ long_format (struct cpio_file_stat *file_hdr, char *link_name)
da5b55
 
da5b55
   if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR
da5b55
       || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK)
da5b55
-    printf ("%3lu, %3lu ", file_hdr->c_rdev_maj,
da5b55
-	    file_hdr->c_rdev_min);
da5b55
+    printf ("%3lu, %3lu ",
da5b55
+	    (unsigned long) file_hdr->c_rdev_maj,
da5b55
+	    (unsigned long) file_hdr->c_rdev_min);
da5b55
   else
da5b55
     printf ("%8"PRIuMAX" ", (uintmax_t) file_hdr->c_filesize);
da5b55
 
da5b55
   printf ("%s ", tbuf + 4);
da5b55
 
da5b55
-  print_name_with_quoting (file_hdr->c_name);
da5b55
+  printf ("%s", quotearg (file_hdr->c_name));
da5b55
   if (link_name)
da5b55
     {
da5b55
       printf (" -> ");
da5b55
-      print_name_with_quoting (link_name);
da5b55
+      printf ("%s", quotearg (link_name));
da5b55
     }
da5b55
   putc ('\n', stdout);
da5b55
 }
da5b55
 
da5b55
-void
da5b55
-print_name_with_quoting (register char *p)
da5b55
-{
da5b55
-  register unsigned char c;
da5b55
-
da5b55
-  while ( (c = *p++) )
da5b55
-    {
da5b55
-      switch (c)
da5b55
-	{
da5b55
-	case '\\':
da5b55
-	  printf ("\\\\");
da5b55
-	  break;
da5b55
-
da5b55
-	case '\n':
da5b55
-	  printf ("\\n");
da5b55
-	  break;
da5b55
-
da5b55
-	case '\b':
da5b55
-	  printf ("\\b");
da5b55
-	  break;
da5b55
-
da5b55
-	case '\r':
da5b55
-	  printf ("\\r");
da5b55
-	  break;
da5b55
-
da5b55
-	case '\t':
da5b55
-	  printf ("\\t");
da5b55
-	  break;
da5b55
-
da5b55
-	case '\f':
da5b55
-	  printf ("\\f");
da5b55
-	  break;
da5b55
-
da5b55
-	case ' ':
da5b55
-	  printf ("\\ ");
da5b55
-	  break;
da5b55
-
da5b55
-	case '"':
da5b55
-	  printf ("\\\"");
da5b55
-	  break;
da5b55
-
da5b55
-	default:
da5b55
-	  if (c > 040 && c < 0177)
da5b55
-	    putchar (c);
da5b55
-	  else
da5b55
-	    printf ("\\%03o", (unsigned int) c);
da5b55
-	}
da5b55
-    }
da5b55
-}
da5b55
-
da5b55
 /* Read a pattern file (for the -E option).  Put a list of
da5b55
    `num_patterns' elements in `save_patterns'.  Any patterns that were
da5b55
    already in `save_patterns' (from the command line) are preserved.  */
da5b55
 
da5b55
 static void
da5b55
-read_pattern_file ()
da5b55
+read_pattern_file (void)
da5b55
 {
da5b55
-  int max_new_patterns;
da5b55
-  char **new_save_patterns;
da5b55
-  int new_num_patterns;
da5b55
+  char **new_save_patterns = NULL;
da5b55
+  size_t max_new_patterns;
da5b55
+  size_t new_num_patterns;
da5b55
   int i;
da5b55
-  dynamic_string pattern_name;
da5b55
+  dynamic_string pattern_name = DYNAMIC_STRING_INITIALIZER;
da5b55
   FILE *pattern_fp;
da5b55
 
da5b55
   if (num_patterns < 0)
da5b55
     num_patterns = 0;
da5b55
-  max_new_patterns = 1 + num_patterns;
da5b55
-  new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
da5b55
   new_num_patterns = num_patterns;
da5b55
-  ds_init (&pattern_name, 128);
da5b55
+  max_new_patterns = num_patterns;
da5b55
+  new_save_patterns = xcalloc (max_new_patterns, sizeof (new_save_patterns[0]));
da5b55
 
da5b55
   pattern_fp = fopen (pattern_file_name, "r");
da5b55
   if (pattern_fp == NULL)
da5b55
@@ -874,16 +800,16 @@ read_pattern_file ()
da5b55
   {
da5b55
     while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
da5b55
       {
da5b55
-        if (new_num_patterns >= max_new_patterns)
da5b55
-        {
da5b55
-          max_new_patterns += 1;
da5b55
-          new_save_patterns = (char **)
da5b55
-          xrealloc ((char *) new_save_patterns,
da5b55
-            max_new_patterns * sizeof (char *));
da5b55
-        }
da5b55
+        if (new_num_patterns == max_new_patterns)
da5b55
+          new_save_patterns = x2nrealloc (new_save_patterns,
da5b55
+                  &max_new_patterns,
da5b55
+                  sizeof (new_save_patterns[0]));
da5b55
         new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
da5b55
         ++new_num_patterns;
da5b55
       }
da5b55
+
da5b55
+    ds_free (&pattern_name);
da5b55
+
da5b55
     if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
da5b55
       close_error (pattern_file_name);
da5b55
   }
da5b55
@@ -1066,6 +992,27 @@ read_in_header (struct cpio_file_stat *file_hdr, int in_des)
da5b55
     }
da5b55
 }
da5b55
 
da5b55
+static void
da5b55
+read_name_from_file (struct cpio_file_stat *file_hdr, int fd, uintmax_t len)
da5b55
+{
da5b55
+  if (len == 0)
da5b55
+    {
da5b55
+      error (0, 0, _("malformed header: file name of zero length"));
da5b55
+    }
da5b55
+  else
da5b55
+    {
da5b55
+      cpio_realloc_c_name (file_hdr, len);
da5b55
+      tape_buffered_read (file_hdr->c_name, fd, len);
da5b55
+      if (file_hdr->c_name[len-1] != 0)
da5b55
+	{
da5b55
+	  error (0, 0, _("malformed header: file name is not nul-terminated"));
da5b55
+	  /* Skip this file */
da5b55
+	  len = 0;
da5b55
+	}
da5b55
+     }
da5b55
+  file_hdr->c_namesize = len;
da5b55
+}
da5b55
+
da5b55
 /* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
da5b55
    file descriptor IN_DES, except for the magic number, which is
da5b55
    already filled in.  */
da5b55
@@ -1092,14 +1039,9 @@ read_in_old_ascii (struct cpio_file_stat *file_hdr, int in_des)
da5b55
   file_hdr->c_rdev_min = minor (dev);
da5b55
 
da5b55
   file_hdr->c_mtime = FROM_OCTAL (ascii_header.c_mtime);
da5b55
-  file_hdr->c_namesize = FROM_OCTAL (ascii_header.c_namesize);
da5b55
   file_hdr->c_filesize = FROM_OCTAL (ascii_header.c_filesize);
da5b55
   
da5b55
-  /* Read file name from input.  */
da5b55
-  if (file_hdr->c_name != NULL)
da5b55
-    free (file_hdr->c_name);
da5b55
-  file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
da5b55
-  tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
da5b55
+  read_name_from_file (file_hdr, in_des, FROM_OCTAL (ascii_header.c_namesize));
da5b55
 
da5b55
   /* HP/UX cpio creates archives that look just like ordinary archives,
da5b55
      but for devices it sets major = 0, minor = 1, and puts the
da5b55
@@ -1154,14 +1096,9 @@ read_in_new_ascii (struct cpio_file_stat *file_hdr, int in_des)
da5b55
   file_hdr->c_dev_min = FROM_HEX (ascii_header.c_dev_min);
da5b55
   file_hdr->c_rdev_maj = FROM_HEX (ascii_header.c_rdev_maj);
da5b55
   file_hdr->c_rdev_min = FROM_HEX (ascii_header.c_rdev_min);
da5b55
-  file_hdr->c_namesize = FROM_HEX (ascii_header.c_namesize);
da5b55
   file_hdr->c_chksum = FROM_HEX (ascii_header.c_chksum);
da5b55
   
da5b55
-  /* Read file name from input.  */
da5b55
-  if (file_hdr->c_name != NULL)
da5b55
-    free (file_hdr->c_name);
da5b55
-  file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
da5b55
-  tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
da5b55
+  read_name_from_file (file_hdr, in_des, FROM_HEX (ascii_header.c_namesize));
da5b55
 
da5b55
   /* In SVR4 ASCII format, the amount of space allocated for the header
da5b55
      is rounded up to the next long-word, so we might need to drop
da5b55
@@ -1209,16 +1146,10 @@ read_in_binary (struct cpio_file_stat *file_hdr,
da5b55
   file_hdr->c_rdev_min = minor ((unsigned short)short_hdr->c_rdev);
da5b55
   file_hdr->c_mtime = (unsigned long) short_hdr->c_mtimes[0] << 16
da5b55
                       | short_hdr->c_mtimes[1];
da5b55
-
da5b55
-  file_hdr->c_namesize = short_hdr->c_namesize;
da5b55
   file_hdr->c_filesize = (unsigned long) short_hdr->c_filesizes[0] << 16
da5b55
                       | short_hdr->c_filesizes[1];
da5b55
 
da5b55
-  /* Read file name from input.  */
da5b55
-  if (file_hdr->c_name != NULL)
da5b55
-    free (file_hdr->c_name);
da5b55
-  file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
da5b55
-  tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
da5b55
+  read_name_from_file (file_hdr, in_des, short_hdr->c_namesize);
da5b55
 
da5b55
   /* In binary mode, the amount of space allocated in the header for
da5b55
      the filename is `c_namesize' rounded up to the next short-word,
da5b55
@@ -1278,14 +1209,14 @@ swab_array (char *ptr, int count)
da5b55
    in the file system.  */
da5b55
 
da5b55
 void
da5b55
-process_copy_in ()
da5b55
+process_copy_in (void)
da5b55
 {
da5b55
-  char done = false;		/* True if trailer reached.  */
da5b55
   FILE *tty_in = NULL;		/* Interactive file for rename option.  */
da5b55
   FILE *tty_out = NULL;		/* Interactive file for rename option.  */
da5b55
   FILE *rename_in = NULL;	/* Batch file for rename option.  */
da5b55
   struct stat file_stat;	/* Output file stat record.  */
da5b55
-  struct cpio_file_stat file_hdr;	/* Output header information.  */
da5b55
+  struct cpio_file_stat file_hdr = CPIO_FILE_STAT_INITIALIZER;
da5b55
+                                /* Output header information.  */
da5b55
   int in_file_des;		/* Input file descriptor.  */
da5b55
   char skip_file;		/* Flag for use with patterns.  */
da5b55
   int i;			/* Loop index variable.  */
da5b55
@@ -1298,8 +1229,7 @@ process_copy_in ()
da5b55
     {
da5b55
       read_pattern_file ();
da5b55
     }
da5b55
-  file_hdr.c_name = NULL;
da5b55
-
da5b55
+  file_hdr.c_namesize = 0;
da5b55
   if (rename_batch_file)
da5b55
     {
da5b55
       rename_in = fopen (rename_batch_file, "r");
da5b55
@@ -1352,7 +1282,7 @@ process_copy_in ()
da5b55
   change_dir ();
da5b55
   
da5b55
   /* While there is more input in the collection, process the input.  */
da5b55
-  while (!done)
da5b55
+  while (1)
da5b55
     {
da5b55
       swapping_halfwords = swapping_bytes = false;
da5b55
 
da5b55
@@ -1380,30 +1310,32 @@ process_copy_in ()
da5b55
 
da5b55
 	}
da5b55
 #endif
da5b55
-      /* Is this the header for the TRAILER file?  */
da5b55
-      if (strcmp (CPIO_TRAILER_NAME, file_hdr.c_name) == 0)
da5b55
+      if (file_hdr.c_namesize == 0)
da5b55
+	skip_file = true;
da5b55
+      else
da5b55
 	{
da5b55
-	  done = true;
da5b55
-	  break;
da5b55
-	}
da5b55
+	  /* Is this the header for the TRAILER file?  */
da5b55
+	  if (strcmp (CPIO_TRAILER_NAME, file_hdr.c_name) == 0)
da5b55
+	    break;
da5b55
 
da5b55
-      cpio_safer_name_suffix (file_hdr.c_name, false, !no_abs_paths_flag,
da5b55
-			      false);
da5b55
+	  cpio_safer_name_suffix (file_hdr.c_name, false, !no_abs_paths_flag,
da5b55
+				  false);
da5b55
       
da5b55
-      /* Does the file name match one of the given patterns?  */
da5b55
-      if (num_patterns <= 0)
da5b55
-	skip_file = false;
da5b55
-      else
da5b55
-	{
da5b55
-	  skip_file = copy_matching_files;
da5b55
-	  for (i = 0; i < num_patterns
da5b55
-	       && skip_file == copy_matching_files; i++)
da5b55
+	  /* Does the file name match one of the given patterns?  */
da5b55
+	  if (num_patterns <= 0)
da5b55
+	    skip_file = false;
da5b55
+	  else
da5b55
 	    {
da5b55
-	      if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
da5b55
-		skip_file = !copy_matching_files;
da5b55
+	      skip_file = copy_matching_files;
da5b55
+	      for (i = 0; i < num_patterns
da5b55
+		     && skip_file == copy_matching_files; i++)
da5b55
+		{
da5b55
+		  if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
da5b55
+		    skip_file = !copy_matching_files;
da5b55
+		}
da5b55
 	    }
da5b55
 	}
da5b55
-
da5b55
+      
da5b55
       if (skip_file)
da5b55
 	{
da5b55
 	  /* If we're skipping a file with links, there might be other
da5b55
@@ -1494,6 +1426,8 @@ process_copy_in ()
da5b55
     fputc ('\n', stderr);
da5b55
 
da5b55
   apply_delayed_set_stat ();
da5b55
+
da5b55
+  cpio_file_stat_free (&file_hdr);
da5b55
   
da5b55
   if (append_flag)
da5b55
     return;
da5b55
diff --git a/src/copyout.c b/src/copyout.c
da5b55
index 56416ba..a576f27 100644
da5b55
--- a/src/copyout.c
da5b55
+++ b/src/copyout.c
da5b55
@@ -269,26 +269,32 @@ writeout_final_defers (int out_des)
da5b55
    so it should be moved to paxutils too.
da5b55
    Allowed values for logbase are: 1 (binary), 2, 3 (octal), 4 (hex) */
da5b55
 int
da5b55
-to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase)
da5b55
+to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase, bool nul)
da5b55
 {
da5b55
   static char codetab[] = "0123456789ABCDEF";
da5b55
-  int i = digits;
da5b55
   
da5b55
-  do
da5b55
+  if (nul)
da5b55
+    where[--digits] = 0;
da5b55
+  while (digits > 0)
da5b55
     {
da5b55
-      where[--i] = codetab[(v & ((1 << logbase) - 1))];
da5b55
+      where[--digits] = codetab[(v & ((1 << logbase) - 1))];
da5b55
       v >>= logbase;
da5b55
     }
da5b55
-  while (i);
da5b55
 
da5b55
   return v != 0;
da5b55
 }
da5b55
 
da5b55
-static void
da5b55
-field_width_error (const char *filename, const char *fieldname)
da5b55
+void
da5b55
+field_width_error (const char *filename, const char *fieldname,
da5b55
+		   uintmax_t value, size_t width, bool nul)
da5b55
 {
da5b55
-  error (1, 0, _("%s: field width not sufficient for storing %s"),
da5b55
-	 filename, fieldname);
da5b55
+  char valbuf[UINTMAX_STRSIZE_BOUND + 1];
da5b55
+  char maxbuf[UINTMAX_STRSIZE_BOUND + 1];
da5b55
+  error (1, 0, _("%s: value %s %s out of allowed range 0..%s"),
da5b55
+	 filename, fieldname,
da5b55
+	 STRINGIFY_BIGINT (value, valbuf),
da5b55
+	 STRINGIFY_BIGINT (MAX_VAL_WITH_DIGITS (width - nul, LG_8),
da5b55
+			   maxbuf));
da5b55
 }
da5b55
 
da5b55
 static void
da5b55
@@ -303,7 +309,7 @@ to_ascii_or_warn (char *where, uintmax_t n, size_t digits,
da5b55
 		  unsigned logbase,
da5b55
 		  const char *filename, const char *fieldname)
da5b55
 {
da5b55
-  if (to_ascii (where, n, digits, logbase))
da5b55
+  if (to_ascii (where, n, digits, logbase, false))
da5b55
     field_width_warning (filename, fieldname);
da5b55
 }    
da5b55
 
da5b55
@@ -312,9 +318,9 @@ to_ascii_or_error (char *where, uintmax_t n, size_t digits,
da5b55
 		   unsigned logbase,
da5b55
 		   const char *filename, const char *fieldname)
da5b55
 {
da5b55
-  if (to_ascii (where, n, digits, logbase))
da5b55
+  if (to_ascii (where, n, digits, logbase, false))
da5b55
     {
da5b55
-      field_width_error (filename, fieldname);
da5b55
+      field_width_error (filename, fieldname, n, digits, false);
da5b55
       return 1;
da5b55
     }
da5b55
   return 0;
da5b55
@@ -371,7 +377,7 @@ write_out_new_ascii_header (const char *magic_string,
da5b55
 			 _("name size")))
da5b55
     return 1;
da5b55
   p += 8;
da5b55
-  to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16);
da5b55
+  to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16, false);
da5b55
 
da5b55
   tape_buffered_write (ascii_header, out_des, sizeof ascii_header);
da5b55
 
da5b55
@@ -388,7 +394,7 @@ write_out_old_ascii_header (dev_t dev, dev_t rdev,
da5b55
   char ascii_header[76];
da5b55
   char *p = ascii_header;
da5b55
   
da5b55
-  to_ascii (p, file_hdr->c_magic, 6, LG_8);
da5b55
+  to_ascii (p, file_hdr->c_magic, 6, LG_8, false);
da5b55
   p += 6;
da5b55
   to_ascii_or_warn (p, dev, 6, LG_8, file_hdr->c_name, _("device number"));
da5b55
   p += 6;
da5b55
@@ -492,7 +498,10 @@ write_out_binary_header (dev_t rdev,
da5b55
   short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF;
da5b55
   if (short_hdr.c_namesize != file_hdr->c_namesize)
da5b55
     {
da5b55
-      field_width_error (file_hdr->c_name, _("name size"));
da5b55
+      char maxbuf[UINTMAX_STRSIZE_BOUND + 1];
da5b55
+      error (1, 0, _("%s: value %s %s out of allowed range 0..%u"),
da5b55
+	     file_hdr->c_name, _("name size"),
da5b55
+	     STRINGIFY_BIGINT (file_hdr->c_namesize, maxbuf), 0xFFFFu);
da5b55
       return 1;
da5b55
     }
da5b55
 		      
da5b55
@@ -502,7 +511,10 @@ write_out_binary_header (dev_t rdev,
da5b55
   if (((off_t)short_hdr.c_filesizes[0] << 16) + short_hdr.c_filesizes[1]
da5b55
        != file_hdr->c_filesize)
da5b55
     {
da5b55
-      field_width_error (file_hdr->c_name, _("file size"));
da5b55
+      char maxbuf[UINTMAX_STRSIZE_BOUND + 1];
da5b55
+      error (1, 0, _("%s: value %s %s out of allowed range 0..%lu"),
da5b55
+	     file_hdr->c_name, _("file size"),
da5b55
+	     STRINGIFY_BIGINT (file_hdr->c_namesize, maxbuf), 0xFFFFFFFFlu);
da5b55
       return 1;
da5b55
     }
da5b55
 		      
da5b55
@@ -582,17 +594,18 @@ assign_string (char **pvar, char *value)
da5b55
    The format of the header depends on the compatibility (-c) flag.  */
da5b55
 
da5b55
 void
da5b55
-process_copy_out ()
da5b55
+process_copy_out (void)
da5b55
 {
da5b55
-  dynamic_string input_name;	/* Name of file read from stdin.  */
da5b55
+  dynamic_string input_name = DYNAMIC_STRING_INITIALIZER;
da5b55
+                                /* Name of file read from stdin.  */
da5b55
   struct stat file_stat;	/* Stat record for file.  */
da5b55
-  struct cpio_file_stat file_hdr; /* Output header information.  */
da5b55
+  struct cpio_file_stat file_hdr = CPIO_FILE_STAT_INITIALIZER;
da5b55
+                                /* Output header information.  */
da5b55
   int in_file_des;		/* Source file descriptor.  */
da5b55
   int out_file_des;		/* Output file descriptor.  */
da5b55
   char *orig_file_name = NULL;
da5b55
 
da5b55
   /* Initialize the copy out.  */
da5b55
-  ds_init (&input_name, 128);
da5b55
   file_hdr.c_magic = 070707;
da5b55
 
da5b55
   /* Check whether the output file might be a tape.  */
da5b55
@@ -644,14 +657,9 @@ process_copy_out ()
da5b55
 	    {
da5b55
 	      if (file_hdr.c_mode & CP_IFDIR)
da5b55
 		{
da5b55
-		  int len = strlen (input_name.ds_string);
da5b55
 		  /* Make sure the name ends with a slash */
da5b55
-		  if (input_name.ds_string[len-1] != '/')
da5b55
-		    {
da5b55
-		      ds_resize (&input_name, len + 2);
da5b55
-		      input_name.ds_string[len] = '/';
da5b55
-		      input_name.ds_string[len+1] = 0;
da5b55
-		    }
da5b55
+		  if (!ds_endswith (&input_name, '/'))
da5b55
+		    ds_append (&input_name, '/');
da5b55
 		}
da5b55
 	    }
da5b55
 	  
da5b55
@@ -659,8 +667,7 @@ process_copy_out ()
da5b55
 	  cpio_safer_name_suffix (input_name.ds_string, false,
da5b55
 				  !no_abs_paths_flag, true);
da5b55
 #ifndef HPUX_CDF
da5b55
-	  file_hdr.c_name = input_name.ds_string;
da5b55
-	  file_hdr.c_namesize = strlen (input_name.ds_string) + 1;
da5b55
+	  cpio_set_c_name (&file_hdr, input_name.ds_string);
da5b55
 #else
da5b55
 	  if ( (archive_format != arf_tar) && (archive_format != arf_ustar) )
da5b55
 	    {
da5b55
@@ -669,16 +676,15 @@ process_copy_out ()
da5b55
 		 properly recreate the directory as hidden (in case the
da5b55
 		 files of a directory go into the archive before the
da5b55
 		 directory itself (e.g from "find ... -depth ... | cpio")).  */
da5b55
-	      file_hdr.c_name = add_cdf_double_slashes (input_name.ds_string);
da5b55
-	      file_hdr.c_namesize = strlen (file_hdr.c_name) + 1;
da5b55
+              cpio_set_c_name (&file_hdr,
da5b55
+                               add_cdf_double_slashes (input_name.ds_string));
da5b55
 	    }
da5b55
 	  else
da5b55
 	    {
da5b55
 	      /* We don't mark CDF's in tar files.  We assume the "hidden"
da5b55
 		 directory will always go into the archive before any of
da5b55
 		 its files.  */
da5b55
-	      file_hdr.c_name = input_name.ds_string;
da5b55
-	      file_hdr.c_namesize = strlen (input_name.ds_string) + 1;
da5b55
+              cpio_set_c_name (&file_hdr, input_name.ds_string);
da5b55
 	    }
da5b55
 #endif
da5b55
 
da5b55
@@ -865,8 +871,7 @@ process_copy_out ()
da5b55
   file_hdr.c_chksum = 0;
da5b55
 
da5b55
   file_hdr.c_filesize = 0;
da5b55
-  file_hdr.c_namesize = 11;
da5b55
-  file_hdr.c_name = CPIO_TRAILER_NAME;
da5b55
+  cpio_set_c_name (&file_hdr, CPIO_TRAILER_NAME);
da5b55
   if (archive_format != arf_tar && archive_format != arf_ustar)
da5b55
     write_out_header (&file_hdr, out_file_des);
da5b55
   else
da5b55
@@ -884,6 +889,8 @@ process_copy_out ()
da5b55
 	       ngettext ("%lu block\n", "%lu blocks\n",
da5b55
 			 (unsigned long) blocks), (unsigned long) blocks);
da5b55
     }
da5b55
+  cpio_file_stat_free (&file_hdr);
da5b55
+  ds_free (&input_name);
da5b55
 }
da5b55
 
da5b55
 
da5b55
diff --git a/src/tar.c b/src/tar.c
da5b55
index 1b1156e..0a34845 100644
da5b55
--- a/src/tar.c
da5b55
+++ b/src/tar.c
da5b55
@@ -282,7 +282,7 @@ read_in_tar_header (struct cpio_file_stat *file_hdr, int in_des)
da5b55
       if (null_block ((long *) &tar_rec, TARRECORDSIZE))
da5b55
 #endif
da5b55
 	{
da5b55
-	  file_hdr->c_name = CPIO_TRAILER_NAME;
da5b55
+	  cpio_set_c_name (file_hdr, CPIO_TRAILER_NAME);
da5b55
 	  return;
da5b55
 	}
da5b55
 #if 0
da5b55
@@ -316,9 +316,11 @@ read_in_tar_header (struct cpio_file_stat *file_hdr, int in_des)
da5b55
 	}
da5b55
 
da5b55
       if (archive_format != arf_ustar)
da5b55
-	file_hdr->c_name = stash_tar_filename (NULL, tar_hdr->name);
da5b55
+        cpio_set_c_name (file_hdr, stash_tar_filename (NULL, tar_hdr->name));
da5b55
       else
da5b55
-	file_hdr->c_name = stash_tar_filename (tar_hdr->prefix, tar_hdr->name);
da5b55
+        cpio_set_c_name (file_hdr, stash_tar_filename (tar_hdr->prefix,
da5b55
+                                                      tar_hdr->name));
da5b55
+
da5b55
       file_hdr->c_nlink = 1;
da5b55
       file_hdr->c_mode = FROM_OCTAL (tar_hdr->mode);
da5b55
       file_hdr->c_mode = file_hdr->c_mode & 07777;
da5b55
@@ -398,7 +400,7 @@ read_in_tar_header (struct cpio_file_stat *file_hdr, int in_des)
da5b55
 	case AREGTYPE:
da5b55
 	  /* Old tar format; if the last char in filename is '/' then it is
da5b55
 	     a directory, otherwise it's a regular file.  */
da5b55
-	  if (file_hdr->c_name[strlen (file_hdr->c_name) - 1] == '/')
da5b55
+	  if (file_hdr->c_name[file_hdr->c_namesize - 1] == '/')
da5b55
 	    file_hdr->c_mode |= CP_IFDIR;
da5b55
 	  else
da5b55
 	    file_hdr->c_mode |= CP_IFREG;
da5b55
diff --git a/src/copypass.c b/src/copypass.c
da5b55
index b4e7169..8378a9b 100644
da5b55
--- a/src/copypass.c
da5b55
+++ b/src/copypass.c
da5b55
@@ -48,10 +48,12 @@ set_copypass_perms (int fd, const char *name, struct stat *st)
da5b55
    If `link_flag', link instead of copying.  */
da5b55
 
da5b55
 void
da5b55
-process_copy_pass ()
da5b55
+process_copy_pass (void)
da5b55
 {
da5b55
-  dynamic_string input_name;	/* Name of file from stdin.  */
da5b55
-  dynamic_string output_name;	/* Name of new file.  */
da5b55
+  dynamic_string input_name = DYNAMIC_STRING_INITIALIZER;
da5b55
+                                /* Name of file from stdin.  */
da5b55
+  dynamic_string output_name = DYNAMIC_STRING_INITIALIZER;
da5b55
+                                /* Name of new file.  */
da5b55
   size_t dirname_len;		/* Length of `directory_name'.  */
da5b55
   int res;			/* Result of functions.  */
da5b55
   char *slash;			/* For moving past slashes in input name.  */
da5b55
@@ -69,25 +71,19 @@ process_copy_pass ()
da5b55
 				   created files  */
da5b55
 
da5b55
   /* Initialize the copy pass.  */
da5b55
-  ds_init (&input_name, 128);
da5b55
   
da5b55
   dirname_len = strlen (directory_name);
da5b55
   if (change_directory_option && !ISSLASH (directory_name[0]))
da5b55
     {
da5b55
       char *pwd = xgetcwd ();
da5b55
 
da5b55
-      dirname_len += strlen (pwd) + 1;
da5b55
-      ds_init (&output_name, dirname_len + 2);
da5b55
-      strcpy (output_name.ds_string, pwd);
da5b55
-      strcat (output_name.ds_string, "/");
da5b55
-      strcat (output_name.ds_string, directory_name);
da5b55
+      ds_concat (&output_name, pwd);
da5b55
+      ds_append (&output_name, '/');
da5b55
     }
da5b55
-  else
da5b55
-    {
da5b55
-      ds_init (&output_name, dirname_len + 2);
da5b55
-      strcpy (output_name.ds_string, directory_name);
da5b55
-    }
da5b55
-  output_name.ds_string[dirname_len] = '/';
da5b55
+  
da5b55
+  ds_concat (&output_name, directory_name);
da5b55
+  ds_append (&output_name, '/');
da5b55
+  dirname_len = ds_len (&output_name);
da5b55
   output_is_seekable = true;
da5b55
 
da5b55
   change_dir ();
da5b55
@@ -127,8 +123,8 @@ process_copy_pass ()
da5b55
 	 keep track of which directories in a path are "hidden".  */
da5b55
       slash = add_cdf_double_slashes (slash);
da5b55
 #endif
da5b55
-      ds_resize (&output_name, dirname_len + strlen (slash) + 2);
da5b55
-      strcpy (output_name.ds_string + dirname_len + 1, slash);
da5b55
+      ds_reset (&output_name, dirname_len);
da5b55
+      ds_concat (&output_name, slash);
da5b55
 
da5b55
       existing_dir = false;
da5b55
       if (lstat (output_name.ds_string, &out_file_stat) == 0)
da5b55
@@ -346,6 +342,8 @@ process_copy_pass ()
da5b55
 			 (unsigned long) blocks),
da5b55
 	       (unsigned long) blocks);
da5b55
     }
da5b55
+  ds_free (&input_name);
da5b55
+  ds_free (&output_name);
da5b55
 }
da5b55
 
da5b55
 /* Try and create a hard link from FILE_NAME to another file 
da5b55
@@ -385,7 +383,7 @@ link_to_maj_min_ino (char *file_name, int st_dev_maj, int st_dev_min,
da5b55
    is created, -1 otherwise.  */
da5b55
 
da5b55
 int
da5b55
-link_to_name (char *link_name, char *link_target)
da5b55
+link_to_name (char const *link_name, char const *link_target)
da5b55
 {
da5b55
   int res = link (link_target, link_name);
da5b55
   if (res < 0 && create_dir_flag)
da5b55
diff --git a/src/cpiohdr.h b/src/cpiohdr.h
da5b55
index b29e6fb..aa4a8c4 100644
da5b55
--- a/src/cpiohdr.h
da5b55
+++ b/src/cpiohdr.h
da5b55
@@ -126,8 +126,15 @@ struct cpio_file_stat /* Internal representation of a CPIO header */
da5b55
   size_t c_namesize;
da5b55
   uint32_t c_chksum;
da5b55
   char *c_name;
da5b55
-  char *c_tar_linkname;
da5b55
+  size_t c_name_buflen;
da5b55
+  char const *c_tar_linkname;
da5b55
 };
da5b55
 
da5b55
+#define CPIO_FILE_STAT_INITIALIZER \
da5b55
+  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0, NULL }
da5b55
+void cpio_file_stat_init (struct cpio_file_stat *file_hdr);
da5b55
+void cpio_file_stat_free (struct cpio_file_stat *file_hdr);
da5b55
+void cpio_set_c_name(struct cpio_file_stat *file_hdr, char *name);
da5b55
+void cpio_realloc_c_name (struct cpio_file_stat *file_hdr, size_t len);
da5b55
 
da5b55
 #endif /* cpiohdr.h */
da5b55
diff --git a/src/dstring.c b/src/dstring.c
da5b55
index 2e6b97b..b70d72e 100644
da5b55
--- a/src/dstring.c
da5b55
+++ b/src/dstring.c
da5b55
@@ -22,37 +22,52 @@
da5b55
 #endif
da5b55
 
da5b55
 #include <stdio.h>
da5b55
+#include <stdlib.h>
da5b55
 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
da5b55
 #include <string.h>
da5b55
 #else
da5b55
 #include <strings.h>
da5b55
 #endif
da5b55
 #include "dstring.h"
da5b55
-
da5b55
-char *xmalloc (unsigned n);
da5b55
-char *xrealloc (char *p, unsigned n);
da5b55
+#include <xalloc.h>
da5b55
 
da5b55
 /* Initialiaze dynamic string STRING with space for SIZE characters.  */
da5b55
 
da5b55
 void
da5b55
-ds_init (dynamic_string *string, int size)
da5b55
+ds_init (dynamic_string *string)
da5b55
+{
da5b55
+  memset (string, 0, sizeof *string);
da5b55
+}
da5b55
+
da5b55
+/* Free the dynamic string storage. */
da5b55
+
da5b55
+void
da5b55
+ds_free (dynamic_string *string)
da5b55
 {
da5b55
-  string->ds_length = size;
da5b55
-  string->ds_string = (char *) xmalloc (size);
da5b55
+  free (string->ds_string);
da5b55
 }
da5b55
 
da5b55
-/* Expand dynamic string STRING, if necessary, to hold SIZE characters.  */
da5b55
+/* Expand dynamic string STRING, if necessary.  */
da5b55
 
da5b55
 void
da5b55
-ds_resize (dynamic_string *string, int size)
da5b55
+ds_resize (dynamic_string *string, size_t len)
da5b55
 {
da5b55
-  if (size > string->ds_length)
da5b55
+  while (len + string->ds_idx >= string->ds_size)
da5b55
     {
da5b55
-      string->ds_length = size;
da5b55
-      string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
da5b55
+      string->ds_string = x2nrealloc (string->ds_string, &string->ds_size,
da5b55
+				      1);
da5b55
     }
da5b55
 }
da5b55
 
da5b55
+/* Reset the index of the dynamic string S to LEN. */
da5b55
+
da5b55
+void
da5b55
+ds_reset (dynamic_string *s, size_t len)
da5b55
+{
da5b55
+  ds_resize (s, len);
da5b55
+  s->ds_idx = len;
da5b55
+}
da5b55
+
da5b55
 /* Dynamic string S gets a string terminated by the EOS character
da5b55
    (which is removed) from file F.  S will increase
da5b55
    in size during the function if the string from F is longer than
da5b55
@@ -63,34 +78,49 @@ ds_resize (dynamic_string *string, int size)
da5b55
 char *
da5b55
 ds_fgetstr (FILE *f, dynamic_string *s, char eos)
da5b55
 {
da5b55
-  int insize;			/* Amount needed for line.  */
da5b55
-  int strsize;			/* Amount allocated for S.  */
da5b55
   int next_ch;
da5b55
 
da5b55
   /* Initialize.  */
da5b55
-  insize = 0;
da5b55
-  strsize = s->ds_length;
da5b55
+  s->ds_idx = 0;
da5b55
 
da5b55
   /* Read the input string.  */
da5b55
-  next_ch = getc (f);
da5b55
-  while (next_ch != eos && next_ch != EOF)
da5b55
+  while ((next_ch = getc (f)) != eos && next_ch != EOF)
da5b55
     {
da5b55
-      if (insize >= strsize - 1)
da5b55
-	{
da5b55
-	  ds_resize (s, strsize * 2 + 2);
da5b55
-	  strsize = s->ds_length;
da5b55
-	}
da5b55
-      s->ds_string[insize++] = next_ch;
da5b55
-      next_ch = getc (f);
da5b55
+      ds_resize (s, 0);
da5b55
+      s->ds_string[s->ds_idx++] = next_ch;
da5b55
     }
da5b55
-  s->ds_string[insize++] = '\0';
da5b55
+  ds_resize (s, 0);
da5b55
+  s->ds_string[s->ds_idx] = '\0';
da5b55
 
da5b55
-  if (insize == 1 && next_ch == EOF)
da5b55
+  if (s->ds_idx == 0 && next_ch == EOF)
da5b55
     return NULL;
da5b55
   else
da5b55
     return s->ds_string;
da5b55
 }
da5b55
 
da5b55
+void
da5b55
+ds_append (dynamic_string *s, int c)
da5b55
+{
da5b55
+  ds_resize (s, 0);
da5b55
+  s->ds_string[s->ds_idx] = c;
da5b55
+  if (c)
da5b55
+    {
da5b55
+      s->ds_idx++;
da5b55
+      ds_resize (s, 0);
da5b55
+      s->ds_string[s->ds_idx] = 0;
da5b55
+    }      
da5b55
+}
da5b55
+
da5b55
+void
da5b55
+ds_concat (dynamic_string *s, char const *str)
da5b55
+{
da5b55
+  size_t len = strlen (str);
da5b55
+  ds_resize (s, len);
da5b55
+  memcpy (s->ds_string + s->ds_idx, str, len);
da5b55
+  s->ds_idx += len;
da5b55
+  s->ds_string[s->ds_idx] = 0;
da5b55
+}
da5b55
+
da5b55
 char *
da5b55
 ds_fgets (FILE *f, dynamic_string *s)
da5b55
 {
da5b55
@@ -102,3 +132,10 @@ ds_fgetname (FILE *f, dynamic_string *s)
da5b55
 {
da5b55
   return ds_fgetstr (f, s, '\0');
da5b55
 }
da5b55
+
da5b55
+/* Return true if the dynamic string S ends with character C. */
da5b55
+int
da5b55
+ds_endswith (dynamic_string *s, int c)
da5b55
+{
da5b55
+  return (s->ds_idx > 0 && s->ds_string[s->ds_idx - 1] == c);
da5b55
+}
da5b55
diff --git a/src/dstring.h b/src/dstring.h
da5b55
index 5b49def..a2b6183 100644
da5b55
--- a/src/dstring.h
da5b55
+++ b/src/dstring.h
da5b55
@@ -17,10 +17,6 @@
da5b55
    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
da5b55
    Boston, MA 02110-1301 USA.  */
da5b55
 
da5b55
-#ifndef NULL
da5b55
-#define NULL 0
da5b55
-#endif
da5b55
-
da5b55
 /* A dynamic string consists of record that records the size of an
da5b55
    allocated string and the pointer to that string.  The actual string
da5b55
    is a normal zero byte terminated string that can be used with the
da5b55
@@ -30,22 +26,24 @@
da5b55
 
da5b55
 typedef struct
da5b55
 {
da5b55
-  int ds_length;		/* Actual amount of storage allocated.  */
da5b55
-  char *ds_string;		/* String.  */
da5b55
+  size_t ds_size;   /* Actual amount of storage allocated.  */
da5b55
+  size_t ds_idx;    /* Index of the next free byte in the string. */
da5b55
+  char *ds_string;  /* String storage. */
da5b55
 } dynamic_string;
da5b55
 
da5b55
+#define DYNAMIC_STRING_INITIALIZER { 0, 0, NULL }
da5b55
 
da5b55
-/* Macros that look similar to the original string functions.
da5b55
-   WARNING:  These macros work only on pointers to dynamic string records.
da5b55
-   If used with a real record, an "&" must be used to get the pointer.  */
da5b55
-#define ds_strlen(s)		strlen ((s)->ds_string)
da5b55
-#define ds_strcmp(s1, s2)	strcmp ((s1)->ds_string, (s2)->ds_string)
da5b55
-#define ds_strncmp(s1, s2, n)	strncmp ((s1)->ds_string, (s2)->ds_string, n)
da5b55
-#define ds_index(s, c)		index ((s)->ds_string, c)
da5b55
-#define ds_rindex(s, c)		rindex ((s)->ds_string, c)
da5b55
+void ds_init (dynamic_string *string);
da5b55
+void ds_free (dynamic_string *string);
da5b55
+void ds_reset (dynamic_string *s, size_t len);
da5b55
 
da5b55
-void ds_init (dynamic_string *string, int size);
da5b55
-void ds_resize (dynamic_string *string, int size);
da5b55
+/* All functions below guarantee that s->ds_string[s->ds_idx] == '\0' */
da5b55
 char *ds_fgetname (FILE *f, dynamic_string *s);
da5b55
 char *ds_fgets (FILE *f, dynamic_string *s);
da5b55
 char *ds_fgetstr (FILE *f, dynamic_string *s, char eos);
da5b55
+void ds_append (dynamic_string *s, int c);
da5b55
+void ds_concat (dynamic_string *s, char const *str);
da5b55
+
da5b55
+#define ds_len(s) ((s)->ds_idx)
da5b55
+
da5b55
+int ds_endswith (dynamic_string *s, int c);
da5b55
diff --git a/src/extern.h b/src/extern.h
da5b55
index 47b477a..6330e04 100644
da5b55
--- a/src/extern.h
da5b55
+++ b/src/extern.h
da5b55
@@ -111,18 +111,21 @@ void read_in_binary (struct cpio_file_stat *file_hdr,
da5b55
 		     struct old_cpio_header *short_hdr, int in_des);
da5b55
 void swab_array (char *arg, int count);
da5b55
 void process_copy_in (void);
da5b55
-void long_format (struct cpio_file_stat *file_hdr, char *link_name);
da5b55
-void print_name_with_quoting (char *p);
da5b55
+void long_format (struct cpio_file_stat *file_hdr, char const *link_name);
da5b55
 
da5b55
 /* copyout.c */
da5b55
 int write_out_header (struct cpio_file_stat *file_hdr, int out_des);
da5b55
 void process_copy_out (void);
da5b55
+int to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase,
da5b55
+	      bool nul);
da5b55
+void field_width_error (const char *filename, const char *fieldname,
da5b55
+			uintmax_t value, size_t width, bool nul);
da5b55
 
da5b55
 /* copypass.c */
da5b55
 void process_copy_pass (void);
da5b55
 int link_to_maj_min_ino (char *file_name, int st_dev_maj, 
da5b55
 			 int st_dev_min, ino_t st_ino);
da5b55
-int link_to_name (char *link_name, char *link_target);
da5b55
+int link_to_name (char const *link_name, char const *link_target);
da5b55
 
da5b55
 /* dirname.c */
da5b55
 char *dirname (char *path);
da5b55
@@ -141,7 +144,7 @@ void process_args (int argc, char *argv[]);
da5b55
 void initialize_buffers (void);
da5b55
 
da5b55
 /* makepath.c */
da5b55
-int make_path (char *argpath, uid_t owner, gid_t group,
da5b55
+int make_path (char const *argpath, uid_t owner, gid_t group,
da5b55
 	       const char *verbose_fmt_string);
da5b55
 
da5b55
 /* tar.c */
da5b55
@@ -169,7 +172,7 @@ void copy_files_disk_to_tape (int in_des, int out_des, off_t num_bytes, char *fi
da5b55
 void copy_files_disk_to_disk (int in_des, int out_des, off_t num_bytes, char *filename);
da5b55
 void warn_if_file_changed (char *file_name, off_t old_file_size,
da5b55
                            time_t old_file_mtime);
da5b55
-void create_all_directories (char *name);
da5b55
+void create_all_directories (char const *name);
da5b55
 void prepare_append (int out_file_des);
da5b55
 char *find_inode_file (ino_t node_num,
da5b55
 		       unsigned long major_num, unsigned long minor_num);
da5b55
@@ -204,10 +207,17 @@ void cpio_safer_name_suffix (char *name, bool link_target,
da5b55
 int cpio_create_dir (struct cpio_file_stat *file_hdr, int existing_dir);
da5b55
 void change_dir (void);
da5b55
 
da5b55
-/* FIXME: These two defines should be defined in paxutils */
da5b55
+/* FIXME: The following three should be defined in paxutils */
da5b55
 #define LG_8  3
da5b55
 #define LG_16 4
da5b55
 
da5b55
+/* The maximum uintmax_t value that can be represented with DIGITS digits,
da5b55
+   assuming that each digit is BITS_PER_DIGIT wide.  */
da5b55
+#define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
da5b55
+   ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
da5b55
+    ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
da5b55
+    : (uintmax_t) -1)
da5b55
+
da5b55
 uintmax_t from_ascii (char const *where, size_t digs, unsigned logbase);
da5b55
 
da5b55
 #define FROM_OCTAL(f) from_ascii (f, sizeof f, LG_8)
da5b55
diff --git a/src/idcache.c b/src/idcache.c
da5b55
index c89e7f1..e82414e 100644
da5b55
--- a/src/idcache.c
da5b55
+++ b/src/idcache.c
da5b55
@@ -34,6 +34,7 @@
da5b55
 #endif
da5b55
 
da5b55
 #include <unistd.h>
da5b55
+#include <inttostr.h>
da5b55
 
da5b55
 struct userid
da5b55
 {
da5b55
@@ -59,7 +60,6 @@ getuser (uid_t uid)
da5b55
 {
da5b55
   register struct userid *tail;
da5b55
   struct passwd *pwent;
da5b55
-  char usernum_string[20];
da5b55
 
da5b55
   for (tail = user_alist; tail; tail = tail->next)
da5b55
     if (tail->id.u == uid)
da5b55
@@ -70,8 +70,8 @@ getuser (uid_t uid)
da5b55
   tail->id.u = uid;
da5b55
   if (pwent == 0)
da5b55
     {
da5b55
-      sprintf (usernum_string, "%u", (unsigned) uid);
da5b55
-      tail->name = xstrdup (usernum_string);
da5b55
+      char nbuf[UINTMAX_STRSIZE_BOUND];
da5b55
+      tail->name = xstrdup (umaxtostr (uid, nbuf));
da5b55
     }
da5b55
   else
da5b55
     tail->name = xstrdup (pwent->pw_name);
da5b55
@@ -134,7 +134,6 @@ getgroup (gid_t gid)
da5b55
 {
da5b55
   register struct userid *tail;
da5b55
   struct group *grent;
da5b55
-  char groupnum_string[20];
da5b55
 
da5b55
   for (tail = group_alist; tail; tail = tail->next)
da5b55
     if (tail->id.g == gid)
da5b55
@@ -145,8 +144,8 @@ getgroup (gid_t gid)
da5b55
   tail->id.g = gid;
da5b55
   if (grent == 0)
da5b55
     {
da5b55
-      sprintf (groupnum_string, "%u", (unsigned int) gid);
da5b55
-      tail->name = xstrdup (groupnum_string);
da5b55
+      char nbuf[UINTMAX_STRSIZE_BOUND];
da5b55
+      tail->name = xstrdup (umaxtostr (gid, nbuf));
da5b55
     }
da5b55
   else
da5b55
     tail->name = xstrdup (grent->gr_name);
da5b55
diff --git a/src/makepath.c b/src/makepath.c
da5b55
index 18d5b69..bad2537 100644
da5b55
--- a/src/makepath.c
da5b55
+++ b/src/makepath.c
da5b55
@@ -49,7 +49,7 @@
da5b55
    ownership and permissions when done, otherwise 1.  */
da5b55
 
da5b55
 int
da5b55
-make_path (char *argpath,
da5b55
+make_path (char const *argpath,
da5b55
 	   uid_t owner,
da5b55
 	   gid_t group,
da5b55
 	   const char *verbose_fmt_string)
da5b55
diff --git a/src/userspec.c b/src/userspec.c
da5b55
index 14d608c..7b8bf2f 100644
da5b55
--- a/src/userspec.c
da5b55
+++ b/src/userspec.c
da5b55
@@ -24,6 +24,7 @@
da5b55
 #include <stdio.h>
da5b55
 #include <ctype.h>
da5b55
 #include <sys/types.h>
da5b55
+#include <inttostr.h>
da5b55
 
da5b55
 #ifndef HAVE_ENDPWENT
da5b55
 # define endpwent()
da5b55
@@ -141,12 +142,8 @@ parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
da5b55
 	      grp = getgrgid (pwd->pw_gid);
da5b55
 	      if (grp == NULL)
da5b55
 		{
da5b55
-		  /* This is enough room to hold the unsigned decimal
da5b55
-		     representation of any 32-bit quantity and the trailing
da5b55
-		     zero byte.  */
da5b55
-		  char uint_buf[21];
da5b55
-		  sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid));
da5b55
-		  V_STRDUP (groupname, uint_buf);
da5b55
+		  char nbuf[UINTMAX_STRSIZE_BOUND];
da5b55
+		  V_STRDUP (groupname, umaxtostr (pwd->pw_gid, nbuf));
da5b55
 		}
da5b55
 	      else
da5b55
 		{
da5b55
diff --git a/src/util.c b/src/util.c
da5b55
index 11f9c30..097304f 100644
da5b55
--- a/src/util.c
da5b55
+++ b/src/util.c
da5b55
@@ -79,8 +79,7 @@ tape_empty_output_buffer (int out_des)
da5b55
 
da5b55
       if (output_is_special
da5b55
 	  && (bytes_written >= 0
da5b55
-	      || (bytes_written < 0
da5b55
-		  && (errno == ENOSPC || errno == EIO || errno == ENXIO))))
da5b55
+	      || (errno == ENOSPC || errno == EIO || errno == ENXIO)))
da5b55
 	{
da5b55
 	  get_next_reel (out_des);
da5b55
 	  if (bytes_written > 0)
da5b55
@@ -596,7 +595,7 @@ warn_if_file_changed (char *file_name, off_t old_file_size,
da5b55
    Do not destroy any nondirectories while creating directories.  */
da5b55
 
da5b55
 void
da5b55
-create_all_directories (char *name)
da5b55
+create_all_directories (char const *name)
da5b55
 {
da5b55
   char *dir;
da5b55
   int   mode;
da5b55
@@ -718,7 +717,6 @@ find_inode_val (ino_t node_num, unsigned long major_num,
da5b55
 		 unsigned long minor_num)
da5b55
 {
da5b55
   struct inode_val sample;
da5b55
-  struct inode_val *ival;
da5b55
   
da5b55
   if (!hash_table)
da5b55
     return NULL;
da5b55
@@ -768,7 +766,7 @@ add_inode (ino_t node_num, char *file_name, unsigned long major_num,
da5b55
   return e;
da5b55
 }
da5b55
 
da5b55
-static ino_t
da5b55
+static void
da5b55
 get_inode_and_dev (struct cpio_file_stat *hdr, struct stat *st)
da5b55
 {
da5b55
   if (renumber_inodes_option)
da5b55
@@ -859,11 +857,9 @@ get_next_reel (int tape_des)
da5b55
   FILE *tty_out;		/* File for interacting with user.  */
da5b55
   int old_tape_des;
da5b55
   char *next_archive_name;
da5b55
-  dynamic_string new_name;
da5b55
+  dynamic_string new_name = DYNAMIC_STRING_INITIALIZER;
da5b55
   char *str_res;
da5b55
 
da5b55
-  ds_init (&new_name, 128);
da5b55
-
da5b55
   /* Open files for interactive communication.  */
da5b55
   tty_in = fopen (TTY_NAME, "r");
da5b55
   if (tty_in == NULL)
da5b55
@@ -938,7 +934,7 @@ get_next_reel (int tape_des)
da5b55
     error (PAXEXIT_FAILURE, 0, _("internal error: tape descriptor changed from %d to %d"),
da5b55
 	   old_tape_des, tape_des);
da5b55
 
da5b55
-  free (new_name.ds_string);
da5b55
+  ds_free (&new_name);
da5b55
   fclose (tty_in);
da5b55
   fclose (tty_out);
da5b55
 }
da5b55
@@ -1412,8 +1408,28 @@ set_file_times (int fd,
da5b55
     utime_error (name);
da5b55
 }
da5b55
 
da5b55
+/* Reallocate file_hdr->c_name to accomodate len bytes (including final \0) */
da5b55
+void
da5b55
+cpio_realloc_c_name (struct cpio_file_stat *file_hdr, size_t len)
da5b55
+{
da5b55
+  while (file_hdr->c_name_buflen < len)
da5b55
+    file_hdr->c_name = x2realloc (file_hdr->c_name, &file_hdr->c_name_buflen);
da5b55
+}
da5b55
+
da5b55
+void
da5b55
+cpio_set_c_name (struct cpio_file_stat *file_hdr, char *name)
da5b55
+{
da5b55
+  size_t len = strlen (name) + 1;
da5b55
+
da5b55
+  cpio_realloc_c_name (file_hdr, len);
da5b55
+  file_hdr->c_namesize = len;
da5b55
+  memmove (file_hdr->c_name, name, len);
da5b55
+}
da5b55
+
da5b55
 /* Do we have to ignore absolute paths, and if so, does the filename
da5b55
-   have an absolute path?  */
da5b55
+   have an absolute path?  Before calling this function make sure that the
da5b55
+   allocated NAME buffer has capacity at least 2 bytes. */
da5b55
+
da5b55
 void
da5b55
 cpio_safer_name_suffix (char *name, bool link_target, bool absolute_names,
da5b55
 			bool strip_leading_dots)
da5b55
@@ -1428,6 +1444,10 @@ cpio_safer_name_suffix (char *name, bool link_target, bool absolute_names,
da5b55
 	  ++p;
da5b55
       }
da5b55
   if (p != name)
da5b55
+    /* The 'p' string is shortened version of 'name' with one exception;  when
da5b55
+       the 'name' points to an empty string (buffer where name[0] == '\0') the
da5b55
+       'p' then points to static string ".".  So caller needs to ensure there
da5b55
+       are at least two bytes available in 'name' buffer so memmove succeeds. */
da5b55
     memmove (name, p, (size_t)(strlen (p) + 1));
da5b55
 }
da5b55
 
da5b55
@@ -1689,4 +1709,17 @@ arf_stores_inode_p (enum archive_format arf)
da5b55
     }
da5b55
   return 1;
da5b55
 }
da5b55
+
da5b55
+void
da5b55
+cpio_file_stat_init (struct cpio_file_stat *file_hdr)
da5b55
+{
da5b55
+  memset (file_hdr, 0, sizeof (*file_hdr));
da5b55
+}
da5b55
+
da5b55
+void
da5b55
+cpio_file_stat_free (struct cpio_file_stat *file_hdr)
da5b55
+{
da5b55
+  free (file_hdr->c_name);
da5b55
+  cpio_file_stat_init (file_hdr);
da5b55
+}
da5b55
   
da5b55
-- 
da5b55
2.31.1
da5b55