Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Use "g" not "f" format in ecpg's PGTYPESnumeric_from_double().
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 1 Dec 2015 16:42:25 +0000 (11:42 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 1 Dec 2015 16:42:47 +0000 (11:42 -0500)
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.

src/interfaces/ecpg/pgtypeslib/numeric.c

index aefabea976f32fe50c971b95804ce442416c8aee..d4806771b346da3e77a8ae650b6138b1a63557ef 100644 (file)
@@ -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)