Use strlcpy instead of memcpy for copying the slot name in pgstat.c.
authorAmit Kapila <akapila@postgresql.org>
Fri, 6 Nov 2020 02:42:48 +0000 (08:12 +0530)
committerAmit Kapila <akapila@postgresql.org>
Fri, 6 Nov 2020 02:42:48 +0000 (08:12 +0530)
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

src/backend/postmaster/pgstat.c

index f1dca2f25b75ec601f43e8294beaf1d308a616be..e76e627c6b2b746c7e8ef94c2e820c5c15ca5259 100644 (file)
@@ -1489,7 +1489,7 @@ pgstat_reset_replslot_counter(const char *name)
        if (SlotIsPhysical(slot))
            return;
 
-       memcpy(&msg.m_slotname, name, NAMEDATALEN);
+       strlcpy(msg.m_slotname, name, NAMEDATALEN);
        msg.clearall = false;
    }
    else
@@ -1716,7 +1716,7 @@ pgstat_report_replslot(const char *slotname, int spilltxns, int spillcount,
     * Prepare and send the message
     */
    pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
-   memcpy(&msg.m_slotname, slotname, NAMEDATALEN);
+   strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
    msg.m_drop = false;
    msg.m_spill_txns = spilltxns;
    msg.m_spill_count = spillcount;
@@ -1739,7 +1739,7 @@ pgstat_report_replslot_drop(const char *slotname)
    PgStat_MsgReplSlot msg;
 
    pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_REPLSLOT);
-   memcpy(&msg.m_slotname, slotname, NAMEDATALEN);
+   strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
    msg.m_drop = true;
    pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
 }
@@ -6880,7 +6880,9 @@ pgstat_recv_replslot(PgStat_MsgReplSlot *msg, int len)
    if (idx < 0)
        return;
 
-   Assert(idx >= 0 && idx <= max_replication_slots);
+   /* it must be a valid replication slot index */
+   Assert(idx >= 0 && idx < max_replication_slots);
+
    if (msg->m_drop)
    {
        /* Remove the replication slot statistics with the given name */
@@ -7113,7 +7115,7 @@ pgstat_replslot_index(const char *name, bool create_it)
 
    /* Register new slot */
    memset(&replSlotStats[nReplSlotStats], 0, sizeof(PgStat_ReplSlotStats));
-   memcpy(&replSlotStats[nReplSlotStats].slotname, name, NAMEDATALEN);
+   strlcpy(replSlotStats[nReplSlotStats].slotname, name, NAMEDATALEN);
 
    return nReplSlotStats++;
 }