Skip to content

Commit ca43f71

Browse files
committed
Avoid consuming unreasonable amounts of memory when GRANT has many
grantees.
1 parent fe055e9 commit ca43f71

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/backend/catalog/aclchk.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.87 2003/08/04 02:39:58 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.88 2003/09/04 15:53:04 tgl Exp $
1212
*
1313
* NOTES
1414
* See acl.h.
@@ -72,6 +72,8 @@ dumpacl(Acl *acl)
7272
* If is_grant is true, adds the given privileges for the list of
7373
* grantees to the existing old_acl. If is_grant is false, the
7474
* privileges for the given grantees are removed from old_acl.
75+
*
76+
* NB: the original old_acl is pfree'd.
7577
*/
7678
static Acl *
7779
merge_acl_with_grant(Acl *old_acl, bool is_grant,
@@ -92,24 +94,25 @@ merge_acl_with_grant(Acl *old_acl, bool is_grant,
9294
foreach(j, grantees)
9395
{
9496
PrivGrantee *grantee = (PrivGrantee *) lfirst(j);
95-
AclItem aclitem;
97+
AclItem aclitem;
9698
uint32 idtype;
99+
Acl *newer_acl;
97100

98101
if (grantee->username)
99102
{
100-
aclitem. ai_grantee = get_usesysid(grantee->username);
103+
aclitem.ai_grantee = get_usesysid(grantee->username);
101104

102105
idtype = ACL_IDTYPE_UID;
103106
}
104107
else if (grantee->groupname)
105108
{
106-
aclitem. ai_grantee = get_grosysid(grantee->groupname);
109+
aclitem.ai_grantee = get_grosysid(grantee->groupname);
107110

108111
idtype = ACL_IDTYPE_GID;
109112
}
110113
else
111114
{
112-
aclitem. ai_grantee = ACL_ID_WORLD;
115+
aclitem.ai_grantee = ACL_ID_WORLD;
113116

114117
idtype = ACL_IDTYPE_WORLD;
115118
}
@@ -126,14 +129,18 @@ merge_acl_with_grant(Acl *old_acl, bool is_grant,
126129
(errcode(ERRCODE_INVALID_GRANT_OPERATION),
127130
errmsg("grant options can only be granted to individual users")));
128131

129-
aclitem. ai_grantor = GetUserId();
132+
aclitem.ai_grantor = GetUserId();
130133

131134
ACLITEM_SET_PRIVS_IDTYPE(aclitem,
132135
(is_grant || !grant_option) ? privileges : ACL_NO_RIGHTS,
133136
(grant_option || !is_grant) ? privileges : ACL_NO_RIGHTS,
134137
idtype);
135138

136-
new_acl = aclinsert3(new_acl, &aclitem, modechg, behavior);
139+
newer_acl = aclinsert3(new_acl, &aclitem, modechg, behavior);
140+
141+
/* avoid memory leak when there are many grantees */
142+
pfree(new_acl);
143+
new_acl = newer_acl;
137144

138145
#ifdef ACLDEBUG
139146
dumpacl(new_acl);
@@ -269,7 +276,6 @@ ExecuteGrantStmt_Relation(GrantStmt *stmt)
269276
/* keep the catalog indexes up to date */
270277
CatalogUpdateIndexes(relation, newtuple);
271278

272-
pfree(old_acl);
273279
pfree(new_acl);
274280

275281
heap_close(relation, RowExclusiveLock);
@@ -366,7 +372,6 @@ ExecuteGrantStmt_Database(GrantStmt *stmt)
366372
/* keep the catalog indexes up to date */
367373
CatalogUpdateIndexes(relation, newtuple);
368374

369-
pfree(old_acl);
370375
pfree(new_acl);
371376

372377
heap_endscan(scan);
@@ -465,7 +470,6 @@ ExecuteGrantStmt_Function(GrantStmt *stmt)
465470
/* keep the catalog indexes up to date */
466471
CatalogUpdateIndexes(relation, newtuple);
467472

468-
pfree(old_acl);
469473
pfree(new_acl);
470474

471475
heap_close(relation, RowExclusiveLock);
@@ -565,7 +569,6 @@ ExecuteGrantStmt_Language(GrantStmt *stmt)
565569
/* keep the catalog indexes up to date */
566570
CatalogUpdateIndexes(relation, newtuple);
567571

568-
pfree(old_acl);
569572
pfree(new_acl);
570573

571574
heap_close(relation, RowExclusiveLock);
@@ -662,7 +665,6 @@ ExecuteGrantStmt_Namespace(GrantStmt *stmt)
662665
/* keep the catalog indexes up to date */
663666
CatalogUpdateIndexes(relation, newtuple);
664667

665-
pfree(old_acl);
666668
pfree(new_acl);
667669

668670
heap_close(relation, RowExclusiveLock);

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