Commit db4a5cfc authored by Tom Lane's avatar Tom Lane

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.
parent 2287b874
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "postgres_fe.h" #include "postgres_fe.h"
#include <ctype.h> #include <ctype.h>
#include <float.h>
#include <limits.h> #include <limits.h>
#include "extern.h" #include "extern.h"
...@@ -1497,11 +1498,11 @@ PGTYPESnumeric_copy(numeric *src, numeric *dst) ...@@ -1497,11 +1498,11 @@ PGTYPESnumeric_copy(numeric *src, numeric *dst)
int int
PGTYPESnumeric_from_double(double d, numeric *dst) PGTYPESnumeric_from_double(double d, numeric *dst)
{ {
char buffer[100]; char buffer[DBL_DIG + 100];
numeric *tmp; numeric *tmp;
int i; int i;
if (sprintf(buffer, "%f", d) == 0) if (sprintf(buffer, "%.*g", DBL_DIG, d) <= 0)
return -1; return -1;
if ((tmp = PGTYPESnumeric_from_asc(buffer, NULL)) == NULL) if ((tmp = PGTYPESnumeric_from_asc(buffer, NULL)) == NULL)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment