summaryrefslogtreecommitdiff
path: root/contrib/seg/segparse.y
diff options
context:
space:
mode:
authorTom Lane2022-12-21 22:51:50 +0000
committerTom Lane2022-12-21 22:51:50 +0000
commit701c881f782b93ee29587112390bd3bfe035e78d (patch)
tree9c6fa26811eb5060846fcc75bb0bda3141c028ab /contrib/seg/segparse.y
parent33dd895ef3316bd1896def6882e9075359d7e9af (diff)
Fix contrib/seg to be more wary of long input numbers.
seg stores the number of significant digits in an input number in a "char" field. If char is signed, and the input is more than 127 digits long, the count can read out as negative causing seg_out() to print garbage (or, if you're really unlucky, even crash). To fix, clamp the digit count to be not more than FLT_DIG. (In theory this loses some information about what the original input was, but it doesn't seem like useful information; it would not survive dump/restore in any case.) Also, in case there are stored values of the seg type containing bad data, add a clamp in seg_out's restore() subroutine. Per bug #17725 from Robins Tharakan. It's been like this forever, so back-patch to all supported branches. Discussion: https://postgr.es/m/17725-0a09313b67fbe86e@postgresql.org
Diffstat (limited to 'contrib/seg/segparse.y')
-rw-r--r--contrib/seg/segparse.y22
1 files changed, 17 insertions, 5 deletions
diff --git a/contrib/seg/segparse.y b/contrib/seg/segparse.y
index 1d2adbbec89..0156c3e0274 100644
--- a/contrib/seg/segparse.y
+++ b/contrib/seg/segparse.y
@@ -3,6 +3,7 @@
#include "postgres.h"
+#include <float.h>
#include <math.h>
#include "fmgr.h"
@@ -20,6 +21,8 @@
static float seg_atof(const char *value);
+static int sig_digits(const char *value);
+
static char strbuf[25] = {
'0', '0', '0', '0', '0',
'0', '0', '0', '0', '0',
@@ -62,9 +65,9 @@ range: boundary PLUMIN deviation
result->lower = $1.val - $3.val;
result->upper = $1.val + $3.val;
sprintf(strbuf, "%g", result->lower);
- result->l_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
+ result->l_sigd = Max(sig_digits(strbuf), Max($1.sigd, $3.sigd));
sprintf(strbuf, "%g", result->upper);
- result->u_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
+ result->u_sigd = Max(sig_digits(strbuf), Max($1.sigd, $3.sigd));
result->l_ext = '\0';
result->u_ext = '\0';
}
@@ -121,7 +124,7 @@ boundary: SEGFLOAT
float val = seg_atof($1);
$$.ext = '\0';
- $$.sigd = significant_digits($1);
+ $$.sigd = sig_digits($1);
$$.val = val;
}
| EXTENSION SEGFLOAT
@@ -130,7 +133,7 @@ boundary: SEGFLOAT
float val = seg_atof($2);
$$.ext = $1[0];
- $$.sigd = significant_digits($2);
+ $$.sigd = sig_digits($2);
$$.val = val;
}
;
@@ -141,7 +144,7 @@ deviation: SEGFLOAT
float val = seg_atof($1);
$$.ext = '\0';
- $$.sigd = significant_digits($1);
+ $$.sigd = sig_digits($1);
$$.val = val;
}
;
@@ -157,3 +160,12 @@ seg_atof(const char *value)
datum = DirectFunctionCall1(float4in, CStringGetDatum(value));
return DatumGetFloat4(datum);
}
+
+static int
+sig_digits(const char *value)
+{
+ int n = significant_digits(value);
+
+ /* Clamp, to ensure value will fit in sigd fields */
+ return Min(n, FLT_DIG);
+}