mpz_out_raw is not writing data to the file stream

Thomas Lowry thomaslowry88 at gmail.com
Fri Jul 19 14:01:21 UTC 2019


#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char** argv) {
  mpz_t b;
  mpz_init(b);

  //not sure why but without doing something before
  //the file IO the file write will segfault
  int len = 5;
  mpz_t l[5];
  for(int i = 0; i < len; ++i) {
    mpz_init(l[i]);
  }
  for(int i = 0; i < len; ++i) {
    mpz_set_ui(l[i], 1);
  }
  for(int i = 0; i < len; ++i) {
    mpz_clear(l[i]);
  }

  FILE *file = fopen("numbers.dat", "wb");
  if(file == NULL) {
    perror("Could not open numbers.dat for writing");
    printf ("Error no is : %d\n", errno);
    exit(EXIT_FAILURE);
  }
  for(int i = 1; i < 4; ++i) {
    printf("writing ");
    mpz_set_ui(b, i);
    //mpz_pow_ui(b, b, 1<<5);
    mpz_out_str(stdout, 16, b);
    printf(" to file\n");
    mpz_out_raw(b, file);
  }
  fclose(file);
  printf("write finished\n");
  file = fopen("numbers.dat", "rb");
  for(int i = 1; i < 4; ++i) {
    printf("reading ");
    mpz_inp_raw(b, file);
    mpz_out_str(stdout, 16, b);
    printf(" from file\n");
  }
  fclose(file);
  mpz_clear(b);
  return 0;
}

I compile this with
gcc test.c -lgmp

Across a few test programs I've observed the following behaviours after
calling mpz_out_raw:

- nothing is written to the file
- mpz_out_raw hangs and never returns (the sample program above does
this)
- 1-2 bytes are written to the file and the second call to mpz_out_raw
hangs

My GMPlib version:

[tlowry at archpad scripts]$ pacman -Qi gmp
Name            : gmp
Version         : 6.1.2-2
Description     : A free library for arbitrary precision arithmetic
Architecture    : x86_64
URL             : http://gmplib.org/
Licenses        : LGPL3  GPL
Groups          : None
Provides        : None
Depends On      : gcc-libs  sh
Optional Deps   : None
Required By     :
coreutils  dnsmasq  ffmpeg  guile  guile2.0  mpfr  nettle  python-
pycryptodome
Optional For    : None
Conflicts With  : None
Replaces        : None
Installed Size  : 969.00 KiB
Packager        : Allan McRae <allan at archlinux.org>
Build Date      : Wed 07 Nov 2018 12:52:28 AM ACDT
Install Date    : Sun 11 Nov 2018 11:08:04 AM ACDT
Install Reason  : Installed as a dependency for another package
Install Script  : No
Validated By    : Signature

Other system information:

[tlowry at archpad scripts]$ uname -a
Linux archpad 5.2.1-arch1-1-ARCH #1 SMP PREEMPT Sun Jul 14 14:52:52 UTC
2019 x86_64 GNU/Linux
[tlowry at archpad scripts]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/9.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --
libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --
infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --
enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared
--enable-threads=posix --with-system-zlib --with-isl --enable-
__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --
disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --
enable-linker-build-id --enable-lto --enable-plugin --enable-install-
libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function
--enable-multilib --disable-werror --enable-checking=release --enable-
default-pie --enable-default-ssp --enable-cet=auto
Thread model: posix
gcc version 9.1.0 (GCC)



More information about the gmp-bugs mailing list