From 95955e6746e847a366143121a8d0a61404cc740e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 8 Jul 2020 08:05:06 -0700 Subject: [PATCH] factor: treat ' +bignum' like non-bignum * 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 | 27 +++++++++------------------ tests/misc/factor.pl | 3 +++ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/factor.c b/src/factor.c index dd87e6309d..c1c35a5629 100644 --- a/src/factor.c +++ b/src/factor.c @@ -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); diff --git a/tests/misc/factor.pl b/tests/misc/factor.pl index ecd2ab293e..db64706aba 100755 --- a/tests/misc/factor.pl +++ b/tests/misc/factor.pl @@ -86,6 +86,9 @@ my @Tests = '115792089237316195423570985008687907853' . '269984665640564039457584007913129639936', {OUT => '2 'x255 . '2'}], + ['bug-gmp-plus_2_sup_128_plus_1', + '+170141183460469231731687303715884105729', + {OUT => '3 56713727820156410577229101238628035243'}], ); -- 2.47.2