]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
factor: treat ' +bignum' like non-bignum
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 8 Jul 2020 15:05:06 +0000 (08:05 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 8 Jul 2020 15:11:57 +0000 (08:11 -0700)
* src/factor.c (strto2uintmax): Instead of here ...
(print_factors): ... skip spaces and '+' here, so that
bignums are treated like non-bignums.
* tests/misc/factor.pl (bug-gmp-plus_2_sup_128_plus_1): New test.

src/factor.c
tests/misc/factor.pl

index dd87e6309d082f98afd373e4f469c242a6525c06..c1c35a56298089cb2ee9c4e7eebb61549aa0187b 100644 (file)
@@ -2258,21 +2258,6 @@ strto2uintmax (uintmax_t *hip, uintmax_t *lop, const char *s)
 
   strtol_error err = LONGINT_INVALID;
 
-  /* Skip initial spaces and '+'.  */
-  for (;;)
-    {
-      char c = *s;
-      if (c == ' ')
-        s++;
-      else if (c == '+')
-        {
-          s++;
-          break;
-        }
-      else
-        break;
-    }
-
   /* Initial scan for invalid digits.  */
   const char *p = s;
   for (;;)
@@ -2290,7 +2275,7 @@ strto2uintmax (uintmax_t *hip, uintmax_t *lop, const char *s)
       err = LONGINT_OK;           /* we've seen at least one valid digit */
     }
 
-  for (;err == LONGINT_OK;)
+  while (err == LONGINT_OK)
     {
       unsigned int c = *s++;
       if (c == 0)
@@ -2473,13 +2458,19 @@ print_factors_single (uintmax_t t1, uintmax_t t0)
 static bool
 print_factors (const char *input)
 {
+  /* Skip initial spaces and '+'.  */
+  char const *str = input;
+  while (*str == ' ')
+    str++;
+  str += *str == '+';
+
   uintmax_t t1, t0;
 
   /* Try converting the number to one or two words.  If it fails, use GMP or
      print an error message.  The 2nd condition checks that the most
      significant bit of the two-word number is clear, in a typesize neutral
      way.  */
-  strtol_error err = strto2uintmax (&t1, &t0, input);
+  strtol_error err = strto2uintmax (&t1, &t0, str);
 
   switch (err)
     {
@@ -2505,7 +2496,7 @@ print_factors (const char *input)
   mpz_t t;
   struct mp_factors factors;
 
-  mpz_init_set_str (t, input, 10);
+  mpz_init_set_str (t, str, 10);
 
   gmp_printf ("%Zd:", t);
   mp_factor (t, &factors);
index ecd2ab293efd71007a764c733733922c9fa806f8..db64706abae2e056a3082bf076e4682887824eea 100755 (executable)
@@ -86,6 +86,9 @@ my @Tests =
       '115792089237316195423570985008687907853'
       . '269984665640564039457584007913129639936',
       {OUT => '2 'x255 . '2'}],
+     ['bug-gmp-plus_2_sup_128_plus_1',
+      '+170141183460469231731687303715884105729',
+      {OUT => '3 56713727820156410577229101238628035243'}],
     );