Subject: Re: bug#12753: printf in make-prime-list.c uses "modern" features

From: Pádraig Brady <[email-masked]>
Date: Mon, 29 Oct 2012 01:57:02 +0000

On 10/28/2012 07:40 PM, John David Anglin wrote:
> make-prime-list crashes on hppa1.1-hp-hpux10.20.  This probably
> will occur on any hppa-hpux system.
>
> Attached is a hack to work around the problem.

Is %jx not supported, or is the * not supported with %j?
I'm guessing the latter since you got a runtime issue
rather than a compile time one.  We also use '*' in
other format strings, so I doubt that's an issue in itself.

Note there are only a couple of other uses of %j.
A newly introduced one in factor.c.
So does your system support %ju?

   src/factor.c:    printf ("%ju", t0);

Also this one introduced a year ago in an error path:

   src/join.c:                     0, _("%s:%ju: is not sorted: %.*s"),

I'm a little surprised (and encouraged) that there were
no reported compile issues for the %ju above, which
suggests we might be able to depend on that (well soon anyway).
Up until now we've been using the PRIuMAX etc. constants.

Un any case I might add a syntax check rule for the moment around
   git grep "%[0*]*j[udx]"
to keep %j out of new code.

For this particular case can you test this more generic workaround.

thanks,
Pádraig.

diff --git a/src/make-prime-list.c b/src/make-prime-list.c
index e0d9b81..a4c0a3b 100644
--- a/src/make-prime-list.c
+++ b/src/make-prime-list.c
@@ -20,6 +20,7 @@ this program.  If not, see http://www.gnu.org/licenses/.  */
  #include <config.h>

  #include <stdint.h>
+#include <inttypes.h>
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
@@ -77,17 +78,23 @@ output_primes (const struct prime *primes, unsigned nprimes)
        exit (EXIT_FAILURE);
      }

-#define SZ (int)(2*sizeof (uintmax_t))
+#if UINTMAX_MAX == UINT32_MAX
+# define SZ "8" /* 8 hex digits.  */
+#elif UINTMAX_MAX == UINT64_MAX
+# define SZ "16" /* 16 hex digits.  */
+#elif UINTMAX_MAX == UINT128_MAX
+# define SZ "32" /* 32 hex digits.  */
+#endif

    for (i = 0, p = 2; i < nprimes; i++)
      {
        unsigned int d8 = i + 8 < nprimes ? primes[i + 8].p - primes[i].p : 0xff;
        if (255 < d8) /* this happens at 668221 */
          abort ();
-      printf ("P (%2u, %3u, 0x%0*jx%s, 0x%0*jx%s) /* %d */\n",
+      printf ("P (%2u, %3u, 0x%0"SZ PRIxMAX"%s, 0x%0"SZ PRIxMAX"%s) /* %d */\n",
                primes[i].p - p, d8,
-              SZ, primes[i].pinv, suffix,
-              SZ, primes[i].lim, suffix, primes[i].p);
+              primes[i].pinv, suffix,
+              primes[i].lim, suffix, primes[i].p);
        p = primes[i].p;
      }