Fix a read of uninitialized memory in array_out(). Perform some minor

cosmetic code cleanup at the same time.
This commit is contained in:
Neil Conway 2004-09-16 03:15:54 +00:00
parent d1b0d965b1
commit 6a2869f64e
3 changed files with 34 additions and 25 deletions

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.111 2004/09/02 20:05:40 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.112 2004/09/16 03:15:52 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@ -896,7 +896,7 @@ array_out(PG_FUNCTION_ARGS)
k,
indx[MAXDIM];
int ndim,
*dim,
*dims,
*lb;
ArrayMetaState *my_extra;
@ -937,9 +937,9 @@ array_out(PG_FUNCTION_ARGS)
typioparam = my_extra->typioparam;
ndim = ARR_NDIM(v);
dim = ARR_DIMS(v);
dims = ARR_DIMS(v);
lb = ARR_LBOUND(v);
nitems = ArrayGetNItems(ndim, dim);
nitems = ArrayGetNItems(ndim, dims);
if (nitems == 0)
{
@ -968,11 +968,12 @@ array_out(PG_FUNCTION_ARGS)
values = (char **) palloc(nitems * sizeof(char *));
needquotes = (bool *) palloc(nitems * sizeof(bool));
p = ARR_DATA_PTR(v);
overall_length = 1; /* [TRH] don't forget to count \0 at end. */
overall_length = 1; /* don't forget to count \0 at end. */
for (i = 0; i < nitems; i++)
{
Datum itemvalue;
bool nq;
bool needquote;
itemvalue = fetch_att(p, typbyval, typlen);
values[i] = DatumGetCString(FunctionCall3(&my_extra->proc,
@ -983,28 +984,32 @@ array_out(PG_FUNCTION_ARGS)
p = (char *) att_align(p, typalign);
/* count data plus backslashes; detect chars needing quotes */
nq = (values[i][0] == '\0'); /* force quotes for empty string */
for (tmp = values[i]; *tmp; tmp++)
if (values[i][0] == '\0')
needquote = true; /* force quotes for empty string */
else
needquote = false;
for (tmp = values[i]; *tmp != '\0'; tmp++)
{
char ch = *tmp;
overall_length += 1;
if (ch == '"' || ch == '\\')
{
nq = true;
needquote = true;
#ifndef TCL_ARRAYS
overall_length += 1;
#endif
}
else if (ch == '{' || ch == '}' || ch == typdelim ||
isspace((unsigned char) ch))
nq = true;
needquote = true;
}
needquotes[i] = nq;
needquotes[i] = needquote;
/* Count the pair of double quotes, if needed */
if (nq)
if (needquote)
overall_length += 2;
/* and the comma */
@ -1014,7 +1019,10 @@ array_out(PG_FUNCTION_ARGS)
/*
* count total number of curly braces in output string
*/
for (i = j = 0, k = 1; i < ndim; k *= dim[i++], j += k);
for (i = j = 0, k = 1; i < ndim; i++)
k *= dims[i], j += k;
dims_str[0] = '\0';
/* add explicit dimensions if required */
if (needdims)
@ -1023,7 +1031,7 @@ array_out(PG_FUNCTION_ARGS)
for (i = 0; i < ndim; i++)
{
sprintf(ptr, "[%d:%d]", lb[i], lb[i] + dim[i] - 1);
sprintf(ptr, "[%d:%d]", lb[i], lb[i] + dims[i] - 1);
ptr += strlen(ptr);
}
*ptr++ = *ASSGN;
@ -1039,7 +1047,8 @@ array_out(PG_FUNCTION_ARGS)
if (needdims)
APPENDSTR(dims_str);
APPENDCHAR('{');
for (i = 0; i < ndim; indx[i++] = 0);
for (i = 0; i < ndim; i++)
indx[i] = 0;
j = 0;
k = 0;
do
@ -1071,7 +1080,7 @@ array_out(PG_FUNCTION_ARGS)
for (i = ndim - 1; i >= 0; i--)
{
indx[i] = (indx[i] + 1) % dim[i];
indx[i] = (indx[i] + 1) % dims[i];
if (indx[i])
{
APPENDCHAR(typdelim);

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.16 2004/08/29 04:12:51 momjian Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.17 2004/09/16 03:15:52 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@ -50,16 +50,16 @@ ArrayGetOffset0(int n, int *tup, int *scale)
/* Convert array dimensions into number of elements */
int
ArrayGetNItems(int n, int *a)
ArrayGetNItems(int ndim, int *dims)
{
int i,
ret;
if (n <= 0)
if (ndim <= 0)
return 0;
ret = 1;
for (i = 0; i < n; i++)
ret *= a[i];
for (i = 0; i < ndim; i++)
ret *= dims[i];
return ret;
}

View File

@ -10,7 +10,7 @@
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.49 2004/08/29 04:13:10 momjian Exp $
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.50 2004/09/16 03:15:54 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@ -81,8 +81,8 @@ typedef struct ArrayMetaState
*
* ARR_LBOUND returns a pointer to an array of array lower bounds.
*
* That is: if the third axis of an array has elements 5 through 10, then
* ARR_DIMS(a)[2] == 6 and ARR_LBOUND(a)[2] == 5.
* That is: if the third axis of an array has elements 5 through 8, then
* ARR_DIMS(a)[2] == 4 and ARR_LBOUND(a)[2] == 5.
*
* Unlike C, the default lower bound is 1.
*/
@ -176,7 +176,7 @@ extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
extern int ArrayGetOffset(int n, int *dim, int *lb, int *indx);
extern int ArrayGetOffset0(int n, int *tup, int *scale);
extern int ArrayGetNItems(int n, int *a);
extern int ArrayGetNItems(int ndims, int *dims);
extern void mda_get_range(int n, int *span, int *st, int *endp);
extern void mda_get_prod(int n, int *range, int *prod);
extern void mda_get_offset_values(int n, int *dist, int *prod, int *span);