Extra zero byte converting mpz_t to a string

Dmitry Antipov dmantipov at yandex.ru
Sat Apr 18 01:36:17 UTC 2015


Hello,

for some strange reason, an attached program outputs an unwanted
trailing zero byte for 6!, 14!, 24!, 29!, 31!, 33!, 40!, 52!, 60!,
65!, 66!, 71!, 79!, 81! and 96!-100! (full output is also attached).

Found on Fedora 21 x86_64 system with vendor's GMP 6.0.0a package
(gmp-6.0.0-9.fc23.src.rpm); reproduced with unstable development
version from hg. Any thoughts?

Dmitry
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>

int
main (int argc, char *argv[])
{
  char *s;
  mpz_t v;
  size_t i, j, len;

  mpz_init (v);

  for (i = 1; i <= 100; i++)
    {
      mpz_fac_ui (v, i);

      len = mpz_sizeinbase (v, 10);
      s = mpz_get_str (malloc (len + 2), 10, v);

      printf ("%lu! = ", i);
      for (j = 0; j < len; j++)
	putchar (s[j]);

      if (s[len - 1] == '\0')
	printf (" [trailing zero byte]\n");
      else
	printf (" [ok]\n");
    }

  return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: simple.out
Type: application/octet-stream
Size: 8506 bytes
Desc: not available
URL: <https://gmplib.org/list-archives/gmp-bugs/attachments/20150418/85b9f80e/attachment.obj>


More information about the gmp-bugs mailing list