summaryrefslogtreecommitdiff
path: root/src/interfaces/libpq/fe-print.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-print.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-print.c')
-rw-r--r--src/interfaces/libpq/fe-print.c36
1 files changed, 8 insertions, 28 deletions
diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c
index cc667c9bac9..fc153bee630 100644
--- a/src/interfaces/libpq/fe-print.c
+++ b/src/interfaces/libpq/fe-print.c
@@ -33,6 +33,7 @@
#endif
#endif
+#include "common/int.h"
#include "libpq-fe.h"
#include "libpq-int.h"
@@ -451,27 +452,6 @@ do_field(const PQprintOpt *po, const PGresult *res,
}
-/*
- * 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;
-}
-
-
static char *
do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax,
const char **fieldNames, unsigned char *fieldNotNum,
@@ -492,20 +472,20 @@ do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax,
for (; n < nFields; n++)
{
/* Field plus separator, plus 2 extra '-' in standard format. */
- if (add_size_overflow(tot, fieldMax[n], &tot) ||
- add_size_overflow(tot, fs_len, &tot) ||
- (po->standard && add_size_overflow(tot, 2, &tot)))
+ if (pg_add_size_overflow(tot, fieldMax[n], &tot) ||
+ pg_add_size_overflow(tot, fs_len, &tot) ||
+ (po->standard && pg_add_size_overflow(tot, 2, &tot)))
goto overflow;
}
if (po->standard)
{
/* An extra separator at the front and back. */
- if (add_size_overflow(tot, fs_len, &tot) ||
- add_size_overflow(tot, fs_len, &tot) ||
- add_size_overflow(tot, 2, &tot))
+ if (pg_add_size_overflow(tot, fs_len, &tot) ||
+ pg_add_size_overflow(tot, fs_len, &tot) ||
+ pg_add_size_overflow(tot, 2, &tot))
goto overflow;
}
- if (add_size_overflow(tot, 1, &tot)) /* terminator */
+ if (pg_add_size_overflow(tot, 1, &tot)) /* terminator */
goto overflow;
border = malloc(tot);