aboutsummaryrefslogtreecommitdiff
path: root/src/bin/scripts/createuser.c
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2021-07-24 18:35:03 +0900
committerMichael Paquier <michael@paquier.xyz>2021-07-24 18:35:03 +0900
commit6f164e6d17616a157ea5d9e34dbb1b211c080c41 (patch)
tree211dc850cd3deb995555efd66b75899c2b37ae7a /src/bin/scripts/createuser.c
parent6beb38cfc9ddd4cd3d2eb5402981ebdd69a618b4 (diff)
downloadpostgresql-6f164e6d17616a157ea5d9e34dbb1b211c080c41.tar.gz
postgresql-6f164e6d17616a157ea5d9e34dbb1b211c080c41.zip
Unify parsing logic for command-line integer options
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 Reviewed-by: David Rowley, Álvaro Herrera Discussion: https://postgr.es/m/CALj2ACXqdG9WhqVoJ9zYf-iZt7sgK7Szv5USs=he6NnWQ2ofTA@mail.gmail.com
Diffstat (limited to 'src/bin/scripts/createuser.c')
-rw-r--r--src/bin/scripts/createuser.c13
1 files changed, 5 insertions, 8 deletions
diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c
index ef7e0e549fb..35b5ccaa5b3 100644
--- a/src/bin/scripts/createuser.c
+++ b/src/bin/scripts/createuser.c
@@ -11,6 +11,9 @@
*/
#include "postgres_fe.h"
+
+#include <limits.h>
+
#include "common.h"
#include "common/logging.h"
#include "common/string.h"
@@ -89,8 +92,6 @@ main(int argc, char *argv[])
while ((c = getopt_long(argc, argv, "h:p:U:g:wWedDsSrRiIlLc:PE",
long_options, &optindex)) != -1)
{
- char *endptr;
-
switch (c)
{
case 'h':
@@ -145,13 +146,9 @@ main(int argc, char *argv[])
login = TRI_NO;
break;
case 'c':
- conn_limit = strtol(optarg, &endptr, 10);
- if (*endptr != '\0' || conn_limit < -1) /* minimum valid value */
- {
- pg_log_error("invalid value for --connection-limit: %s",
- optarg);
+ if (!option_parse_int(optarg, "-c/--connection-limit",
+ -1, INT_MAX, &conn_limit))
exit(1);
- }
break;
case 'P':
pwprompt = true;