From 6a2869f64e5a61f003eaced3f521ea0d748d3a50 Mon Sep 17 00:00:00 2001 From: Neil Conway Date: Thu, 16 Sep 2004 03:15:54 +0000 Subject: [PATCH] Fix a read of uninitialized memory in array_out(). Perform some minor cosmetic code cleanup at the same time. --- src/backend/utils/adt/arrayfuncs.c | 41 +++++++++++++++++++++++--------------- src/backend/utils/adt/arrayutils.c | 10 +++++----- src/include/utils/array.h | 8 ++++---- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 3918fb0484..eb4dd6de33 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -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); diff --git a/src/backend/utils/adt/arrayutils.c b/src/backend/utils/adt/arrayutils.c index 15a4fb54ed..41cd8f5549 100644 --- a/src/backend/utils/adt/arrayutils.c +++ b/src/backend/utils/adt/arrayutils.c @@ -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; } diff --git a/src/include/utils/array.h b/src/include/utils/array.h index 81db5709cb..88c40a3ec9 100644 --- a/src/include/utils/array.h +++ b/src/include/utils/array.h @@ -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); -- 2.11.0