aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-12-01 11:42:25 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2015-12-01 11:42:39 -0500
commit346cc2f0192791b5ee0fb5de4276d3cb8f5ab9a6 (patch)
tree7bb3204a30dd24642a4d6fcd0a36392b2f34ed2a /src
parentb7fc1dd1fed3c38b1d95c6433b65cbbd22c0323f (diff)
downloadpostgresql-346cc2f0192791b5ee0fb5de4276d3cb8f5ab9a6.tar.gz
postgresql-346cc2f0192791b5ee0fb5de4276d3cb8f5ab9a6.zip
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.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c
index 84d73b7bb7b..35eacf86503 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"
@@ -1497,11 +1498,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)