88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.87 2003/08 /0402:39:58 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.88 2003/09 /0415: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 */
7678static Acl *
7779merge_acl_with_grant (Acl * old_acl ,bool is_grant ,
@@ -92,24 +94,25 @@ merge_acl_with_grant(Acl *old_acl, bool is_grant,
9294foreach (j ,grantees )
9395{
9496PrivGrantee * grantee = (PrivGrantee * )lfirst (j );
95- AclItem aclitem ;
97+ AclItem aclitem ;
9698uint32 idtype ;
99+ Acl * newer_acl ;
97100
98101if (grantee -> username )
99102{
100- aclitem .ai_grantee = get_usesysid (grantee -> username );
103+ aclitem .ai_grantee = get_usesysid (grantee -> username );
101104
102105idtype = ACL_IDTYPE_UID ;
103106}
104107else if (grantee -> groupname )
105108{
106- aclitem .ai_grantee = get_grosysid (grantee -> groupname );
109+ aclitem .ai_grantee = get_grosysid (grantee -> groupname );
107110
108111idtype = ACL_IDTYPE_GID ;
109112}
110113else
111114{
112- aclitem .ai_grantee = ACL_ID_WORLD ;
115+ aclitem .ai_grantee = ACL_ID_WORLD ;
113116
114117idtype = ACL_IDTYPE_WORLD ;
115118}
@@ -126,14 +129,18 @@ merge_acl_with_grant(Acl *old_acl, bool is_grant,
126129(errcode (ERRCODE_INVALID_GRANT_OPERATION ),
127130errmsg ("grant options can only be granted to individual users" )));
128131
129- aclitem .ai_grantor = GetUserId ();
132+ aclitem .ai_grantor = GetUserId ();
130133
131134ACLITEM_SET_PRIVS_IDTYPE (aclitem ,
132135(is_grant || !grant_option ) ?privileges :ACL_NO_RIGHTS ,
133136(grant_option || !is_grant ) ?privileges :ACL_NO_RIGHTS ,
134137idtype );
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
139146dumpacl (new_acl );
@@ -269,7 +276,6 @@ ExecuteGrantStmt_Relation(GrantStmt *stmt)
269276/* keep the catalog indexes up to date */
270277CatalogUpdateIndexes (relation ,newtuple );
271278
272- pfree (old_acl );
273279pfree (new_acl );
274280
275281heap_close (relation ,RowExclusiveLock );
@@ -366,7 +372,6 @@ ExecuteGrantStmt_Database(GrantStmt *stmt)
366372/* keep the catalog indexes up to date */
367373CatalogUpdateIndexes (relation ,newtuple );
368374
369- pfree (old_acl );
370375pfree (new_acl );
371376
372377heap_endscan (scan );
@@ -465,7 +470,6 @@ ExecuteGrantStmt_Function(GrantStmt *stmt)
465470/* keep the catalog indexes up to date */
466471CatalogUpdateIndexes (relation ,newtuple );
467472
468- pfree (old_acl );
469473pfree (new_acl );
470474
471475heap_close (relation ,RowExclusiveLock );
@@ -565,7 +569,6 @@ ExecuteGrantStmt_Language(GrantStmt *stmt)
565569/* keep the catalog indexes up to date */
566570CatalogUpdateIndexes (relation ,newtuple );
567571
568- pfree (old_acl );
569572pfree (new_acl );
570573
571574heap_close (relation ,RowExclusiveLock );
@@ -662,7 +665,6 @@ ExecuteGrantStmt_Namespace(GrantStmt *stmt)
662665/* keep the catalog indexes up to date */
663666CatalogUpdateIndexes (relation ,newtuple );
664667
665- pfree (old_acl );
666668pfree (new_acl );
667669
668670heap_close (relation ,RowExclusiveLock );