From: Tom Lane Date: Tue, 1 Dec 2015 16:42:25 +0000 (-0500) Subject: Use "g" not "f" format in ecpg's PGTYPESnumeric_from_double(). X-Git-Tag: REL9_6_BETA1~1049 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=db4a5cfc76206db82d0b929d96c53de229ef1aa4;p=postgresql 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. --- diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c index 0504f3cac8..d061616787 100644 --- a/src/interfaces/ecpg/pgtypeslib/numeric.c +++ b/src/interfaces/ecpg/pgtypeslib/numeric.c @@ -2,6 +2,7 @@ #include "postgres_fe.h" #include +#include #include #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)