8000 Fix contrib/seg to be more wary of long input numbers. · postgres/postgres@0ff4056 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ff4056

Browse files
committed
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
1 parent f48aa5d commit 0ff4056

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

contrib/seg/expected/seg.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ SELECT '12.34567890123456'::seg AS seg;
256256
12.3457
257257
(1 row)
258258

259+
-- Same, with a very long input
260+
SELECT '12.3456789012345600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'::seg AS seg;
261+
seg
262+
---------
263+
12.3457
264+
(1 row)
265+
259266
-- Numbers with certainty indicators
260267
SELECT '~6.5'::seg AS seg;
261268
seg

contrib/seg/expected/seg_1.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ SELECT '12.34567890123456'::seg AS seg;
256256
12.3457
257257
(1 row)
258258

259+
-- Same, with a very long input
260+
SELECT '12.3456789012345600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'::seg AS seg;
261+
seg
262+
---------
263+
12.3457
264+
(1 row)
265+
259266
-- Numbers with certainty indicators
260267
SELECT '~6.5'::seg AS seg;
261268
seg

contrib/seg/seg.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,13 @@ restore(char *result, float val, int n)
923923

924924
/*
925925
* Put a cap on the number of significant digits to avoid garbage in the
926-
* output and ensure we don't overrun the result buffer.
926+
* output and ensure we don't overrun the result buffer. (n should not be
927+
* negative, but check to protect ourselves against corrupted data.)
927928
*/
928-
n = Min(n, FLT_DIG);
929+
if (n <= 0)
930+
n = FLT_DIG;
931+
else
932+
n = Min(n, FLT_DIG);
929933

930934
/* remember the sign */
931935
sign = (val < 0 ? 1 : 0);

contrib/seg/segparse.y

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "postgres.h"
55

6+
#include <float.h>
67
#include <math.h>
78

89
#include "fmgr.h"
@@ -23,6 +24,8 @@
2324

2425
static float seg_atof(const char *value);
2526

27+
static int sig_digits(const char *value);
28+
2629
static char strbuf[25] = {
2730
'0', '0', '0', '0', '0',
2831
'0', '0', '0', '0', '0',
@@ -63,9 +66,9 @@ range: boundary PLUMIN deviation
6366
result->lower = $1.val - $3.val;
6467
result->upper = $1.val + $3.val;
6568
sprintf(strbuf, "%g", result->lower);
66-
result->l_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
69+
result->l_sigd = Max(sig_digits(strbuf), Max($1.sigd, $3.sigd));
6770
sprintf(strbuf, "%g", result->upper);
68-
result->u_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
71+
result->u_sigd = Max(sig_digits(strbuf), Max($1.sigd, $3.sigd));
6972
result->l_ext = '\0';
7073
result->u_ext = '\0';
7174
}
@@ -122,7 +125,7 @@ boundary: SEGFLOAT
122125
float val = seg_atof($1);
123126

124127
$$.ext = '\0';
125-
$$.sigd = significant_digits($1);
128+
$$.sigd = sig_digits($1);
126129
$$.val = val;
127130
}
128131
| EXTENSION SEGFLOAT
@@ -131,7 +134,7 @@ boundary: SEGFLOAT
131134
float val = seg_atof($2);
132135

133136
$$.ext = $1[0];
134-
$$.sigd = significant_digits($2);
137+
$$.sigd = sig_digits($2);
135138
$$.val = val;
136139
}
137140
;
@@ -142,7 +145,7 @@ deviation: SEGFLOAT
142145
float val = seg_atof($1);
143146

144147
$$.ext = '\0';
145-
$$.sigd = significant_digits($1);
148+
$$.sigd = sig_digits($1);
146149
$$.val = val;
14715 628C 0
}
148151
;
@@ -159,5 +162,14 @@ seg_atof(const char *value)
159162
return DatumGetFloat4(datum);
160163
}
161164

165+
static int
166+
sig_digits(const char *value)
167+
{
168+
int n = significant_digits(value);
169+
170+
/* Clamp, to ensure value will fit in sigd fields */
171+
return Min(n, FLT_DIG);
172+
}
173+
162174

163175
#include "segscan.c"

contrib/seg/sql/seg.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ SELECT '3.400e5'::seg AS seg;
6060
-- Digits truncated
6161
SELECT '12.34567890123456'::seg AS seg;
6262

63+
-- Same, with a very long input
64+
SELECT '12.3456789012345600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'::seg AS seg;
65+
6366
-- Numbers with certainty indicators
6467
SELECT '~6.5'::seg AS seg;
6568
SELECT '<6.5'::seg AS seg;

0 commit comments

Comments
 (0)
0