]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
pr: fix -n to pad consistently and not crash
authorPádraig Brady <P@draigBrady.com>
Wed, 24 Oct 2012 09:55:13 +0000 (10:55 +0100)
committerPádraig Brady <P@draigBrady.com>
Thu, 25 Oct 2012 12:01:13 +0000 (13:01 +0100)
* src/pr.c: Replace the code to truncate the most significant
digits of line numbers, with much simpler string manipulation
that supports arbitrary widths.  Before this, specifying a
width >= 32 to -n would result in a divide by zero error.
Also remove the inconsistent padding with zeros and spaces, which
would result in zero padding for widths 12 and 15.
* tests/pr/pr-tests.pl: Added a test to ensure no zero padding,
and also a test for the divide by zero case.
* NEWS: Mentioned the fix
Reported by Ondrej Oprala

NEWS
src/pr.c
tests/pr/pr-tests.pl

diff --git a/NEWS b/NEWS
index 04721da5d39c6945fdf98c282255637c3300b4ef..e13ba22a3d3e1730838b979b726a9961afe52c5c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,12 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Bug fixes
+
+  pr -n no longer crashes when passed values >= 32.  Also line numbers are
+  consistently padded with spaces, rather than with zeros for certain widths.
+  [bug introduced in TEXTUTILS-1_22i]
+
 
 * Noteworthy changes in release 8.20 (2012-10-23) [stable]
 
index e97c43445719fd29b754cf344ac5c45996daa207..3e401676bcc9f3ddce3cf8d5a4ced3ed46b6db1b 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
@@ -630,10 +630,6 @@ static uintmax_t page_number;
    2    moo     4       hoo     6       zoo */
 static int line_number;
 
-/* With line_number overflow, we use power_10 to cut off the higher-order
-   digits of the line_number */
-static int power_10;
-
 /* (-n) True means lines should be preceded by numbers. */
 static bool numbered_lines = false;
 
@@ -1268,7 +1264,6 @@ init_parameters (int number_of_files)
 
   if (numbered_lines)
     {
-      int tmp_i;
       int chars_per_default_tab = 8;
 
       line_count = start_line_num;
@@ -1289,12 +1284,6 @@ init_parameters (int number_of_files)
          printing files in parallel. */
       if (parallel_files)
         chars_used_by_number = number_width;
-
-      /* We use power_10 to cut off the higher-order digits of the
-         line_number in function add_line_number */
-      tmp_i = chars_per_number;
-      for (power_10 = 1; tmp_i > 0; --tmp_i)
-        power_10 = 10 * power_10;
     }
 
   chars_per_column = (chars_per_line - chars_used_by_number
@@ -1306,7 +1295,8 @@ init_parameters (int number_of_files)
   if (numbered_lines)
     {
       free (number_buff);
-      number_buff = xmalloc (2 * chars_per_number);
+      number_buff = xmalloc (MAX (chars_per_number,
+                                  INT_STRLEN_BOUND (line_number)) + 1);
     }
 
   /* Pick the maximum between the tab width and the width of an
@@ -2029,19 +2019,13 @@ add_line_number (COLUMN *p)
 {
   int i;
   char *s;
-  int left_cut;
+  int num_width;
 
   /* Cutting off the higher-order digits is more informative than
-     lower-order cut off*/
-  if (line_number < power_10)
-    sprintf (number_buff, "%*d", chars_per_number, line_number);
-  else
-    {
-      left_cut = line_number % power_10;
-      sprintf (number_buff, "%0*d", chars_per_number, left_cut);
-    }
+     lower-order cut off. */
+  num_width = sprintf (number_buff, "%*d", chars_per_number, line_number);
   line_number++;
-  s = number_buff;
+  s = number_buff + (num_width - chars_per_number);
   for (i = chars_per_number; i > 0; i--)
     (p->char_func) (*s++);
 
index d06b34f8f949cc231cee647686580c37a51c2c9d..e648a99532a93ac115a9d322cd4f940e5fddf7ec 100755 (executable)
@@ -407,6 +407,13 @@ my @tv = (
 # Before coreutils-8.13 page numbers were not handled correctly when
 # headers were not printed (when -l <= 10 or -t or -T specified)
 ['page-range', '+1:1 -2 -l1 -s" "',  "a\nb\nc\n", "a b\n", 0],
+
+# This padded with zeros before coreutils-8.21
+['padding1', '-t -n,15', "1\n", (" "x 14)."1,1\n", 0],
+# This crashed with divide by zero before coreutils-8.21
+['padding2', '-t -n,64', "1\n", (" "x 63)."1,1\n", 0],
+# Ensure we handle buffer truncation correctly
+['padding3', '-t -N1000000 -n,1', "1\n", "0,1\n", 0],
 );
 
 # Convert the above old-style test vectors to the newer