coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] factor: fix integer validation and GMP fallback


From: Pádraig Brady
Subject: [PATCH] factor: fix integer validation and GMP fallback
Date: Mon, 8 Oct 2012 16:57:42 +0100

In the recent factor rewrite, the GMP code
wasn't actually used; just an error was printed
on integer overflow.  While fixing that it was noticed
that correct input validation wasn't done in all cases
when falling back to the GMP code.

* src/factor.c (print_factors) Fallback to GMP on overflow.
(strto2uintmax): Scan the string for invalid characters,
so that can be detected independently of overflow.
Return an error when an empty string is passed.
---
 src/factor.c |   23 ++++++++++++++++++-----
 1 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/factor.c b/src/factor.c
index 843542b..ce36581 100644
--- a/src/factor.c
+++ b/src/factor.c
@@ -2221,14 +2221,15 @@ static strtol_error
 strto2uintmax (uintmax_t *hip, uintmax_t *lop, const char *s)
 {
   unsigned int lo_carry;
-  uintmax_t hi, lo;
+  uintmax_t hi = 0, lo = 0;
 
-  strtol_error err;
+  strtol_error err = LONGINT_INVALID;
 
-  hi = lo = 0;
+  /* Initial scan for invalid digits.  */
+  const char *p = s;
   for (;;)
     {
-      unsigned int c = *s++;
+      unsigned int c = *p++;
       if (c == 0)
         break;
 
@@ -2237,9 +2238,17 @@ strto2uintmax (uintmax_t *hip, uintmax_t *lop, const 
char *s)
           err = LONGINT_INVALID;
           break;
         }
-      c -= '0';
 
       err = LONGINT_OK;           /* we've seen at least one valid digit */
+    }
+
+  for (;err == LONGINT_OK;)
+    {
+      unsigned int c = *s++;
+      if (c == 0)
+        break;
+
+      c -= '0';
 
       if (UNLIKELY (hi > ~(uintmax_t)0 / 10))
         {
@@ -2342,6 +2351,10 @@ print_factors (const char *input)
         }
       break;
 
+    case LONGINT_OVERFLOW:
+      /* Try GMP.  */
+      break;
+
     default:
       error (0, 0, _("%s is not a valid positive integer"), quote (input));
       return false;
-- 
1.7.6.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]