Re: Incorrect usage of strtol, atoi for non-numeric junk inputs
Michael Paquier <michael@paquier.xyz>
From: Michael Paquier <michael@paquier.xyz>
To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Cc: bharath.rupireddyforpostgres@gmail.com, alvherre@alvh.no-ip.org, pgsql-hackers@lists.postgresql.org
Date: 2021-07-09T01:29:07Z
Lists: pgsql-hackers
On Thu, Jul 08, 2021 at 05:30:23PM +0900, Kyotaro Horiguchi wrote:
> Looked through the three threads.
Thanks!
> [1] is trying to expose pg_strtoint16/32 to frontend, but I don't see
> much point in doing that in conjunction with [2] or this thread. Since
> the integral parameter values of pg-commands are in int, which the
> exising function strtoint() is sufficient to read. So even [2] itself
> doesn't need to utilize [1].
It sounds sensible from here to just use strtoint(), some strtol(),
son strtod() and call it a day as these are already available.
> - wait_seconds = atoi(optarg);
> + errno = 0;
> + wait_seconds = strtoint(optarg, &endptr, 10);
> + if (*endptr || errno == ERANGE || wait_seconds < 0)
> + {
> + pg_log_error("invalid timeout \"%s\"", optarg);
> + exit(1);
> + }
> [ ... ]
> - killproc = atol(argv[++optind]);
> + errno = 0;
> + killproc = strtol(argv[++optind], &endptr, 10);
> + if (*endptr || errno == ERANGE || killproc < 0)
> + {
> + pg_log_error("invalid process ID \"%s\"", argv[optind]);
> + exit(1);
> + }
Er, wait. We've actually allowed negative values for pg_ctl
--timeout or the subcommand kill!?
> case 'j':
> - user_opts.jobs = atoi(optarg);
> + errno = 0;
> + user_opts.jobs = strtoint(optarg, &endptr, 10);
> + /**/
> + if (*endptr || errno == ERANGE)
> + pg_fatal("invalid number of jobs %s\n", optarg);
> +
> break;
This one in pg_upgrade is incomplete. Perhaps the missing comment
should tell that negative job values are checked later on?
--
Michael
Commits
-
Simplify matching pattern check in TAP tests of pg_receivewal
- 24ba1a87e405 15.0 landed
-
Skip trailing whitespaces when parsing integer options
- f7a9a3d4b24a 15.0 landed
-
Unify parsing logic for command-line integer options
- 6f164e6d1761 15.0 landed