David edwards jr wrote:


    thank you leif. works well and with the protection not in sprintf. I
    also found  it in the pdf file that I had. I simply did not go down
    to the functions section

Note that while gmp_snprintf() protects your buffer from overflowing, it doesn't protect you from truncated numbers.

If the buffer was too small, [gmp_]snprintf() returns a number >= the size you passed to it.

You can use mpz_sizeinbase(x, 10) to check in advance whether your number x will fit into the string buffer (in decimal notation, i.e., base 10). Your buffer should be at least two bytes larger than that (assuming you use just "%Zd"); one byte for a possible '-' sign, one for the trailing '\0'.

See also mpz_get_str(), which does the necessary allocation for you if you pass NULL instead of the address of a buffer; it's up to you to free the allocated memory afterwards when necessary or appropriate though.


-leif

--
() The ASCII Ribbon Campaign
/\   Help Cure HTML E-Mail

--
You received this message because you are subscribed to the Google Groups 
"mpir-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mpir-devel+unsubscr...@googlegroups.com.
To post to this group, send email to mpir-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/mpir-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to