Skip to content

Commit 5c784d9

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 926da21 commit 5c784d9

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
@@ -104,7 +104,7 @@ insert { return(INSERT_TUPLE); }
104104
"toast" { return(XTOAST); }
105105

106106
{arrayid} {
107-
yylval.str = pstrdup(MapArrayTypeName(yytext));
107+
yylval.str = MapArrayTypeName(yytext);
108108
return(ID);
109109
}
110110
{id} {

src/backend/bootstrap/bootstrap.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,38 +1016,33 @@ AllocateAttribute(void)
10161016
return attribute;
10171017
}
10181018

1019-
/* ----------------
1019+
/*
10201020
* MapArrayTypeName
1021-
* XXX arrays of "basetype" are always "_basetype".
1022-
* this is an evil hack inherited from rel. 3.1.
1023-
* XXX array dimension is thrown away because we
1024-
* don't support fixed-dimension arrays. again,
1025-
* sickness from 3.1.
10261021
*
1027-
* the string passed in must have a '[' character in it
1022+
* Given a type name, produce the corresponding array type name by prepending
1023+
* '_' and truncating as needed to fit in NAMEDATALEN-1 bytes. This is only
1024+
* used in bootstrap mode, so we can get away with assuming that the input is
1025+
* ASCII and we don't need multibyte-aware truncation.
10281026
*
1029-
* the string returned is a pointer to static storage and should NOT
1030-
* be freed by the CALLER.
1031-
* ----------------
1027+
* The given string normally ends with '[]' or '[digits]'; we discard that.
1028+
*
1029+
* The result is a palloc'd string.
10321030
*/
10331031
char *
1034-
MapArrayTypeName(char *s)
1032+
MapArrayTypeName(const char *s)
10351033
{
10361034
int i,
10371035
j;
1038-
static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN long */
1036+
char newStr[NAMEDATALEN];
10391037

1040-
if (s == NULL || s[0] == '\0')
1041-
return s;
1042-
1043-
j = 1;
10441038
newStr[0] = '_';
1045-
for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1039+
j = 1;
1040+
for (i = 0; i < NAMEDATALEN - 2 && s[i] != '['; i++, j++)
10461041
newStr[j] = s[i];
10471042

10481043
newStr[j] = '\0';
10491044

1050-
return newStr;
1045+
return pstrdup(newStr);
10511046
}
10521047

10531048

src/include/bootstrap/bootstrap.h

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

54-
extern char *MapArrayTypeName(char *s);
54+
extern char *MapArrayTypeName(const char *s);
5555

5656
extern void index_register(Oid heap, Oid ind, IndexInfo *indexInfo);
5757
extern void build_indices(void);

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