Skip to content

Commit 287f6d2

Browse files
committed
Fix error case for CREATE ROLE ... IN ROLE.
CreateRole() was passing a Value node, not a RoleSpec node, for the newly-created role name when adding the role as a member of existing roles for the IN ROLE syntax. This mistake went unnoticed because the node in question is used only for error messages and is not accessed on non-error paths. In older pg versions (such as 9.5 where this was found), this results in an "unexpected node type" error in place of the real error. That node type check was removed at some point, after which the code would accidentally fail to fail on 64-bit platforms (on which accessing the Value node as if it were a RoleSpec would be mostly harmless) or give an "unexpected role type" error on 32-bit platforms. Fix the code to pass the correct node type, and add an lfirst_node assertion just in case. Per report on irc from user m1chelangelo. Backpatch all the way, because this error has been around for a long time.
1 parent 40d4bc5 commit 287f6d2

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/backend/commands/user.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,31 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
455455
/*
456456
* Add the new role to the specified existing roles.
457457
*/
458-
foreach(item, addroleto)
458+
if (addroleto)
459459
{
460-
RoleSpec *oldrole = lfirst(item);
461-
HeapTuple oldroletup = get_rolespec_tuple(oldrole);
462-
Oid oldroleid = HeapTupleGetOid(oldroletup);
463-
char *oldrolename = NameStr(((Form_pg_authid) GETSTRUCT(oldroletup))->rolname);
464-
465-
AddRoleMems(oldrolename, oldroleid,
466-
list_make1(makeString(stmt->role)),
467-
list_make1_oid(roleid),
468-
GetUserId(), false);
460+
RoleSpec *thisrole = makeNode(RoleSpec);
461+
List *thisrole_list = list_make1(thisrole);
462+
List *thisrole_oidlist = list_make1_oid(roleid);
463+
464+
thisrole->roletype = ROLESPEC_CSTRING;
465+
thisrole->rolename = stmt->role;
466+
thisrole->location = -1;
469467

470-
ReleaseSysCache(oldroletup);
468+
foreach(item, addroleto)
469+
{
470+
RoleSpec *oldrole = lfirst(item);
471+
HeapTuple oldroletup = get_rolespec_tuple(oldrole);
472+
Form_pg_authid oldroleform = (Form_pg_authid) GETSTRUCT(oldroletup);
473+
Oid oldroleid = HeapTupleGetOid(oldroletup);
474+
char *oldrolename = NameStr(oldroleform->rolname);
475+
476+
AddRoleMems(oldrolename, oldroleid,
477+
thisrole_list,
478+
thisrole_oidlist,
479+
GetUserId(), false);
480+
481+
ReleaseSysCache(oldroletup);
482+
}
471483
}
472484

473485
/*
@@ -1478,7 +1490,7 @@ AddRoleMems(const char *rolename, Oid roleid,
14781490

14791491
forboth(specitem, memberSpecs, iditem, memberIds)
14801492
{
1481-
RoleSpec *memberRole = lfirst(specitem);
1493+
RoleSpec *memberRole = lfirst_node(RoleSpec, specitem);
14821494
Oid memberid = lfirst_oid(iditem);
14831495
HeapTuple authmem_tuple;
14841496
HeapTuple tuple;

0 commit comments

Comments
 (0)