diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2015-12-01 11:42:25 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2015-12-01 11:42:52 -0500 |
commit | 84387496f47cac8a1a5f3566e493d270258847b7 (patch) | |
tree | 3e49a41aab8e56efb1c06dca1428bda21266bfe8 /src | |
parent | cb7ea8d9857b75d599f249bb6119c4fab08f9d14 (diff) |
Use "g" not "f" format in ecpg's PGTYPESnumeric_from_double().
The previous coding could overrun the provided buffer size for a very large
input, or lose precision for a very small input. Adopt the methodology
that's been in use in the equivalent backend code for a long time.
Per private report from Bas van Schaik. Back-patch to all supported
branches.
Diffstat (limited to 'src')
-rw-r--r-- | src/interfaces/ecpg/pgtypeslib/numeric.c | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c index aefabea976f..d4806771b34 100644 --- a/src/interfaces/ecpg/pgtypeslib/numeric.c +++ b/src/interfaces/ecpg/pgtypeslib/numeric.c @@ -2,6 +2,7 @@ #include "postgres_fe.h" #include <ctype.h> +#include <float.h> #include <limits.h> #include "extern.h" @@ -1498,11 +1499,11 @@ PGTYPESnumeric_copy(numeric *src, numeric *dst) int PGTYPESnumeric_from_double(double d, numeric *dst) { - char buffer[100]; + char buffer[DBL_DIG + 100]; numeric *tmp; int i; - if (sprintf(buffer, "%f", d) == 0) + if (sprintf(buffer, "%.*g", DBL_DIG, d) <= 0) return -1; if ((tmp = PGTYPESnumeric_from_asc(buffer, NULL)) == NULL) |