mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	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
		
			
				
	
	
		
			176 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			176 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
%{
 | 
						|
/* contrib/seg/segparse.y */
 | 
						|
 | 
						|
#include "postgres.h"
 | 
						|
 | 
						|
#include <float.h>
 | 
						|
#include <math.h>
 | 
						|
 | 
						|
#include "fmgr.h"
 | 
						|
#include "utils/builtins.h"
 | 
						|
 | 
						|
#include "segdata.h"
 | 
						|
 | 
						|
/*
 | 
						|
 * Bison doesn't allocate anything that needs to live across parser calls,
 | 
						|
 * so we can easily have it use palloc instead of malloc.  This prevents
 | 
						|
 * memory leaks if we error out during parsing.  Note this only works with
 | 
						|
 * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
 | 
						|
 * if possible, so there's not really much problem anyhow, at least if
 | 
						|
 * you're building with gcc.
 | 
						|
 */
 | 
						|
#define YYMALLOC palloc
 | 
						|
#define YYFREE   pfree
 | 
						|
 | 
						|
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',
 | 
						|
	'0', '0', '0', '0', '0',
 | 
						|
	'0', '0', '0', '0', '0',
 | 
						|
	'0', '0', '0', '0', '\0'
 | 
						|
};
 | 
						|
 | 
						|
%}
 | 
						|
 | 
						|
/* BISON Declarations */
 | 
						|
%parse-param {SEG *result}
 | 
						|
%expect 0
 | 
						|
%name-prefix="seg_yy"
 | 
						|
 | 
						|
%union {
 | 
						|
	struct BND {
 | 
						|
		float val;
 | 
						|
		char  ext;
 | 
						|
		char  sigd;
 | 
						|
	} bnd;
 | 
						|
	char * text;
 | 
						|
}
 | 
						|
%token <text> SEGFLOAT
 | 
						|
%token <text> RANGE
 | 
						|
%token <text> PLUMIN
 | 
						|
%token <text> EXTENSION
 | 
						|
%type  <bnd>  boundary
 | 
						|
%type  <bnd>  deviation
 | 
						|
%start range
 | 
						|
 | 
						|
/* Grammar follows */
 | 
						|
%%
 | 
						|
 | 
						|
 | 
						|
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(sig_digits(strbuf), Max($1.sigd, $3.sigd));
 | 
						|
		sprintf(strbuf, "%g", result->upper);
 | 
						|
		result->u_sigd = Max(sig_digits(strbuf), Max($1.sigd, $3.sigd));
 | 
						|
		result->l_ext = '\0';
 | 
						|
		result->u_ext = '\0';
 | 
						|
	}
 | 
						|
 | 
						|
	| boundary RANGE boundary
 | 
						|
	{
 | 
						|
		result->lower = $1.val;
 | 
						|
		result->upper = $3.val;
 | 
						|
		if ( result->lower > result->upper ) {
 | 
						|
			ereport(ERROR,
 | 
						|
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 | 
						|
					 errmsg("swapped boundaries: %g is greater than %g",
 | 
						|
							result->lower, result->upper)));
 | 
						|
 | 
						|
			YYERROR;
 | 
						|
		}
 | 
						|
		result->l_sigd = $1.sigd;
 | 
						|
		result->u_sigd = $3.sigd;
 | 
						|
		result->l_ext = ( $1.ext ? $1.ext : '\0' );
 | 
						|
		result->u_ext = ( $3.ext ? $3.ext : '\0' );
 | 
						|
	}
 | 
						|
 | 
						|
	| boundary RANGE
 | 
						|
	{
 | 
						|
		result->lower = $1.val;
 | 
						|
		result->upper = HUGE_VAL;
 | 
						|
		result->l_sigd = $1.sigd;
 | 
						|
		result->u_sigd = 0;
 | 
						|
		result->l_ext = ( $1.ext ? $1.ext : '\0' );
 | 
						|
		result->u_ext = '-';
 | 
						|
	}
 | 
						|
 | 
						|
	| RANGE boundary
 | 
						|
	{
 | 
						|
		result->lower = -HUGE_VAL;
 | 
						|
		result->upper = $2.val;
 | 
						|
		result->l_sigd = 0;
 | 
						|
		result->u_sigd = $2.sigd;
 | 
						|
		result->l_ext = '-';
 | 
						|
		result->u_ext = ( $2.ext ? $2.ext : '\0' );
 | 
						|
	}
 | 
						|
 | 
						|
	| boundary
 | 
						|
	{
 | 
						|
		result->lower = result->upper = $1.val;
 | 
						|
		result->l_sigd = result->u_sigd = $1.sigd;
 | 
						|
		result->l_ext = result->u_ext = ( $1.ext ? $1.ext : '\0' );
 | 
						|
	}
 | 
						|
	;
 | 
						|
 | 
						|
boundary: SEGFLOAT
 | 
						|
	{
 | 
						|
		/* temp variable avoids a gcc 3.3.x bug on Sparc64 */
 | 
						|
		float val = seg_atof($1);
 | 
						|
 | 
						|
		$$.ext = '\0';
 | 
						|
		$$.sigd = sig_digits($1);
 | 
						|
		$$.val = val;
 | 
						|
	}
 | 
						|
	| EXTENSION SEGFLOAT
 | 
						|
	{
 | 
						|
		/* temp variable avoids a gcc 3.3.x bug on Sparc64 */
 | 
						|
		float val = seg_atof($2);
 | 
						|
 | 
						|
		$$.ext = $1[0];
 | 
						|
		$$.sigd = sig_digits($2);
 | 
						|
		$$.val = val;
 | 
						|
	}
 | 
						|
	;
 | 
						|
 | 
						|
deviation: SEGFLOAT
 | 
						|
	{
 | 
						|
		/* temp variable avoids a gcc 3.3.x bug on Sparc64 */
 | 
						|
		float val = seg_atof($1);
 | 
						|
 | 
						|
		$$.ext = '\0';
 | 
						|
		$$.sigd = sig_digits($1);
 | 
						|
		$$.val = val;
 | 
						|
	}
 | 
						|
	;
 | 
						|
 | 
						|
%%
 | 
						|
 | 
						|
 | 
						|
static float
 | 
						|
seg_atof(const char *value)
 | 
						|
{
 | 
						|
	Datum datum;
 | 
						|
 | 
						|
	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);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
#include "segscan.c"
 |