Unify parsing logic for command-line integer options

Enterprise / PostgreSQL - Michael Paquier [paquier.xyz] - 24 July 2021 09:35 UTC

Most of the integer options for command-line binaries now make use of a single routine able to do the job, fixing issues with the detection of sloppy values caused for example by the use of atoi(), that fails on strings beginning with numerical characters with junk trailing characters.

This commit cuts down the number of strings requiring translation by 26 per my count, switching the code to have two error types for invalid and out-of-range values instead.

Much more could be done here, with float or even int64 options, but int32 was the most appealing case as it is possible to rely on strtol() to do the job reliably. Note that there are some exceptions for now, like pg_ctl or pg_upgrade that use their own logging logic. A couple of negative TAP tests required some adjustments for the new errors generated.

pg_dump and pg_restore tracked the maximum number of parallel jobs within the option parsing. The code is refactored a bit to track that in the code dedicated to parallelism instead.

Author: Kyotaro Horiguchi, Michael Paquier

6f164e6d17 Unify parsing logic for command-line integer options
src/bin/pg_amcheck/pg_amcheck.c | 8 ++--
src/bin/pg_basebackup/pg_basebackup.c | 17 ++++-----
src/bin/pg_basebackup/pg_receivewal.c | 23 ++++--------
src/bin/pg_basebackup/pg_recvlogical.c | 25 +++++--------
src/bin/pg_checksums/Makefile | 3 ++
src/bin/pg_checksums/pg_checksums.c | 9 +++--
src/bin/pg_dump/parallel.h | 13 +++++++
src/bin/pg_dump/pg_dump.c | 47 ++++++-----------------
src/bin/pg_dump/pg_restore.c | 22 +++--------
src/bin/pg_dump/t/001_basic.pl | 20 +++++-----
src/bin/pgbench/pgbench.c | 60 ++++++++++--------------------
src/bin/pgbench/t/002_pgbench_no_server.pl | 34 +++++++++++------
src/bin/scripts/createuser.c | 13 +++----
src/bin/scripts/reindexdb.c | 9 ++---
src/bin/scripts/vacuumdb.c | 31 +++++----------
src/fe_utils/option_utils.c | 39 +++++++++++++++++++
src/include/fe_utils/option_utils.h | 3 ++
17 files changed, 177 insertions(+), 199 deletions(-)

Upstream: git.postgresql.org


  • Share