Skip to content

Commit eb4d24b

Browse files
author
Commitfest Bot
committed
[CF 6301] v3 - Safer hash table initialization macro
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/6301 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/DESS1U66SWHO.2JT1YL2Q20K0F@jeltef.nl Author(s): Bertrand Drouvot, Jelte Fennema-Nio
2 parents bd8d9c9 + ce85be4 commit eb4d24b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+890
-1366
lines changed

contrib/dblink/dblink.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,14 +1275,11 @@ PG_FUNCTION_INFO_V1(dblink_get_connections);
12751275
Datum
12761276
dblink_get_connections(PG_FUNCTION_ARGS)
12771277
{
1278-
HASH_SEQ_STATUS status;
1279-
remoteConnHashEnt *hentry;
12801278
ArrayBuildState *astate = NULL;
12811279

12821280
if (remoteConnHash)
12831281
{
1284-
hash_seq_init(&status, remoteConnHash);
1285-
while ((hentry = (remoteConnHashEnt *) hash_seq_search(&status)) != NULL)
1282+
foreach_hash(remoteConnHashEnt, hentry, remoteConnHash)
12861283
{
12871284
/* ignore it if it's not an open connection */
12881285
if (hentry->rconn.conn == NULL)
@@ -2540,13 +2537,9 @@ getConnectionByName(const char *name)
25402537
static HTAB *
25412538
createConnHash(void)
25422539
{
2543-
HASHCTL ctl;
2544-
2545-
ctl.keysize = NAMEDATALEN;
2546-
ctl.entrysize = sizeof(remoteConnHashEnt);
2547-
2548-
return hash_create("Remote Con hash", NUMCONN, &ctl,
2549-
HASH_ELEM | HASH_STRINGS);
2540+
return hash_make_cxt(remoteConnHashEnt, name,
2541+
"Remote Con hash", NUMCONN,
2542+
TopMemoryContext);
25502543
}
25512544

25522545
static remoteConn *

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ static void
517517
pgss_shmem_startup(void)
518518
{
519519
bool found;
520-
HASHCTL info;
521520
FILE *file = NULL;
522521
FILE *qfile = NULL;
523522
uint32 header;
@@ -557,12 +556,9 @@ pgss_shmem_startup(void)
557556
pgss->stats.stats_reset = GetCurrentTimestamp();
558557
}
559558

560-
info.keysize = sizeof(pgssHashKey);
561-
info.entrysize = sizeof(pgssEntry);
562-
pgss_hash = ShmemInitHash("pg_stat_statements hash",
563-
pgss_max, pgss_max,
564-
&info,
565-
HASH_ELEM | HASH_BLOBS);
559+
pgss_hash = shmem_hash_make(pgssEntry, key,
560+
"pg_stat_statements hash",
561+
pgss_max, pgss_max);
566562

567563
LWLockRelease(AddinShmemInitLock);
568564

@@ -745,9 +741,7 @@ pgss_shmem_shutdown(int code, Datum arg)
745741
FILE *file;
746742
char *qbuffer = NULL;
747743
Size qbuffer_size = 0;
748-
HASH_SEQ_STATUS hash_seq;
749744
int32 num_entries;
750-
pgssEntry *entry;
751745

752746
/* Don't try to dump during a crash. */
753747
if (code)
@@ -781,8 +775,7 @@ pgss_shmem_shutdown(int code, Datum arg)
781775
* When serializing to disk, we store query texts immediately after their
782776
* entry data. Any orphaned query texts are thereby excluded.
783777
*/
784-
hash_seq_init(&hash_seq, pgss_hash);
785-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
778+
foreach_hash(pgssEntry, entry, pgss_hash)
786779
{
787780
int len = entry->query_len;
788781
char *qstr = qtext_fetch(entry->query_offset, len,
@@ -794,8 +787,8 @@ pgss_shmem_shutdown(int code, Datum arg)
794787
if (fwrite(entry, sizeof(pgssEntry), 1, file) != 1 ||
795788
fwrite(qstr, 1, len + 1, file) != len + 1)
796789
{
797-
/* note: we assume hash_seq_term won't change errno */
798-
hash_seq_term(&hash_seq);
790+
/* note: we assume foreach_hash_term won't change errno */
791+
foreach_hash_term(entry);
799792
goto error;
800793
}
801794
}
@@ -1699,8 +1692,6 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
16991692
Size qbuffer_size = 0;
17001693
Size extent = 0;
17011694
int gc_count = 0;
1702-
HASH_SEQ_STATUS hash_seq;
1703-
pgssEntry *entry;
17041695

17051696
/*
17061697
* Superusers or roles with the privileges of pg_read_all_stats members
@@ -1829,8 +1820,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18291820
}
18301821
}
18311822

1832-
hash_seq_init(&hash_seq, pgss_hash);
1833-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
1823+
foreach_hash(pgssEntry, entry, pgss_hash)
18341824
{
18351825
Datum values[PG_STAT_STATEMENTS_COLS];
18361826
bool nulls[PG_STAT_STATEMENTS_COLS];
@@ -2174,9 +2164,7 @@ entry_cmp(const void *lhs, const void *rhs)
21742164
static void
21752165
entry_dealloc(void)
21762166
{
2177-
HASH_SEQ_STATUS hash_seq;
21782167
pgssEntry **entries;
2179-
pgssEntry *entry;
21802168
int nvictims;
21812169
int i;
21822170
Size tottextlen;
@@ -2200,8 +2188,7 @@ entry_dealloc(void)
22002188
tottextlen = 0;
22012189
nvalidtexts = 0;
22022190

2203-
hash_seq_init(&hash_seq, pgss_hash);
2204-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2191+
foreach_hash(pgssEntry, entry, pgss_hash)
22052192
{
22062193
entries[i++] = entry;
22072194
/* "Sticky" entries get a different usage decay rate. */
@@ -2513,8 +2500,6 @@ gc_qtexts(void)
25132500
char *qbuffer;
25142501
Size qbuffer_size;
25152502
FILE *qfile = NULL;
2516-
HASH_SEQ_STATUS hash_seq;
2517-
pgssEntry *entry;
25182503
Size extent;
25192504
int nentries;
25202505

@@ -2556,8 +2541,7 @@ gc_qtexts(void)
25562541
extent = 0;
25572542
nentries = 0;
25582543

2559-
hash_seq_init(&hash_seq, pgss_hash);
2560-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2544+
foreach_hash(pgssEntry, entry, pgss_hash)
25612545
{
25622546
int query_len = entry->query_len;
25632547
char *qry = qtext_fetch(entry->query_offset,
@@ -2580,7 +2564,7 @@ gc_qtexts(void)
25802564
(errcode_for_file_access(),
25812565
errmsg("could not write file \"%s\": %m",
25822566
PGSS_TEXT_FILE)));
2583-
hash_seq_term(&hash_seq);
2567+
foreach_hash_term(entry);
25842568
goto gc_fail;
25852569
}
25862570

@@ -2647,8 +2631,7 @@ gc_qtexts(void)
26472631
* Since the contents of the external file are now uncertain, mark all
26482632
* hashtable entries as having invalid texts.
26492633
*/
2650-
hash_seq_init(&hash_seq, pgss_hash);
2651-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2634+
foreach_hash(pgssEntry, entry, pgss_hash)
26522635
{
26532636
entry->query_offset = 0;
26542637
entry->query_len = -1;
@@ -2712,8 +2695,6 @@ if (e) { \
27122695
static TimestampTz
27132696
entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27142697
{
2715-
HASH_SEQ_STATUS hash_seq;
2716-
pgssEntry *entry;
27172698
FILE *qfile;
27182699
int64 num_entries;
27192700
int64 num_remove = 0;
@@ -2733,6 +2714,8 @@ entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27332714
if (userid != 0 && dbid != 0 && queryid != INT64CONST(0))
27342715
{
27352716
/* If all the parameters are available, use the fast path. */
2717+
pgssEntry *entry;
2718+
27362719
memset(&key, 0, sizeof(pgssHashKey));
27372720
key.userid = userid;
27382721
key.dbid = dbid;
@@ -2756,8 +2739,7 @@ entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27562739
else if (userid != 0 || dbid != 0 || queryid != INT64CONST(0))
27572740
{
27582741
/* Reset entries corresponding to valid parameters. */
2759-
hash_seq_init(&hash_seq, pgss_hash);
2760-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2742+
foreach_hash(pgssEntry, entry, pgss_hash)
27612743
{
27622744
if ((!userid || entry->key.userid == userid) &&
27632745
(!dbid || entry->key.dbid == dbid) &&
@@ -2770,8 +2752,7 @@ entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
27702752
else
27712753
{
27722754
/* Reset all entries. */
2773-
hash_seq_init(&hash_seq, pgss_hash);
2774-
while ((entry = hash_seq_search(&hash_seq)) != NULL)
2755+
foreach_hash(pgssEntry, entry, pgss_hash)
27752756
{
27762757
SINGLE_ENTRY_RESET(entry);
27772758
}

contrib/pg_trgm/trgm_regexp.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,6 @@ convertPgWchar(pg_wchar c, trgm_mb_char *result)
893893
static void
894894
transformGraph(TrgmNFA *trgmNFA)
895895
{
896-
HASHCTL hashCtl;
897896
TrgmStateKey initkey;
898897
TrgmState *initstate;
899898
ListCell *lc;
@@ -905,13 +904,7 @@ transformGraph(TrgmNFA *trgmNFA)
905904
trgmNFA->overflowed = false;
906905

907906
/* Create hashtable for states */
908-
hashCtl.keysize = sizeof(TrgmStateKey);
909-
hashCtl.entrysize = sizeof(TrgmState);
910-
hashCtl.hcxt = CurrentMemoryContext;
911-
trgmNFA->states = hash_create("Trigram NFA",
912-
1024,
913-
&hashCtl,
914-
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
907+
trgmNFA->states = hash_make(TrgmState, stateKey, "Trigram NFA", 1024);
915908
trgmNFA->nstates = 0;
916909

917910
/* Create initial state: ambiguous prefix, NFA's initial state */
@@ -1456,10 +1449,8 @@ prefixContains(TrgmPrefix *prefix1, TrgmPrefix *prefix2)
14561449
static bool
14571450
selectColorTrigrams(TrgmNFA *trgmNFA)
14581451
{
1459-
HASH_SEQ_STATUS scan_status;
14601452
int arcsCount = trgmNFA->arcsCount,
14611453
i;
1462-
TrgmState *state;
14631454
ColorTrgmInfo *colorTrgms;
14641455
int64 totalTrgmCount;
14651456
float4 totalTrgmPenalty;
@@ -1470,8 +1461,7 @@ selectColorTrigrams(TrgmNFA *trgmNFA)
14701461
trgmNFA->colorTrgms = colorTrgms;
14711462

14721463
i = 0;
1473-
hash_seq_init(&scan_status, trgmNFA->states);
1474-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
1464+
foreach_hash(TrgmState, state, trgmNFA->states)
14751465
{
14761466
ListCell *cell;
14771467

@@ -1933,17 +1923,14 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
19331923
int snumber = 2,
19341924
arcIndex,
19351925
arcsCount;
1936-
HASH_SEQ_STATUS scan_status;
1937-
TrgmState *state;
19381926
TrgmPackArcInfo *arcs;
19391927
TrgmPackedArc *packedArcs;
19401928
TrgmPackedGraph *result;
19411929
int i,
19421930
j;
19431931

19441932
/* Enumerate surviving states, giving init and fin reserved numbers */
1945-
hash_seq_init(&scan_status, trgmNFA->states);
1946-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
1933+
foreach_hash(TrgmState, state, trgmNFA->states)
19471934
{
19481935
while (state->parent)
19491936
state = state->parent;
@@ -1965,8 +1952,7 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
19651952
/* Collect array of all arcs */
19661953
arcs = palloc_array(TrgmPackArcInfo, trgmNFA->arcsCount);
19671954
arcIndex = 0;
1968-
hash_seq_init(&scan_status, trgmNFA->states);
1969-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
1955+
foreach_hash(TrgmState, state, trgmNFA->states)
19701956
{
19711957
TrgmState *source = state;
19721958
ListCell *cell;
@@ -2209,16 +2195,13 @@ static void
22092195
printTrgmNFA(TrgmNFA *trgmNFA)
22102196
{
22112197
StringInfoData buf;
2212-
HASH_SEQ_STATUS scan_status;
2213-
TrgmState *state;
22142198
TrgmState *initstate = NULL;
22152199

22162200
initStringInfo(&buf);
22172201

22182202
appendStringInfoString(&buf, "\ndigraph transformedNFA {\n");
22192203

2220-
hash_seq_init(&scan_status, trgmNFA->states);
2221-
while ((state = (TrgmState *) hash_seq_search(&scan_status)) != NULL)
2204+
foreach_hash(TrgmState, state, trgmNFA->states)
22222205
{
22232206
ListCell *cell;
22242207

0 commit comments

Comments
 (0)