Skip to content

Commit 025e7e1

Browse files
Remove count_one_bits() in acl.c.
The only caller, select_best_grantor(), can instead use pg_popcount64(). This isn't performance-critical code, but we might as well use the centralized implementation. While at it, add some test coverage for this part of select_best_grantor(). Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/Z9GtL7Nm6hsYyJnF%40nathan
1 parent ff79b5b commit 025e7e1

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

src/backend/utils/adt/acl.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5432,24 +5432,6 @@ select_best_admin(Oid member, Oid role)
54325432
return admin_role;
54335433
}
54345434

5435-
5436-
/* does what it says ... */
5437-
static int
5438-
count_one_bits(AclMode mask)
5439-
{
5440-
int nbits = 0;
5441-
5442-
/* this code relies on AclMode being an unsigned type */
5443-
while (mask)
5444-
{
5445-
if (mask & 1)
5446-
nbits++;
5447-
mask >>= 1;
5448-
}
5449-
return nbits;
5450-
}
5451-
5452-
54535435
/*
54545436
* Select the effective grantor ID for a GRANT or REVOKE operation.
54555437
*
@@ -5532,7 +5514,7 @@ select_best_grantor(Oid roleId, AclMode privileges,
55325514
*/
55335515
if (otherprivs != ACL_NO_RIGHTS)
55345516
{
5535-
int nnewrights = count_one_bits(otherprivs);
5517+
int nnewrights = pg_popcount64(otherprivs);
55365518

55375519
if (nnewrights > nrights)
55385520
{

src/test/regress/expected/privileges.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,3 +3293,30 @@ DROP SCHEMA reindex_test;
32933293
DROP ROLE regress_no_maintain;
32943294
DROP ROLE regress_maintain;
32953295
DROP ROLE regress_maintain_all;
3296+
-- grantor selection
3297+
CREATE ROLE regress_grantor1;
3298+
CREATE ROLE regress_grantor2 ROLE regress_grantor1;
3299+
CREATE ROLE regress_grantor3;
3300+
CREATE TABLE grantor_test1 ();
3301+
CREATE TABLE grantor_test2 ();
3302+
CREATE TABLE grantor_test3 ();
3303+
GRANT SELECT ON grantor_test2 TO regress_grantor1 WITH GRANT OPTION;
3304+
GRANT SELECT, UPDATE ON grantor_test3 TO regress_grantor2 WITH GRANT OPTION;
3305+
SET ROLE regress_grantor1;
3306+
GRANT SELECT, UPDATE ON grantor_test1 TO regress_grantor3;
3307+
ERROR: permission denied for table grantor_test1
3308+
GRANT SELECT, UPDATE ON grantor_test2 TO regress_grantor3;
3309+
WARNING: not all privileges were granted for "grantor_test2"
3310+
GRANT SELECT, UPDATE ON grantor_test3 TO regress_grantor3;
3311+
RESET ROLE;
3312+
SELECT * FROM information_schema.table_privileges t
3313+
WHERE grantor LIKE 'regress_grantor%' ORDER BY ROW(t.*);
3314+
grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy
3315+
------------------+------------------+---------------+--------------+---------------+----------------+--------------+----------------
3316+
regress_grantor1 | regress_grantor3 | regression | public | grantor_test2 | SELECT | NO | YES
3317+
regress_grantor2 | regress_grantor3 | regression | public | grantor_test3 | SELECT | NO | YES
3318+
regress_grantor2 | regress_grantor3 | regression | public | grantor_test3 | UPDATE | NO | NO
3319+
(3 rows)
3320+
3321+
DROP TABLE grantor_test1, grantor_test2, grantor_test3;
3322+
DROP ROLE regress_grantor1, regress_grantor2, regress_grantor3;

src/test/regress/sql/privileges.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,3 +2042,25 @@ DROP SCHEMA reindex_test;
20422042
DROP ROLE regress_no_maintain;
20432043
DROP ROLE regress_maintain;
20442044
DROP ROLE regress_maintain_all;
2045+
2046+
-- grantor selection
2047+
CREATE ROLE regress_grantor1;
2048+
CREATE ROLE regress_grantor2 ROLE regress_grantor1;
2049+
CREATE ROLE regress_grantor3;
2050+
CREATE TABLE grantor_test1 ();
2051+
CREATE TABLE grantor_test2 ();
2052+
CREATE TABLE grantor_test3 ();
2053+
GRANT SELECT ON grantor_test2 TO regress_grantor1 WITH GRANT OPTION;
2054+
GRANT SELECT, UPDATE ON grantor_test3 TO regress_grantor2 WITH GRANT OPTION;
2055+
2056+
SET ROLE regress_grantor1;
2057+
GRANT SELECT, UPDATE ON grantor_test1 TO regress_grantor3;
2058+
GRANT SELECT, UPDATE ON grantor_test2 TO regress_grantor3;
2059+
GRANT SELECT, UPDATE ON grantor_test3 TO regress_grantor3;
2060+
RESET ROLE;
2061+
2062+
SELECT * FROM information_schema.table_privileges t
2063+
WHERE grantor LIKE 'regress_grantor%' ORDER BY ROW(t.*);
2064+
2065+
DROP TABLE grantor_test1, grantor_test2, grantor_test3;
2066+
DROP ROLE regress_grantor1, regress_grantor2, regress_grantor3;

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