Skip to content

Commit 4f841ce

Browse files
author
Amit Kapila
committed
Use strlcpy instead of memcpy for copying the slot name in pgstat.c.
There is no outright bug here but it is better to be consistent with the usage at other places in the same file. In the passing, fix a wrong assertion in pgstat_recv_replslot. Author: Kyotaro Horiguchi Reviewed-by: Sawada Masahiko and Amit Kapila Discussion: https://postgr.es/m/20201104.175523.1704166915688949637.horikyota.ntt@gmail.com
1 parent efc5dcf commit 4f841ce

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ pgstat_reset_replslot_counter(const char *name)
14891489
if (SlotIsPhysical(slot))
14901490
return;
14911491

1492-
memcpy(&msg.m_slotname, name, NAMEDATALEN);
1492+
strlcpy(msg.m_slotname, name, NAMEDATALEN);
14931493
msg.clearall = false;
14941494
}
14951495
else
@@ -1716,7 +1716,7 @@ pgstat_report_replslot(const char *slotname, int spilltxns, int spillcount,
17161716
* Prepare and send the message
17171717
*/
17181718
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
1719-
memcpy(&msg.m_slotname, slotname, NAMEDATALEN);
1719+
strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
17201720
msg.m_drop = false;
17211721
msg.m_spill_txns = spilltxns;
17221722
msg.m_spill_count = spillcount;
@@ -1739,7 +1739,7 @@ pgstat_report_replslot_drop(const char *slotname)
17391739
PgStat_MsgReplSlot msg;
17401740

17411741
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
1742-
memcpy(&msg.m_slotname, slotname, NAMEDATALEN);
1742+
strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
17431743
msg.m_drop = true;
17441744
pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
17451745
}
@@ -6880,7 +6880,9 @@ pgstat_recv_replslot(PgStat_MsgReplSlot *msg, int len)
68806880
if (idx < 0)
68816881
return;
68826882

6883-
Assert(idx >= 0 && idx <= max_replication_slots);
6883+
/* it must be a valid replication slot index */
6884+
Assert(idx >= 0 && idx < max_replication_slots);
6885+
68846886
if (msg->m_drop)
68856887
{
68866888
/* Remove the replication slot statistics with the given name */
@@ -7113,7 +7115,7 @@ pgstat_replslot_index(const char *name, bool create_it)
71137115

71147116
/* Register new slot */
71157117
memset(&replSlotStats[nReplSlotStats], 0, sizeof(PgStat_ReplSlotStats));
7116-
memcpy(&replSlotStats[nReplSlotStats].slotname, name, NAMEDATALEN);
7118+
strlcpy(replSlotStats[nReplSlotStats].slotname, name, NAMEDATALEN);
71177119

71187120
return nReplSlotStats++;
71197121
}

0 commit comments

Comments
 (0)