1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-11 20:28:21 +03:00

postgres_fdw: Tighten up allowed values for batch_size, fetch_size options.

Previously the values such as '100$%$#$#', '9,223,372,' were accepted and
treated as valid integers for postgres_fdw options batch_size and fetch_size.
Whereas this is not the case with fdw_startup_cost and fdw_tuple_cost options
for which an error is thrown. This was because endptr was not used
while converting strings to integers using strtol.

This commit changes the logic so that it uses parse_int function
instead of strtol as it serves the purpose by returning false in case
if it is unable to convert the string to integer. Note that
this function also rounds off the values such as '100.456' to 100 and
'100.567' or '100.678' to 101.

While on this, use parse_real for fdw_startup_cost and fdw_tuple_cost options.

Since parse_int and parse_real are being used for reloptions and GUCs,
it is more appropriate to use in postgres_fdw rather than using strtol
and strtod directly.

Back-patch to v14.

Author: Bharath Rupireddy
Reviewed-by: Ashutosh Bapat, Tom Lane, Kyotaro Horiguchi, Fujii Masao
Discussion: https://postgr.es/m/CALj2ACVMO6wY5Pc4oe1OCgUOAtdjHuFsBDw8R5uoYR86eWFQDA@mail.gmail.com
This commit is contained in:
Fujii Masao
2021-07-07 11:13:40 +09:00
parent 86d4914210
commit 4173477b38
5 changed files with 83 additions and 36 deletions

View File

@ -20,6 +20,7 @@
#include "commands/extension.h"
#include "postgres_fdw.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/varlena.h"
/*
@ -119,14 +120,23 @@ postgres_fdw_validator(PG_FUNCTION_ARGS)
strcmp(def->defname, "fdw_tuple_cost") == 0)
{
/* these must have a non-negative numeric value */
double val;
char *endp;
char *value;
double real_val;
bool is_parsed;
val = strtod(defGetString(def), &endp);
if (*endp || val < 0)
value = defGetString(def);
is_parsed = parse_real(value, &real_val, 0, NULL);
if (!is_parsed)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("%s requires a non-negative numeric value",
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid value for floating point option \"%s\": %s",
def->defname, value)));
if (real_val < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\"%s\" requires a non-negative floating point value",
def->defname)));
}
else if (strcmp(def->defname, "extensions") == 0)
@ -134,26 +144,26 @@ postgres_fdw_validator(PG_FUNCTION_ARGS)
/* check list syntax, warn about uninstalled extensions */
(void) ExtractExtensionList(defGetString(def), true);
}
else if (strcmp(def->defname, "fetch_size") == 0)
else if (strcmp(def->defname, "fetch_size") == 0 ||
strcmp(def->defname, "batch_size") == 0)
{
int fetch_size;
char *value;
int int_val;
bool is_parsed;
fetch_size = strtol(defGetString(def), NULL, 10);
if (fetch_size <= 0)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("%s requires a non-negative integer value",
def->defname)));
}
else if (strcmp(def->defname, "batch_size") == 0)
{
int batch_size;
value = defGetString(def);
is_parsed = parse_int(value, &int_val, 0, NULL);
batch_size = strtol(defGetString(def), NULL, 10);
if (batch_size <= 0)
if (!is_parsed)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("%s requires a non-negative integer value",
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid value for integer option \"%s\": %s",
def->defname, value)));
if (int_val <= 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\"%s\" requires a non-negative integer value",
def->defname)));
}
else if (strcmp(def->defname, "password_required") == 0)