Skip to content

Commit

Permalink
Use NameData and namedatacpy for names
Browse files Browse the repository at this point in the history
Using `strlcpy` to copy variables holding PostgreSQL names can cause
issues since names are fixed-size types of length 64. This means that
any data that follows the initial null-terminated string will also be
part of the data.

Instead of using `const char*` for PostgreSQL names, use `NameData`
type for PostgreSQL names and use `namedatacpy` to copy them rather
than `strlcpy`.
  • Loading branch information
mkindahl committed Feb 15, 2023
1 parent 0963609 commit b1b0d66
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ accidentally triggering the load of a previous DB version.**

**Bugfixes**
* #4926 Fix corruption when inserting into compressed chunks
* #5218 Add role-level security to job error log
* #5214 Fix use of prepared statement in async module
* #5290 Compression can't be enabled on continuous aggregates when segmentby/orderby columns need quotation
* #5218 Add role-level security to job error log
* #5239 Fix next_start calculation for fixed schedules
* #5290 Compression can't be enabled on continuous aggregates when segmentby/orderby columns need quotation
* #5336 Use NameData and namedatacpy for names

## 2.9.3 (2023-02-03)

Expand Down
20 changes: 12 additions & 8 deletions src/partitioning.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ partitioning_func_set_func_fmgr(PartitioningFunc *pf, Oid argtype, DimensionType
if (dimtype != DIMENSION_TYPE_CLOSED && dimtype != DIMENSION_TYPE_OPEN)
elog(ERROR, "invalid dimension type %u", dimtype);

funcoid = ts_lookup_proc_filtered(pf->schema, pf->name, &pf->rettype, filter, &argtype);
funcoid = ts_lookup_proc_filtered(NameStr(pf->schema),
NameStr(pf->name),
&pf->rettype,
filter,
&argtype);

if (!OidIsValid(funcoid))
{
Expand All @@ -145,7 +149,7 @@ partitioning_func_set_func_fmgr(PartitioningFunc *pf, Oid argtype, DimensionType
List *
ts_partitioning_func_qualified_name(PartitioningFunc *pf)
{
return list_make2(makeString(pf->schema), makeString(pf->name));
return list_make2(makeString(NameStr(pf->schema)), makeString(NameStr(pf->name)));
}

static Oid
Expand Down Expand Up @@ -184,16 +188,16 @@ ts_partitioning_info_create(const char *schema, const char *partfunc, const char
errmsg("partitioning function information cannot be null")));

pinfo = palloc0(sizeof(PartitioningInfo));
strlcpy(pinfo->partfunc.name, partfunc, NAMEDATALEN);
strlcpy(pinfo->column, partcol, NAMEDATALEN);
pinfo->column_attnum = get_attnum(relid, pinfo->column);
namestrcpy(&pinfo->partfunc.name, partfunc);
namestrcpy(&pinfo->column, partcol);
pinfo->column_attnum = get_attnum(relid, NameStr(pinfo->column));
pinfo->dimtype = dimtype;

/* handle the case that the attribute has been dropped */
if (pinfo->column_attnum == InvalidAttrNumber)
return NULL;

strlcpy(pinfo->partfunc.schema, schema, NAMEDATALEN);
namestrcpy(&pinfo->partfunc.schema, schema);

/* Lookup the type cache entry to access the hash function for the type */
columntype = get_atttype(relid, pinfo->column_attnum);
Expand Down Expand Up @@ -250,8 +254,8 @@ ts_partitioning_func_apply(PartitioningInfo *pinfo, Oid collation, Datum value)
if (fcinfo->isnull)
elog(ERROR,
"partitioning function \"%s.%s\" returned NULL",
pinfo->partfunc.schema,
pinfo->partfunc.name);
NameStr(pinfo->partfunc.schema),
NameStr(pinfo->partfunc.name));

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/partitioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

typedef struct PartitioningFunc
{
char schema[NAMEDATALEN];
char name[NAMEDATALEN];
NameData schema;
NameData name;
Oid rettype;

/*
Expand All @@ -38,7 +38,7 @@ typedef struct PartitioningFunc

typedef struct PartitioningInfo
{
char column[NAMEDATALEN];
NameData column;
AttrNumber column_attnum;
DimensionType dimtype;
PartitioningFunc partfunc;
Expand Down
2 changes: 1 addition & 1 deletion src/ts_catalog/catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ static void
catalog_database_info_init(CatalogDatabaseInfo *info)
{
info->database_id = MyDatabaseId;
strlcpy(info->database_name, get_database_name(MyDatabaseId), NAMEDATALEN);
namestrcpy(&info->database_name, get_database_name(MyDatabaseId));
info->schema_id = get_namespace_oid(CATALOG_SCHEMA_NAME, false);
info->owner_uid = catalog_owner();

Expand Down
2 changes: 1 addition & 1 deletion src/ts_catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ typedef struct CatalogTableInfo

typedef struct CatalogDatabaseInfo
{
char database_name[NAMEDATALEN];
NameData database_name;
Oid database_id;
Oid schema_id;
Oid owner_uid;
Expand Down

0 comments on commit b1b0d66

Please sign in to comment.