ce91e8
From 0d5aa7b6578422d3a3a7cad315807ae5b53566b2 Mon Sep 17 00:00:00 2001
ce91e8
From: Ondrej Dubaj <odubaj@redhat.com>
ce91e8
Date: Thu, 19 Mar 2020 08:58:32 +0100
ce91e8
Subject: [PATCH] * src/copyout.c (to_ascii): Additional argument nul controls
ce91e8
 whether to add the terminating nul character. (field_width_error): Improve
ce91e8
 diagnostics: print the actual and the maximum allowed field value. *
ce91e8
 src/extern.h (to_ascii, field_width_error): New prototypes. * src/tar.c
ce91e8
 (to_oct): Remove. (to_oct_or_error): New function. (TO_OCT): New macro.
ce91e8
 (write_out_tar_header)
ce91e8
ce91e8
---
ce91e8
 src/copyout.c | 47 +++++++++++++++++++++-------------
ce91e8
 src/extern.h  | 15 +++++++++--
ce91e8
 src/tar.c     | 71 ++++++++++++++++++++++++---------------------------
ce91e8
 3 files changed, 75 insertions(+), 58 deletions(-)
ce91e8
ce91e8
diff --git a/src/copyout.c b/src/copyout.c
ce91e8
index 03eaf88..c0b1ffa 100644
ce91e8
--- a/src/copyout.c
ce91e8
+++ b/src/copyout.c
ce91e8
@@ -279,26 +279,32 @@ writeout_final_defers (int out_des)
ce91e8
    so it should be moved to paxutils too.
ce91e8
    Allowed values for logbase are: 1 (binary), 2, 3 (octal), 4 (hex) */
ce91e8
 int
ce91e8
-to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase)
ce91e8
+to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase, bool nul)
ce91e8
 {
ce91e8
   static char codetab[] = "0123456789ABCDEF";
ce91e8
-  int i = digits;
ce91e8
   
ce91e8
-  do
ce91e8
+  if (nul)
ce91e8
+    where[--digits] = 0;
ce91e8
+  while (digits > 0)
ce91e8
     {
ce91e8
-      where[--i] = codetab[(v & ((1 << logbase) - 1))];
ce91e8
+      where[--digits] = codetab[(v & ((1 << logbase) - 1))];
ce91e8
       v >>= logbase;
ce91e8
     }
ce91e8
-  while (i);
ce91e8
 
ce91e8
   return v != 0;
ce91e8
 }
ce91e8
 
ce91e8
-static void
ce91e8
-field_width_error (const char *filename, const char *fieldname)
ce91e8
+void
ce91e8
+field_width_error (const char *filename, const char *fieldname,
ce91e8
+		   uintmax_t value, size_t width, bool nul)
ce91e8
 {
ce91e8
-  error (1, 0, _("%s: field width not sufficient for storing %s"),
ce91e8
-	 filename, fieldname);
ce91e8
+  char valbuf[UINTMAX_STRSIZE_BOUND + 1];
ce91e8
+  char maxbuf[UINTMAX_STRSIZE_BOUND + 1];
ce91e8
+  error (0, 0, _("%s: value %s %s out of allowed range 0..%s"),
ce91e8
+	 filename, fieldname,
ce91e8
+	 STRINGIFY_BIGINT (value, valbuf),
ce91e8
+	 STRINGIFY_BIGINT (MAX_VAL_WITH_DIGITS (width - nul, LG_8),
ce91e8
+			   maxbuf));
ce91e8
 }
ce91e8
 
ce91e8
 static void
ce91e8
@@ -313,7 +319,7 @@ to_ascii_or_warn (char *where, uintmax_t n, size_t digits,
ce91e8
 		  unsigned logbase,
ce91e8
 		  const char *filename, const char *fieldname)
ce91e8
 {
ce91e8
-  if (to_ascii (where, n, digits, logbase))
ce91e8
+  if (to_ascii (where, n, digits, logbase, false))
ce91e8
     field_width_warning (filename, fieldname);
ce91e8
 }    
ce91e8
 
ce91e8
@@ -322,9 +328,9 @@ to_ascii_or_error (char *where, uintmax_t n, size_t digits,
ce91e8
 		   unsigned logbase,
ce91e8
 		   const char *filename, const char *fieldname)
