Fix potential buffer overrun in cube_out(), per report from

Bruno Wolff.
This commit is contained in:
Tom Lane 2002-08-18 20:15:52 +00:00
parent 22bfa72068
commit e1b040a7c3
2 changed files with 21 additions and 24 deletions

View File

@ -10,6 +10,7 @@
#include "access/gist.h" #include "access/gist.h"
#include "access/rtree.h" #include "access/rtree.h"
#include "lib/stringinfo.h"
#include "utils/elog.h" #include "utils/elog.h"
#include "utils/palloc.h" #include "utils/palloc.h"
#include "utils/builtins.h" #include "utils/builtins.h"
@ -107,47 +108,43 @@ cube_in(char *str)
* char *out_func(char *); * char *out_func(char *);
*/ */
char * char *
cube_out(NDBOX * cube) cube_out(NDBOX *cube)
{ {
char *result; StringInfoData buf;
char *p; bool equal = true;
int equal = 1;
int dim = cube->dim; int dim = cube->dim;
int i; int i;
if (cube == NULL) initStringInfo(&buf);
return (NULL);
p = result = (char *) palloc(100);
/* /*
* while printing the first (LL) corner, check if it is equal to the * while printing the first (LL) corner, check if it is equal to the
* scond one * second one
*/ */
p += sprintf(p, "("); appendStringInfoChar(&buf, '(');
for (i = 0; i < dim; i++) for (i = 0; i < dim; i++)
{ {
p += sprintf(p, "%g", cube->x[i]); if (i > 0)
p += sprintf(p, ", "); appendStringInfo(&buf, ", ");
appendStringInfo(&buf, "%g", cube->x[i]);
if (cube->x[i] != cube->x[i + dim]) if (cube->x[i] != cube->x[i + dim])
equal = 0; equal = false;
} }
p -= 2; /* get rid of the last ", " */ appendStringInfoChar(&buf, ')');
p += sprintf(p, ")");
if (!equal) if (!equal)
{ {
p += sprintf(p, ",("); appendStringInfo(&buf, ",(");
for (i = dim; i < dim * 2; i++) for (i = 0; i < dim; i++)
{ {
p += sprintf(p, "%g", cube->x[i]); if (i > 0)
p += sprintf(p, ", "); appendStringInfo(&buf, ", ");
appendStringInfo(&buf, "%g", cube->x[i + dim]);
} }
p -= 2; appendStringInfoChar(&buf, ')');
p += sprintf(p, ")");
} }
return (result); return buf.data;
} }

View File

@ -8,12 +8,12 @@ SET search_path = public;
CREATE FUNCTION cube_in(opaque) CREATE FUNCTION cube_in(opaque)
RETURNS opaque RETURNS opaque
AS 'MODULE_PATHNAME' AS 'MODULE_PATHNAME'
LANGUAGE 'c'; LANGUAGE 'c' WITH (isStrict);
CREATE FUNCTION cube_out(opaque) CREATE FUNCTION cube_out(opaque)
RETURNS opaque RETURNS opaque
AS 'MODULE_PATHNAME' AS 'MODULE_PATHNAME'
LANGUAGE 'c'; LANGUAGE 'c' WITH (isStrict);
CREATE TYPE cube ( CREATE TYPE cube (
internallength = variable, internallength = variable,