Re: Incorrect usage of strtol, atoi for non-numeric junk inputs

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>
Cc: bharath(dot)rupireddyforpostgres(at)gmail(dot)com, alvherre(at)alvh(dot)no-ip(dot)org, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Incorrect usage of strtol, atoi for non-numeric junk inputs
Date: 2021-07-09 01:29:07
Message-ID: YOemYz96spvHoTtv@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2021-07-09 01:44:13 Re: ERROR: "ft1" is of the wrong type.
Previous Message Quan Zongliang 2021-07-09 01:26:37 Re: bugfix: when the blocksize is 32k, the function page_header of pageinspect returns negative numbers.