ce91e8
 {
ce91e8
-  if (to_ascii (where, n, digits, logbase))
ce91e8
+  if (to_ascii (where, n, digits, logbase, false))
ce91e8
     {
ce91e8
-      field_width_error (filename, fieldname);
ce91e8
+      field_width_error (filename, fieldname, n, digits, false);
ce91e8
       return 1;
ce91e8
     }
ce91e8
   return 0;
ce91e8
@@ -381,7 +387,7 @@ write_out_new_ascii_header (const char *magic_string,
ce91e8
 			 _("name size")))
ce91e8
     return 1;
ce91e8
   p += 8;
ce91e8
-  to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16);
ce91e8
+  to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16, false);
ce91e8
 
ce91e8
   tape_buffered_write (ascii_header, out_des, sizeof ascii_header);
ce91e8
 
ce91e8
@@ -398,7 +404,7 @@ write_out_old_ascii_header (dev_t dev, dev_t rdev,
ce91e8
   char ascii_header[76];
ce91e8
   char *p = ascii_header;
ce91e8
   
ce91e8
-  to_ascii (p, file_hdr->c_magic, 6, LG_8);
ce91e8
+  to_ascii (p, file_hdr->c_magic, 6, LG_8, false);
ce91e8
   p += 6;
ce91e8
   to_ascii_or_warn (p, dev, 6, LG_8, file_hdr->c_name, _("device number"));
ce91e8
   p += 6;
ce91e8
@@ -502,7 +508,10 @@ write_out_binary_header (dev_t rdev,
ce91e8
   short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF;
ce91e8
   if (short_hdr.c_namesize != file_hdr->c_namesize)
ce91e8
     {
ce91e8
-      field_width_error (file_hdr->c_name, _("name size"));
ce91e8
+      char maxbuf[UINTMAX_STRSIZE_BOUND + 1];
ce91e8
+      error (0, 0, _("%s: value %s %s out of allowed range 0..%u"),
ce91e8
+	     file_hdr->c_name, _("name size"),
ce91e8
+	     STRINGIFY_BIGINT (file_hdr->c_namesize, maxbuf), 0xFFFFu);
ce91e8
       return 1;
ce91e8
     }
ce91e8
 		      
ce91e8
@@ -512,7 +521,10 @@ write_out_binary_header (dev_t rdev,
ce91e8
   if (((off_t)short_hdr.c_filesizes[0] << 16) + short_hdr.c_filesizes[1]
ce91e8
        != file_hdr->c_filesize)
ce91e8
     {
ce91e8
-      field_width_error (file_hdr->c_name, _("file size"));
ce91e8
+      char maxbuf[UINTMAX_STRSIZE_BOUND + 1];
ce91e8
+      error (0, 0, _("%s: value %s %s out of allowed range 0..%lu"),
ce91e8
+	     file_hdr->c_name, _("file size"),
ce91e8
+	     STRINGIFY_BIGINT (file_hdr->c_namesize, maxbuf), 0xFFFFFFFFlu);
ce91e8
       return 1;
ce91e8
     }
ce91e8
 		      
ce91e8
@@ -562,8 +574,7 @@ write_out_header (struct cpio_file_stat *file_hdr, int out_des)
ce91e8
 	  error (0, 0, _("%s: file name too long"), file_hdr->c_name);
ce91e8
 	  return 1;
ce91e8
 	}
ce91e8
-      write_out_tar_header (file_hdr, out_des); /* FIXME: No error checking */
ce91e8
-      return 0;
ce91e8
+      return write_out_tar_header (file_hdr, out_des);
ce91e8
 
ce91e8
     case arf_binary:
ce91e8
       return write_out_binary_header (makedev (file_hdr->c_rdev_maj,
ce91e8
diff --git a/src/extern.h b/src/extern.h
ce91e8
index 1c6f70b..a0b6009 100644
ce91e8
--- a/src/extern.h
ce91e8
+++ b/src/extern.h
ce91e8
@@ -117,6 +117,10 @@ void print_name_with_quoting (char *p);
ce91e8
 /* copyout.c */
ce91e8
 int write_out_header (struct cpio_file_stat *file_hdr, int out_des);
ce91e8
 void process_copy_out (void);
ce91e8
+int to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase,
ce91e8
+	      bool nul);
ce91e8
+void field_width_error (const char *filename, const char *fieldname,
ce91e8
+			uintmax_t value, size_t width, bool nul);
ce91e8
 
ce91e8
 /* copypass.c */
ce91e8
 void process_copy_pass (void);
ce91e8
@@ -145,7 +149,7 @@ int make_path (char *argpath, uid_t owner, gid_t group,
ce91e8
 	       const char *verbose_fmt_string);
ce91e8
 
ce91e8
 /* tar.c */
ce91e8
-void write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des);
ce91e8
+int write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des);
ce91e8
 int null_block (long *block, int size);
ce91e8
 void read_in_tar_header (struct cpio_file_stat *file_hdr, int in_des);
ce91e8
 int otoa (char *s, unsigned long *n);
ce91e8
@@ -203,9 +207,16 @@ void cpio_safer_name_suffix (char *name, bool link_target,
ce91e8
 			     bool absolute_names, bool strip_leading_dots);
ce91e8
 int cpio_create_dir (struct cpio_file_stat *file_hdr, int existing_dir);
ce91e8
 
ce91e8
-/* FIXME: These two defines should be defined in paxutils */
ce91e8
+/* FIXME: The following three should be defined in paxutils */
ce91e8
 #define LG_8  3
ce91e8
 #define LG_16 4
ce91e8
+/* The maximum uintmax_t value that can be represented with DIGITS digits,
ce91e8
+   assuming that each digit is BITS_PER_DIGIT wide.  */
ce91e8
+#define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
ce91e8
+   ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
ce91e8
+    ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
ce91e8
+    : (uintmax_t) -1)
ce91e8
+
ce91e8
 
