Skip to content

Commit e92c67d

Browse files
committed
Fix off-by-one loop count in MapArrayTypeName, and get rid of static array.
MapArrayTypeName would copy up to NAMEDATALEN-1 bytes of the base type name, which of course is wrong: after prepending '_' there is only room for NAMEDATALEN-2 bytes. Aside from being the wrong result, this case would lead to overrunning the statically allocated work buffer. This would be a security bug if the function were ever used outside bootstrap mode, but it isn't, at least not in any currently supported branches. Aside from fixing the off-by-one loop logic, this patch gets rid of the static work buffer by having MapArrayTypeName pstrdup its result; the sole caller was already doing that, so this just requires moving the pstrdup call. This saves a few bytes but mainly it makes the API a lot cleaner. Back-patch on the off chance that there is some third-party code using MapArrayTypeName with less-secure input. Pushing pstrdup into the function should not cause any serious problems for such hypothetical code; at worst there might be a short term memory leak. Per Coverity scanning.
1 parent 5b2c8f0 commit e92c67d

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/backend/bootstrap/bootscanner.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ insert { return(INSERT_TUPLE); }
105105
"toast" { return(XTOAST); }
106106

107107
{arrayid} {
108-
yylval.str = pstrdup(MapArrayTypeName(yytext));
108+
yylval.str = MapArrayTypeName(yytext);
109109
return(ID);
110110
}
111111
{id} {

src/backend/bootstrap/bootstrap.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,38 +1026,33 @@ AllocateAttribute(void)
10261026
return attribute;
10271027
}
10281028

1029-
/* ----------------
1029+
/*
10301030
* MapArrayTypeName
1031-
* XXX arrays of "basetype" are always "_basetype".
1032-
* this is an evil hack inherited from rel. 3.1.
1033-
* XXX array dimension is thrown away because we
1034-
* don't support fixed-dimension arrays. again,
1035-
* sickness from 3.1.
10361031
*
1037-
* the string passed in must have a '[' character in it
1032+
* Given a type name, produce the corresponding array type name by prepending
1033+
* '_' and truncating as needed to fit in NAMEDATALEN-1 bytes. This is only
1034+
* used in bootstrap mode, so we can get away with assuming that the input is
1035+
* ASCII and we don't need multibyte-aware truncation.
10381036
*
1039-
* the string returned is a pointer to static storage and should NOT
1040-
* be freed by the CALLER.
1041-
* ----------------
1037+
* The given string normally ends with '[]' or '[digits]'; we discard that.
1038+
*
1039+
* The result is a palloc'd string.
10421040
*/
10431041
char *
1044-
MapArrayTypeName(char *s)
1042+
MapArrayTypeName(const char *s)
10451043
{
10461044
int i,
10471045
j;
1048-
static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN long */
1046+
char newStr[NAMEDATALEN];
10491047

1050-
if (s == NULL || s[0] == '\0')
1051-
return s;
1052-
1053-
j = 1;
10541048
newStr[0] = '_';
1055-
for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1049+
j = 1;
1050+
for (i = 0; i < NAMEDATALEN - 2 && s[i] != '['; i++, j++)
10561051
newStr[j] = s[i];
10571052

10581053
newStr[j] = '\0';
10591054

1060-
return newStr;
1055+
return pstrdup(newStr);
10611056
}
10621057

10631058

src/include/bootstrap/bootstrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern void InsertOneTuple(Oid objectid);
4040
extern void InsertOneValue(char *value, int i);
4141
extern void InsertOneNull(int i);
4242

43-
extern char *MapArrayTypeName(char *s);
43+
extern char *MapArrayTypeName(const char *s);
4444

4545
extern void index_register(Oid heap, Oid ind, IndexInfo *indexInfo);
4646
extern void build_indices(void);

0 commit comments

Comments
 (0)