Skip to content

Commit db4a5cf

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 2287b87 commit db4a5cf

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/interfaces/ecpg/pgtypeslib/numeric.c

Lines changed: 3 additions & 2 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy