Skip to content

Commit 6a2869f

Browse files
author
Neil Conway
committed
Fix a read of uninitialized memory in array_out(). Perform some minor
cosmetic code cleanup at the same time.
1 parent d1b0d96 commit 6a2869f

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

src/backend/utils/adt/arrayfuncs.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.111 2004/09/02 20:05:40 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.112 2004/09/16 03:15:52 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -896,7 +896,7 @@ array_out(PG_FUNCTION_ARGS)
896896
k,
897897
indx[MAXDIM];
898898
int ndim,
899-
*dim,
899+
*dims,
900900
*lb;
901901
ArrayMetaState *my_extra;
902902

@@ -937,9 +937,9 @@ array_out(PG_FUNCTION_ARGS)
937937
typioparam = my_extra->typioparam;
938938

939939
ndim = ARR_NDIM(v);
940-
dim = ARR_DIMS(v);
940+
dims = ARR_DIMS(v);
941941
lb = ARR_LBOUND(v);
942-
nitems = ArrayGetNItems(ndim, dim);
942+
nitems = ArrayGetNItems(ndim, dims);
943943

944944
if (nitems == 0)
945945
{
@@ -968,11 +968,12 @@ array_out(PG_FUNCTION_ARGS)
968968
values = (char **) palloc(nitems * sizeof(char *));
969969
needquotes = (bool *) palloc(nitems * sizeof(bool));
970970
p = ARR_DATA_PTR(v);
971-
overall_length = 1; /* [TRH] don't forget to count \0 at end. */
971+
overall_length = 1; /* don't forget to count \0 at end. */
972+
972973
for (i = 0; i < nitems; i++)
973974
{
974975
Datum itemvalue;
975-
bool nq;
976+
bool needquote;
976977

977978
itemvalue = fetch_att(p, typbyval, typlen);
978979
values[i] = DatumGetCString(FunctionCall3(&my_extra->proc,
@@ -983,28 +984,32 @@ array_out(PG_FUNCTION_ARGS)
983984
p = (char *) att_align(p, typalign);
984985

985986
/* count data plus backslashes; detect chars needing quotes */
986-
nq = (values[i][0] == '\0'); /* force quotes for empty string */
987-
for (tmp = values[i]; *tmp; tmp++)
987+
if (values[i][0] == '\0')
988+
needquote = true; /* force quotes for empty string */
989+
else
990+
needquote = false;
991+
992+
for (tmp = values[i]; *tmp != '\0'; tmp++)
988993
{
989994
char ch = *tmp;
990995

991996
overall_length += 1;
992997
if (ch == '"' || ch == '\\')
993998
{
994-
nq = true;
999+
needquote = true;
9951000
#ifndef TCL_ARRAYS
9961001
overall_length += 1;
9971002
#endif
9981003
}
9991004
else if (ch == '{' || ch == '}' || ch == typdelim ||
10001005
isspace((unsigned char) ch))
1001-
nq = true;
1006+
needquote = true;
10021007
}
10031008

1004-
needquotes[i] = nq;
1009+
needquotes[i] = needquote;
10051010

10061011
/* Count the pair of double quotes, if needed */
1007-
if (nq)
1012+
if (needquote)
10081013
overall_length += 2;
10091014

10101015
/* and the comma */
@@ -1014,7 +1019,10 @@ array_out(PG_FUNCTION_ARGS)
10141019
/*
10151020
* count total number of curly braces in output string
10161021
*/
1017-
for (i = j = 0, k = 1; i < ndim; k *= dim[i++], j += k);
1022+
for (i = j = 0, k = 1; i < ndim; i++)
1023+
k *= dims[i], j += k;
1024+
1025+
dims_str[0] = '\0';
10181026

10191027
/* add explicit dimensions if required */
10201028
if (needdims)
@@ -1023,7 +1031,7 @@ array_out(PG_FUNCTION_ARGS)
10231031

10241032
for (i = 0; i < ndim; i++)
10251033
{
1026-
sprintf(ptr, "[%d:%d]", lb[i], lb[i] + dim[i] - 1);
1034+
sprintf(ptr, "[%d:%d]", lb[i], lb[i] + dims[i] - 1);
10271035
ptr += strlen(ptr);
10281036
}
10291037
*ptr++ = *ASSGN;
@@ -1039,7 +1047,8 @@ array_out(PG_FUNCTION_ARGS)
10391047
if (needdims)
10401048
APPENDSTR(dims_str);
10411049
APPENDCHAR('{');
1042-
for (i = 0; i < ndim; indx[i++] = 0);
1050+
for (i = 0; i < ndim; i++)
1051+
indx[i] = 0;
10431052
j = 0;
10441053
k = 0;
10451054
do
@@ -1071,7 +1080,7 @@ array_out(PG_FUNCTION_ARGS)
10711080

10721081
for (i = ndim - 1; i >= 0; i--)
10731082
{
1074-
indx[i] = (indx[i] + 1) % dim[i];
1083+
indx[i] = (indx[i] + 1) % dims[i];
10751084
if (indx[i])
10761085
{
10771086
APPENDCHAR(typdelim);

src/backend/utils/adt/arrayutils.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.16 2004/08/29 04:12:51 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.17 2004/09/16 03:15:52 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -50,16 +50,16 @@ ArrayGetOffset0(int n, int *tup, int *scale)
5050

5151
/* Convert array dimensions into number of elements */
5252
int
53-
ArrayGetNItems(int n, int *a)
53+
ArrayGetNItems(int ndim, int *dims)
5454
{
5555
int i,
5656
ret;
5757

58-
if (n <= 0)
58+
if (ndim <= 0)
5959
return 0;
6060
ret = 1;
61-
for (i = 0; i < n; i++)
62-
ret *= a[i];
61+
for (i = 0; i < ndim; i++)
62+
ret *= dims[i];
6363
return ret;
6464
}
6565

src/include/utils/array.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
13-
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.49 2004/08/29 04:13:10 momjian Exp $
13+
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.50 2004/09/16 03:15:54 neilc Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -81,8 +81,8 @@ typedef struct ArrayMetaState
8181
*
8282
* ARR_LBOUND returns a pointer to an array of array lower bounds.
8383
*
84-
* That is: if the third axis of an array has elements 5 through 10, then
85-
* ARR_DIMS(a)[2] == 6 and ARR_LBOUND(a)[2] == 5.
84+
* That is: if the third axis of an array has elements 5 through 8, then
85+
* ARR_DIMS(a)[2] == 4 and ARR_LBOUND(a)[2] == 5.
8686
*
8787
* Unlike C, the default lower bound is 1.
8888
*/
@@ -176,7 +176,7 @@ extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
176176

177177
extern int ArrayGetOffset(int n, int *dim, int *lb, int *indx);
178178
extern int ArrayGetOffset0(int n, int *tup, int *scale);
179-
extern int ArrayGetNItems(int n, int *a);
179+
extern int ArrayGetNItems(int ndims, int *dims);
180180
extern void mda_get_range(int n, int *span, int *st, int *endp);
181181
extern void mda_get_prod(int n, int *range, int *prod);
182182
extern void mda_get_offset_values(int n, int *dist, int *prod, int *span);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy