summaryrefslogtreecommitdiff
path: root/src/interfaces/libpq/fe-protocol3.c
diff options
context:
space:
mode:
authorJacob Champion <jchampion@postgresql.org>2025-11-24 09:59:38 -0800
committerJacob Champion <jchampion@postgresql.org>2025-11-24 09:59:38 -0800
commit8934f2136cd82333fd148954a13a8ab01f7bd7ef (patch)
tree3e92b92bb156dea504dcf76cd949d4b17998ea4a /src/interfaces/libpq/fe-protocol3.c
parentf1881c7dd32cf5f429f6bd525b5cbacef3bb9c99 (diff)
Add pg_add_size_overflow() and friends
Commit 600086f47 added (several bespoke copies of) size_t addition with overflow checks to libpq. Move this to common/int.h, along with its subtraction and multiplication counterparts. pg_neg_size_overflow() is intentionally omitted; I'm not sure we should add SSIZE_MAX to win32_port.h for the sake of a function with no callers. Reviewed-by: Chao Li <li.evan.chao@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/CAOYmi%2B%3D%2BpqUd2MUitvgW1pAJuXgG_TKCVc3_Ek7pe8z9nkf%2BAg%40mail.gmail.com
Diffstat (limited to 'src/interfaces/libpq/fe-protocol3.c')
-rw-r--r--src/interfaces/libpq/fe-protocol3.c27
1 files changed, 4 insertions, 23 deletions
diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c
index 838e42e661a..16f504a867d 100644
--- a/src/interfaces/libpq/fe-protocol3.c
+++ b/src/interfaces/libpq/fe-protocol3.c
@@ -25,6 +25,7 @@
#include <netinet/tcp.h>
#endif
+#include "common/int.h"
#include "libpq-fe.h"
#include "libpq-int.h"
#include "mb/pg_wchar.h"
@@ -2405,26 +2406,6 @@ pqBuildStartupPacket3(PGconn *conn, int *packetlen,
}
/*
- * Frontend version of the backend's add_size(), intended to be API-compatible
- * with the pg_add_*_overflow() helpers. Stores the result into *dst on success.
- * Returns true instead if the addition overflows.
- *
- * TODO: move to common/int.h
- */
-static bool
-add_size_overflow(size_t s1, size_t s2, size_t *dst)
-{
- size_t result;
-
- result = s1 + s2;
- if (result < s1 || result < s2)
- return true;
-
- *dst = result;
- return false;
-}
-
-/*
* Build a startup packet given a filled-in PGconn structure.
*
* We need to figure out how much space is needed, then fill it in.
@@ -2456,11 +2437,11 @@ build_startup_packet(const PGconn *conn, char *packet,
do { \
if (packet) \
strcpy(packet + packet_len, optname); \
- if (add_size_overflow(packet_len, strlen(optname) + 1, &packet_len)) \
+ if (pg_add_size_overflow(packet_len, strlen(optname) + 1, &packet_len)) \
return 0; \
if (packet) \
strcpy(packet + packet_len, optval); \
- if (add_size_overflow(packet_len, strlen(optval) + 1, &packet_len)) \
+ if (pg_add_size_overflow(packet_len, strlen(optval) + 1, &packet_len)) \
return 0; \
} while(0)
@@ -2496,7 +2477,7 @@ build_startup_packet(const PGconn *conn, char *packet,
/* Add trailing terminator */
if (packet)
packet[packet_len] = '\0';
- if (add_size_overflow(packet_len, 1, &packet_len))
+ if (pg_add_size_overflow(packet_len, 1, &packet_len))
return 0;
return packet_len;