ce91e8
 uintmax_t from_ascii (char const *where, size_t digs, unsigned logbase);
ce91e8
 
ce91e8
diff --git a/src/tar.c b/src/tar.c
ce91e8
index 854878e..fe8fdcf 100644
ce91e8
--- a/src/tar.c
ce91e8
+++ b/src/tar.c
ce91e8
@@ -1,6 +1,5 @@
ce91e8
 /* tar.c - read in write tar headers for cpio
ce91e8
-   Copyright (C) 1992, 2001, 2004, 2006, 2007, 2010 Free Software
ce91e8
-   Foundation, Inc.
ce91e8
+   Copyright (C) 1992-2019 Free Software Foundation, Inc.
ce91e8
 
ce91e8
    This program is free software; you can redistribute it and/or modify
ce91e8
    it under the terms of the GNU General Public License as published by
ce91e8
@@ -81,34 +80,16 @@ stash_tar_filename (char *prefix, char *filename)
ce91e8
   return hold_tar_filename;
ce91e8
 }
ce91e8
 
ce91e8
-/* Convert a number into a string of octal digits.
ce91e8
-   Convert long VALUE into a DIGITS-digit field at WHERE,
ce91e8
-   including a trailing space and room for a NUL.  DIGITS==3 means
ce91e8
-   1 digit, a space, and room for a NUL.
ce91e8
-
ce91e8
-   We assume the trailing NUL is already there and don't fill it in.
ce91e8
-   This fact is used by start_header and finish_header, so don't change it!
ce91e8
-
ce91e8
-   This is be equivalent to:
ce91e8
-   sprintf (where, "%*lo ", digits - 2, value);
ce91e8
-   except that sprintf fills in the trailing NUL and we don't.  */
ce91e8
-
ce91e8
-static void
ce91e8
-to_oct (register long value, register int digits, register char *where)
ce91e8
+static int
ce91e8
+to_oct_or_error (uintmax_t value, size_t digits, char *where, char const *field,
ce91e8
+		 char const *file)
ce91e8
 {
ce91e8
-  --digits;			/* Leave the trailing NUL slot alone.  */
ce91e8
-
ce91e8
-  /* Produce the digits -- at least one.  */
ce91e8
-  do
ce91e8
+  if (to_ascii (where, value, digits, LG_8, true))
ce91e8
     {
ce91e8
-      where[--digits] = '0' + (char) (value & 7); /* One octal digit.  */
ce91e8
-      value >>= 3;
ce91e8
+      field_width_error (file, field, value, digits, true);
ce91e8
+      return 1;
ce91e8
     }
ce91e8
-  while (digits > 0 && value != 0);
ce91e8
-
ce91e8
-  /* Add leading zeroes, if necessary.  */
ce91e8
-  while (digits > 0)
ce91e8
-    where[--digits] = '0';
ce91e8
+  return 0;
ce91e8
 }
ce91e8
 
ce91e8
 
ce91e8
@@ -136,10 +117,22 @@ tar_checksum (struct tar_header *tar_hdr)
ce91e8
   return sum;
ce91e8
 }
