]> granicus.if.org Git - postgresql/commitdiff
Be pickier about converting between Name and Datum.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 13 Sep 2016 21:17:48 +0000 (17:17 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 13 Sep 2016 21:17:48 +0000 (17:17 -0400)
We were misapplying NameGetDatum() to plain C strings in some places.
This worked, because it was just a pointer cast anyway, but it's a type
cheat in some sense.  Use CStringGetDatum instead, and modify the
NameGetDatum macro so it won't compile if applied to something that's
not a pointer to NameData.  This should result in no changes to
generated code, but it is logically cleaner.

Mark Dilger, tweaked a bit by me

Discussion: <EFD8AC94-4C1F-40C1-A5EA-304080089C1B@gmail.com>

src/backend/commands/dbcommands.c
src/backend/commands/proclang.c
src/backend/commands/typecmds.c
src/include/postgres.h

index ef486593c0034f074feab8d16c7a4a4b55fca9c7..0919ad8dfe73983feea948929343d486a3670712 100644 (file)
@@ -1260,7 +1260,7 @@ movedb(const char *dbname, const char *tblspcname)
                ScanKeyInit(&scankey,
                                        Anum_pg_database_datname,
                                        BTEqualStrategyNumber, F_NAMEEQ,
-                                       NameGetDatum(dbname));
+                                       CStringGetDatum(dbname));
                sysscan = systable_beginscan(pgdbrel, DatabaseNameIndexId, true,
                                                                         NULL, 1, &scankey);
                oldtuple = systable_getnext(sysscan);
@@ -1486,7 +1486,7 @@ AlterDatabase(ParseState *pstate, AlterDatabaseStmt *stmt, bool isTopLevel)
        ScanKeyInit(&scankey,
                                Anum_pg_database_datname,
                                BTEqualStrategyNumber, F_NAMEEQ,
-                               NameGetDatum(stmt->dbname));
+                               CStringGetDatum(stmt->dbname));
        scan = systable_beginscan(rel, DatabaseNameIndexId, true,
                                                          NULL, 1, &scankey);
        tuple = systable_getnext(scan);
@@ -1603,7 +1603,7 @@ AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
        ScanKeyInit(&scankey,
                                Anum_pg_database_datname,
                                BTEqualStrategyNumber, F_NAMEEQ,
-                               NameGetDatum(dbname));
+                               CStringGetDatum(dbname));
        scan = systable_beginscan(rel, DatabaseNameIndexId, true,
                                                          NULL, 1, &scankey);
        tuple = systable_getnext(scan);
@@ -1743,7 +1743,7 @@ get_db_info(const char *name, LOCKMODE lockmode,
                ScanKeyInit(&scanKey,
                                        Anum_pg_database_datname,
                                        BTEqualStrategyNumber, F_NAMEEQ,
-                                       NameGetDatum(name));
+                                       CStringGetDatum(name));
 
                scan = systable_beginscan(relation, DatabaseNameIndexId, true,
                                                                  NULL, 1, &scanKey);
index 761d08f604b66d77e0d7dbfb53ce8e2e45dee397..4f870e8d300fdbbf78fe74f747a049d7d1382d8c 100644 (file)
@@ -463,7 +463,7 @@ find_language_template(const char *languageName)
        ScanKeyInit(&key,
                                Anum_pg_pltemplate_tmplname,
                                BTEqualStrategyNumber, F_NAMEEQ,
-                               NameGetDatum(languageName));
+                               CStringGetDatum(languageName));
        scan = systable_beginscan(rel, PLTemplateNameIndexId, true,
                                                          NULL, 1, &key);
 
index 41fd2dae7f8c30d8803f48d497ecdc7aacae4f87..056933a5845c73f177a8ccd0ef3dee8cb00033a6 100644 (file)
@@ -3503,7 +3503,7 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
 
                /* check for duplicate name (more friendly than unique-index failure) */
                if (SearchSysCacheExists2(TYPENAMENSP,
-                                                                 CStringGetDatum(NameStr(typform->typname)),
+                                                                 NameGetDatum(&typform->typname),
                                                                  ObjectIdGetDatum(nspOid)))
                        ereport(ERROR,
                                        (errcode(ERRCODE_DUPLICATE_OBJECT),
index d999013238a8821ff8ab649341e6a76587dc6302..be4d0d609eac05c1dbd6733ca0cece8f6749fd56 100644 (file)
@@ -600,7 +600,7 @@ typedef Datum *DatumPtr;
  * value has adequate lifetime.
  */
 
-#define NameGetDatum(X) PointerGetDatum(X)
+#define NameGetDatum(X) CStringGetDatum(NameStr(*(X)))
 
 /*
  * DatumGetInt64