Very annoying mpz_get_str function Problem !

The Codrinus codrin at flynet.iasi.rdsnet.ro
Wed Nov 26 15:49:07 CET 2003


> This behavior does not *prove* the problem is in mpz_get_str. It is 
> conceivable that client code corrupts the memory in a way that causes 
> mpz_get_str to corrupt other parts of the memory. But there is certainly 
> grounds for suspicions...
> 
> Two suggestions:
> 
> (1) Run your program under a system like valgrind or purify or 
> electricfence.

Partial report for "valgrind --leak-check=yes ./myprogram <vargs>":
==6487==    Address 0x413CF75C is 0 bytes inside a block of size 3 alloc'd
==6487==    at 0x40029832: __builtin_vec_new (vg_replace_malloc.c:203)
==6487==    by 0x40029889: operator new[](unsigned) (vg_replace_malloc.c:216)
==6487== Invalid write of size 1
==6487==    at 0x4002275A: strcpy (mac_replace_strmem.c:174)
==6487== Invalid read of size 1
==6487==    at 0x403B3C17: strtok (in /lib/libc-2.3.2.so)
==6487==    Address 0x413D3653 is 0 bytes after a block of size 3 alloc'd
==6487==    at 0x40029832: __builtin_vec_new (vg_replace_malloc.c:203)
==6487==    by 0x40029889: operator new[](unsigned) (vg_replace_malloc.c:216)

And these are the errors only from GMP library functions.

> 
> (2) Do you need a fully functional mpz_get_str or is it only for 
> printing? If the latter, make your own but simply one returning an 
> string of '?' of the correct length.

I need to convert a bignumber into a string in a base x, and parse that 
string during the program execution.

> (3) Do you allocate your own buffer or let gmp do it for you? If the 
> latter, in the code of mpz_get_str (after all that's what OpenSource is 
> for!), add a check that the allocated length is OK w.r.t. a length 
> computed by yourself. (if your own buffer I trust you have made it big 
> enough, i.e. mpz_sizeinbase (op, base) + 2).

I used both versions, with my memory allocation, and the default 
GMP allocation with the same weird results.

1) char *buffer = new char[mpz_sizeinbase(bignumber,base)+2];
   mpz_get_str (buffer, base, bignumber);

2) char *buffer = mpz_get_str(NULL, base, bignumber);

> If you have already done all that, sorry, just trying to help.
> 
> HaND,
> 



More information about the gmp-discuss mailing list