ce91e8
 
ce91e8
+#define TO_OCT(file_hdr, c_fld, digits, tar_hdr, tar_field) \
ce91e8
+  do							    \
ce91e8
+    {							    \
ce91e8
+       if (to_oct_or_error (file_hdr -> c_fld,		    \
ce91e8
+			    digits,			    \
ce91e8
+			    tar_hdr -> tar_field,	    \
ce91e8
+			    #tar_field,			    \
ce91e8
+			    file_hdr->c_name))		    \
ce91e8
+	 return 1;					    \
ce91e8
+    }							    \
ce91e8
+  while (0)
ce91e8
+
ce91e8
 /* Write out header FILE_HDR, including the file name, to file
ce91e8
    descriptor OUT_DES.  */
ce91e8
 
ce91e8
-void
ce91e8
+int
ce91e8
 write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
ce91e8
 {
ce91e8
   int name_len;
ce91e8
@@ -168,11 +161,11 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
ce91e8
 
ce91e8
   /* Ustar standard (POSIX.1-1988) requires the mode to contain only 3 octal
ce91e8
      digits */
ce91e8
-  to_oct (file_hdr->c_mode & MODE_ALL, 8, tar_hdr->mode);
ce91e8
-  to_oct (file_hdr->c_uid, 8, tar_hdr->uid);
ce91e8
-  to_oct (file_hdr->c_gid, 8, tar_hdr->gid);
ce91e8
-  to_oct (file_hdr->c_filesize, 12, tar_hdr->size);
ce91e8
-  to_oct (file_hdr->c_mtime, 12, tar_hdr->mtime);
ce91e8
+  TO_OCT (file_hdr, c_mode & MODE_ALL, 8, tar_hdr, mode);
ce91e8
+  TO_OCT (file_hdr, c_uid, 8, tar_hdr, uid);
ce91e8
+  TO_OCT (file_hdr, c_gid, 8, tar_hdr, gid);
ce91e8
+  TO_OCT (file_hdr, c_filesize, 12, tar_hdr, size);
ce91e8
+  TO_OCT (file_hdr, c_mtime, 12, tar_hdr, mtime);
ce91e8
 
ce91e8
   switch (file_hdr->c_mode & CP_IFMT)
ce91e8
     {
ce91e8
@@ -184,7 +177,7 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
ce91e8
 	  strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
ce91e8
 		   TARLINKNAMESIZE);
ce91e8
 	  tar_hdr->typeflag = LNKTYPE;
ce91e8
-	  to_oct (0, 12, tar_hdr->size);
ce91e8
+	  to_ascii (tar_hdr->size, 0, 12, LG_8, true);
ce91e8
 	}
ce91e8
       else
ce91e8
 	tar_hdr->typeflag = REGTYPE;
ce91e8
@@ -210,7 +203,7 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
ce91e8
 	 than TARLINKNAMESIZE.  */
ce91e8
       strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
ce91e8
 	       TARLINKNAMESIZE);
ce91e8
-      to_oct (0, 12, tar_hdr->size);
ce91e8
+      to_ascii (tar_hdr->size, 0, 12, LG_8, true);
ce91e8
       break;
ce91e8
 #endif /* CP_IFLNK */
ce91e8
     }
ce91e8
@@ -229,13 +222,15 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
ce91e8
       if (name)
ce91e8
 	strcpy (tar_hdr->gname, name);
ce91e8
 
ce91e8
-      to_oct (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor);
ce91e8
-      to_oct (file_hdr->c_rdev_min, 8, tar_hdr->devminor);
ce91e8
+      TO_OCT (file_hdr, c_rdev_maj, 8, tar_hdr, devmajor);
ce91e8
+      TO_OCT (file_hdr, c_rdev_min, 8, tar_hdr, devminor);
ce91e8
     }
ce91e8
 
ce91e8
-  to_oct (tar_checksum (tar_hdr), 8, tar_hdr->chksum);
ce91e8
+  to_ascii (tar_hdr->chksum, tar_checksum (tar_hdr), 8, LG_8, true);
ce91e8
 
ce91e8
   tape_buffered_write ((char *) &tar_rec, out_des, TARRECORDSIZE);
ce91e8
+
ce91e8
+  return 0;
ce91e8
 }
ce91e8
 
ce91e8
 /* Return nonzero iff all the bytes in BLOCK are NUL.
ce91e8
-- 
ce91e8
2.24.1
ce91e8