Skip to content

Commit 346cc2f

Browse files
committed
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.
1 parent b7fc1dd commit 346cc2f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/interfaces/ecpg/pgtypeslib/numeric.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "postgres_fe.h"
44
#include <ctype.h>
5+
#include <float.h>
56
#include <limits.h>
67

78
#include "extern.h"
@@ -1497,11 +1498,11 @@ PGTYPESnumeric_copy(numeric *src, numeric *dst)
14971498
int
14981499
PGTYPESnumeric_from_double(double d, numeric *dst)
14991500
{
1500-
char buffer[100];
1501+
char buffer[DBL_DIG + 100];
15011502
numeric *tmp;
15021503
int i;
15031504

1504-
if (sprintf(buffer, "%f", d) == 0)
1505+
if (sprintf(buffer, "%.*g", DBL_DIG, d) <= 0)
15051506
return -1;
15061507

15071508
if ((tmp = PGTYPESnumeric_from_asc(buffer, NULL)) == NULL)

0 commit comments

Comments
 (0)