From 7f2f860f74d364ac0df42f2adb0e888deb63409a Mon Sep 17 00:00:00 2001 From: totaj Date: Fri, 5 May 2023 11:11:48 +0800 Subject: [PATCH] Add negative option test for pg_resetxlog. --- src/bin/pg_resetxlog/pg_resetxlog.cpp | 31 +++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/bin/pg_resetxlog/pg_resetxlog.cpp b/src/bin/pg_resetxlog/pg_resetxlog.cpp index 5959b29157..f2a77a5c90 100644 --- a/src/bin/pg_resetxlog/pg_resetxlog.cpp +++ b/src/bin/pg_resetxlog/pg_resetxlog.cpp @@ -89,6 +89,15 @@ const uint64 FREEZE_MAX_AGE = 2000000000; /* DSS connect parameters */ static DssOptions dss; +static inline bool is_negative_num(char *str) +{ + char *s = str; + while (*s != '\0' && isspace(*s)) { + s++; + } + return *s == '-' ? true : false; +} + int main(int argc, char* argv[]) { int c; @@ -153,6 +162,10 @@ int main(int argc, char* argv[]) fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } + if (is_negative_num(optarg)) { + fprintf(stderr, _("%s: transaction ID epoch (-e) can't be negative.\n"), progname); + exit(1); + } break; case 'x': @@ -162,8 +175,14 @@ int main(int argc, char* argv[]) fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } - if (set_xid == 0) { - fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname); + if (is_negative_num(optarg)) { + fprintf(stderr, _("%s: transaction ID (-x) can't be negative.\n"), progname); + exit(1); + } + + if (!TransactionIdIsNormal(set_xid)) { + fprintf(stderr, _("%s: transaction ID (-x) must be greater than or equal to %lu.\n"), + progname, FirstNormalTransactionId); exit(1); } break; @@ -175,6 +194,10 @@ int main(int argc, char* argv[]) fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } + if (is_negative_num(optarg)) { + fprintf(stderr, _("%s: OID (-o) can't be negative.\n"), progname); + exit(1); + } set_oid = (Oid)tmpValue; if (set_oid == 0) { fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname); @@ -189,6 +212,10 @@ int main(int argc, char* argv[]) fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } + if (is_negative_num(optarg)) { + fprintf(stderr, _("%s: multitransaction ID (-m) can't be negative.\n"), progname); + exit(1); + } if (set_mxid == 0) { fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname); exit(1); -- Gitee