mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
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
This commit is contained in:
@ -12,6 +12,8 @@
|
||||
|
||||
#include "postgres_fe.h"
|
||||
|
||||
#include "common/logging.h"
|
||||
#include "common/string.h"
|
||||
#include "fe_utils/option_utils.h"
|
||||
|
||||
/*
|
||||
@ -36,3 +38,40 @@ handle_help_version_opts(int argc, char *argv[],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* option_parse_int
|
||||
*
|
||||
* Parse integer value for an option. If the parsing is successful, returns
|
||||
* true and stores the result in *result if that's given; if parsing fails,
|
||||
* returns false.
|
||||
*/
|
||||
bool
|
||||
option_parse_int(const char *optarg, const char *optname,
|
||||
int min_range, int max_range,
|
||||
int *result)
|
||||
{
|
||||
char *endptr;
|
||||
int val;
|
||||
|
||||
errno = 0;
|
||||
val = strtoint(optarg, &endptr, 10);
|
||||
|
||||
if (*endptr)
|
||||
{
|
||||
pg_log_error("invalid value \"%s\" for option %s",
|
||||
optarg, optname);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (errno == ERANGE || val < min_range || val > max_range)
|
||||
{
|
||||
pg_log_error("%s must be in range %d..%d",
|
||||
optname, min_range, max_range);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (result)
|
||||
*result = val;
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user