diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2015-03-17 22:46:11 +0200 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2015-03-17 22:46:11 +0200 |
commit | cffd09247c1681fbf3d5cad5253b3199704f83e7 (patch) | |
tree | 5341be1b30a9efa371d1802a5bb73aba5e447294 /profile.c | |
parent | 1b047a42077ca58eeeaa93e0561c0b589350702b (diff) | |
download | egawk-cffd09247c1681fbf3d5cad5253b3199704f83e7.tar.gz egawk-cffd09247c1681fbf3d5cad5253b3199704f83e7.tar.bz2 egawk-cffd09247c1681fbf3d5cad5253b3199704f83e7.zip |
Fix bad allocs -M and profiling.
Diffstat (limited to 'profile.c')
-rw-r--r-- | profile.c | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -1304,16 +1304,30 @@ pp_number(NODE *n) { #define PP_PRECISION 6 char *str; + size_t count; - emalloc(str, char *, PP_PRECISION + 10, "pp_number"); #ifdef HAVE_MPFR - if (is_mpg_float(n)) - mpfr_sprintf(str, "%0.*R*g", PP_PRECISION, ROUND_MODE, n->mpg_numbr); - else if (is_mpg_integer(n)) + if (is_mpg_float(n)) { + count = mpfr_get_prec(n->mpg_numbr) / 3; /* ~ 3.22 binary digits per decimal digit */ + emalloc(str, char *, count, "pp_number"); + /* + * 3/2015: Format string used to be "%0.*R*g". That padded + * with leading zeros. But it doesn't do that for regular + * numbers in the non-MPFR case. + */ + mpfr_sprintf(str, "%.*R*g", PP_PRECISION, ROUND_MODE, n->mpg_numbr); + } else if (is_mpg_integer(n)) { + count = mpz_sizeinbase(n->mpg_i, 10) + 2; /* +1 for sign, +1 for NUL at end */ + emalloc(str, char *, count, "pp_number"); mpfr_sprintf(str, "%Zd", n->mpg_i); - else + } else #endif - sprintf(str, "%0.*g", PP_PRECISION, n->numbr); + { + count = PP_PRECISION + 10; + emalloc(str, char *, count, "pp_number"); + sprintf(str, "%0.*g", PP_PRECISION, n->numbr); + } + return str; #undef PP_PRECISION } |