summaryrefslogtreecommitdiff
path: root/src/backend/bootstrap/bootstrap.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-12-16 15:35:40 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2014-12-16 15:35:40 -0500
commit53960e7eb34618c96f4d17216e6a3f92ac98c749 (patch)
tree42318e3a6cb1fa443f9686582e82ee168f3b3ee8 /src/backend/bootstrap/bootstrap.c
parent3b750ec155be3b8d658eadd8effe4d3c31955852 (diff)
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.
Diffstat (limited to 'src/backend/bootstrap/bootstrap.c')
-rw-r--r--src/backend/bootstrap/bootstrap.c31
1 files changed, 13 insertions, 18 deletions
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index e954762e549..fc1b3c8b4c7 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -1037,38 +1037,33 @@ AllocateAttribute(void)
return attribute;
}
-/* ----------------
+/*
* MapArrayTypeName
- * XXX arrays of "basetype" are always "_basetype".
- * this is an evil hack inherited from rel. 3.1.
- * XXX array dimension is thrown away because we
- * don't support fixed-dimension arrays. again,
- * sickness from 3.1.
*
- * the string passed in must have a '[' character in it
+ * Given a type name, produce the corresponding array type name by prepending
+ * '_' and truncating as needed to fit in NAMEDATALEN-1 bytes. This is only
+ * used in bootstrap mode, so we can get away with assuming that the input is
+ * ASCII and we don't need multibyte-aware truncation.
*
- * the string returned is a pointer to static storage and should NOT
- * be freed by the CALLER.
- * ----------------
+ * The given string normally ends with '[]' or '[digits]'; we discard that.
+ *
+ * The result is a palloc'd string.
*/
char *
-MapArrayTypeName(char *s)
+MapArrayTypeName(const char *s)
{
int i,
j;
- static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN long */
+ char newStr[NAMEDATALEN];
- if (s == NULL || s[0] == '\0')
- return s;
-
- j = 1;
newStr[0] = '_';
- for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
+ j = 1;
+ for (i = 0; i < NAMEDATALEN - 2 && s[i] != '['; i++, j++)
newStr[j] = s[i];
newStr[j] = '\0';
- return newStr;
+ return pstrdup(newStr);
}