The attached program creates a rather large number, then attempts to print
it.  The printing step encounters a segmentation fault.

Typical error traceback as seen by gdb:
Program received signal SIGSEGV, Segmentation fault.
__memmove_avx_unaligned_erms () at
../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:500
500 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file
or directory.
(gdb) where
#0  __memmove_avx_unaligned_erms () at
../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:500
#1  0x000055555555cb3c in __gmp_asprintf_memory (d=0x7fffffffe2e0,
str=0x7ffeff624010 "", len=18446744071562067968) at asprntffuns.c:48
#2  0x000055555555d042 in __gmp_doprnt_integer (funs=0x555555583d80
<__gmp_asprintf_funs_noformat>, data=0x7fffffffe2e0, p=0x7fffffffe340,
s=0x7ffeff624010 "") at doprnti.c:125
#3  0x0000555555556ad0 in __gmp_doprnt_integer_ostream (o=...,
p=0x7fffffffe340, s=0x7ffeff624010 "") at osdoprnti.cc:60
#4  0x0000555555556a09 in operator<< (o=..., z=0x7fffffffe3d0) at
osmpz.cc:46
#5  0x0000555555556934 in operator<< <__mpz_struct [1], __mpz_struct [1]>
(o=..., expr=...) at ./gmpxx.h:2181
#6  0x0000555555556428 in main (argc=2, argv=0x7fffffffe528) at bigg.c:16

It appears that the character-size of strings at some layer
(__gmp_doprnt_integer?) is restricted to INT_MAX bytes of output.

Problem exists with the development version at changeset:
18211:92681f2490cc (April 16) as well as with 6.1.2+dfsg-4 as published by
Debian for Buster.
#include <gmpxx.h>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

int main(int argc, char **argv) {
    for(int i=1; i<argc; i++) {
        size_t ai = (size_t)stoull(argv[i]);
        mpz_class v{1};
        v <<= ai;
        cout << setw(15) << ai << ": "
             << mpz_sizeinbase(v.get_mpz_t(), 10) << " decimal digits\n";
        cout << hex << v << "\n";
    }
}

_______________________________________________
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs

Reply via email to