diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 2a50d2af1e..7009564b41 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -194,7 +194,7 @@ float4in(PG_FUNCTION_ARGS) * strtod() on different platforms. */ if (*num == '\0') - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type real: \"%s\"", orig_num))); @@ -262,13 +262,13 @@ float4in(PG_FUNCTION_ARGS) * to see if the result is zero or huge. */ if (val == 0.0 || val >= HUGE_VAL || val <= -HUGE_VAL) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("\"%s\" is out of range for type real", orig_num))); } else - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type real: \"%s\"", orig_num))); @@ -292,7 +292,7 @@ float4in(PG_FUNCTION_ARGS) /* if there is any junk left at the end of the string, bail out */ if (*endptr != '\0') - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type real: \"%s\"", orig_num))); @@ -394,7 +394,7 @@ float8in(PG_FUNCTION_ARGS) * strtod() on different platforms. */ if (*num == '\0') - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type double precision: \"%s\"", orig_num))); @@ -462,13 +462,13 @@ float8in(PG_FUNCTION_ARGS) * to see if the result is zero or huge. */ if (val == 0.0 || val >= HUGE_VAL || val <= -HUGE_VAL) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("\"%s\" is out of range for type double precision", orig_num))); } else - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type double precision: \"%s\"", orig_num))); @@ -492,7 +492,7 @@ float8in(PG_FUNCTION_ARGS) /* if there is any junk left at the end of the string, bail out */ if (*endptr != '\0') - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type double precision: \"%s\"", orig_num))); @@ -788,7 +788,7 @@ float4div(PG_FUNCTION_ARGS) float4 result; if (arg2 == 0.0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); @@ -852,7 +852,7 @@ float8div(PG_FUNCTION_ARGS) float8 result; if (arg2 == 0.0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); @@ -1167,7 +1167,7 @@ dtoi4(PG_FUNCTION_ARGS) /* Range check */ if (isnan(num) || !FLOAT8_FITS_IN_INT32(num)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1192,7 +1192,7 @@ dtoi2(PG_FUNCTION_ARGS) /* Range check */ if (isnan(num) || !FLOAT8_FITS_IN_INT16(num)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -1241,7 +1241,7 @@ ftoi4(PG_FUNCTION_ARGS) /* Range check */ if (isnan(num) || !FLOAT4_FITS_IN_INT32(num)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1266,7 +1266,7 @@ ftoi2(PG_FUNCTION_ARGS) /* Range check */ if (isnan(num) || !FLOAT4_FITS_IN_INT16(num)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -2684,7 +2684,7 @@ float48div(PG_FUNCTION_ARGS) float8 result; if (arg2 == 0.0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); @@ -2747,7 +2747,7 @@ float84div(PG_FUNCTION_ARGS) float8 result; if (arg2 == 0.0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index b8f56e5c2e..b9c09b8ace 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -62,7 +62,7 @@ int2in(PG_FUNCTION_ARGS) { char *num = PG_GETARG_CSTRING(0); - PG_RETURN_INT16(pg_atoi(num, sizeof(int16), '\0')); + PG_RETURN_INT16(pg_atoi_safe(num, sizeof(int16), '\0', fcinfo->context)); } /* @@ -157,7 +157,7 @@ int2vectorin(PG_FUNCTION_ARGS) while (*intString && isspace((unsigned char) *intString)) intString++; if (*intString) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("int2vector has too many elements"))); @@ -232,13 +232,13 @@ int2vectorrecv(PG_FUNCTION_ARGS) ARR_HASNULL(result) || ARR_ELEMTYPE(result) != INT2OID || ARR_LBOUND(result)[0] != 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), errmsg("invalid int2vector data"))); /* check length for consistency with int2vectorin() */ if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("oidvector has too many elements"))); @@ -282,7 +282,7 @@ int4in(PG_FUNCTION_ARGS) { char *num = PG_GETARG_CSTRING(0); - PG_RETURN_INT32(pg_atoi(num, sizeof(int32), '\0')); + PG_RETURN_INT32(pg_atoi_safe(num, sizeof(int32), '\0', fcinfo->context)); } /* @@ -344,7 +344,7 @@ i4toi2(PG_FUNCTION_ARGS) int32 arg1 = PG_GETARG_INT32(0); if (arg1 < SHRT_MIN || arg1 > SHRT_MAX) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -618,7 +618,7 @@ int4um(PG_FUNCTION_ARGS) result = -arg; /* overflow check (needed for INT_MIN) */ if (arg != 0 && SAMESIGN(result, arg)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -647,7 +647,7 @@ int4pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -668,7 +668,7 @@ int4mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -699,7 +699,7 @@ int4mul(PG_FUNCTION_ARGS) arg2 != 0 && ((arg2 == -1 && arg1 < 0 && result < 0) || result / arg2 != arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -714,7 +714,7 @@ int4div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -732,7 +732,7 @@ int4div(PG_FUNCTION_ARGS) result = -arg1; /* overflow check (needed for INT_MIN) */ if (arg1 != 0 && SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -754,7 +754,7 @@ int4inc(PG_FUNCTION_ARGS) result = arg + 1; /* Overflow check */ if (arg > 0 && result < 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -770,7 +770,7 @@ int2um(PG_FUNCTION_ARGS) result = -arg; /* overflow check (needed for SHRT_MIN) */ if (arg != 0 && SAMESIGN(result, arg)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); PG_RETURN_INT16(result); @@ -799,7 +799,7 @@ int2pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); PG_RETURN_INT16(result); @@ -820,7 +820,7 @@ int2mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); PG_RETURN_INT16(result); @@ -840,7 +840,7 @@ int2mul(PG_FUNCTION_ARGS) result32 = (int32) arg1 *(int32) arg2; if (result32 < SHRT_MIN || result32 > SHRT_MAX) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -856,7 +856,7 @@ int2div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -874,7 +874,7 @@ int2div(PG_FUNCTION_ARGS) result = -arg1; /* overflow check (needed for SHRT_MIN) */ if (arg1 != 0 && SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); PG_RETURN_INT16(result); @@ -902,7 +902,7 @@ int24pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -923,7 +923,7 @@ int24mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -950,7 +950,7 @@ int24mul(PG_FUNCTION_ARGS) */ if (!(arg2 >= (int32) SHRT_MIN && arg2 <= (int32) SHRT_MAX) && result / arg2 != arg1) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -964,7 +964,7 @@ int24div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -990,7 +990,7 @@ int42pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -1011,7 +1011,7 @@ int42mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -1038,7 +1038,7 @@ int42mul(PG_FUNCTION_ARGS) */ if (!(arg1 >= (int32) SHRT_MIN && arg1 <= (int32) SHRT_MAX) && result / arg1 != arg2) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -1053,7 +1053,7 @@ int42div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -1071,7 +1071,7 @@ int42div(PG_FUNCTION_ARGS) result = -arg1; /* overflow check (needed for INT_MIN) */ if (arg1 != 0 && SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -1092,7 +1092,7 @@ int4mod(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -1120,7 +1120,7 @@ int2mod(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -1154,7 +1154,7 @@ int4abs(PG_FUNCTION_ARGS) result = (arg1 < 0) ? -arg1 : arg1; /* overflow check (needed for INT_MIN) */ if (result < 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); PG_RETURN_INT32(result); @@ -1169,7 +1169,7 @@ int2abs(PG_FUNCTION_ARGS) result = (arg1 < 0) ? -arg1 : arg1; /* overflow check (needed for SHRT_MIN) */ if (result < 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); PG_RETURN_INT16(result); diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 2788853c1b..e777675832 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -53,6 +53,11 @@ typedef struct */ bool scanint8(const char *str, bool errorOK, int64 *result) +{ + return scanint8_safe(str, errorOK, result, NULL); +} +bool +scanint8_safe(const char *str, bool errorOK, int64 *result, Node* escontext) { const char *ptr = str; int64 tmp = 0; @@ -93,7 +98,7 @@ scanint8(const char *str, bool errorOK, int64 *result) if (errorOK) return false; else - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for integer: \"%s\"", str))); @@ -109,7 +114,7 @@ scanint8(const char *str, bool errorOK, int64 *result) if (errorOK) return false; else - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("value \"%s\" is out of range for type bigint", str))); @@ -128,7 +133,7 @@ scanint8(const char *str, bool errorOK, int64 *result) if (errorOK) return false; else - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for integer: \"%s\"", str))); @@ -495,7 +500,7 @@ int8um(PG_FUNCTION_ARGS) result = -arg; /* overflow check (needed for INT64_MIN) */ if (arg != 0 && SAMESIGN(result, arg)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -524,7 +529,7 @@ int8pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -545,7 +550,7 @@ int8mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -576,7 +581,7 @@ int8mul(PG_FUNCTION_ARGS) if (arg2 != 0 && ((arg2 == -1 && arg1 < 0 && result < 0) || result / arg2 != arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); } @@ -592,7 +597,7 @@ int8div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -610,7 +615,7 @@ int8div(PG_FUNCTION_ARGS) result = -arg1; /* overflow check (needed for INT64_MIN) */ if (arg1 != 0 && SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -635,7 +640,7 @@ int8abs(PG_FUNCTION_ARGS) result = (arg1 < 0) ? -arg1 : arg1; /* overflow check (needed for INT64_MIN) */ if (result < 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -652,7 +657,7 @@ int8mod(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -692,7 +697,7 @@ int8inc(PG_FUNCTION_ARGS) result = *arg + 1; /* Overflow check */ if (result < 0 && *arg > 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -709,7 +714,7 @@ int8inc(PG_FUNCTION_ARGS) result = arg + 1; /* Overflow check */ if (result < 0 && arg > 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -736,7 +741,7 @@ int8dec(PG_FUNCTION_ARGS) result = *arg - 1; /* Overflow check */ if (result > 0 && *arg < 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -753,7 +758,7 @@ int8dec(PG_FUNCTION_ARGS) result = arg - 1; /* Overflow check */ if (result > 0 && arg < 0) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -829,7 +834,7 @@ int84pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -850,7 +855,7 @@ int84mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -877,7 +882,7 @@ int84mul(PG_FUNCTION_ARGS) */ if (arg1 != (int64) ((int32) arg1) && result / arg1 != arg2) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -892,7 +897,7 @@ int84div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -910,7 +915,7 @@ int84div(PG_FUNCTION_ARGS) result = -arg1; /* overflow check (needed for INT64_MIN) */ if (arg1 != 0 && SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -938,7 +943,7 @@ int48pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -959,7 +964,7 @@ int48mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -986,7 +991,7 @@ int48mul(PG_FUNCTION_ARGS) */ if (arg2 != (int64) ((int32) arg2) && result / arg2 != arg1) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1000,7 +1005,7 @@ int48div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -1026,7 +1031,7 @@ int82pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1047,7 +1052,7 @@ int82mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1074,7 +1079,7 @@ int82mul(PG_FUNCTION_ARGS) */ if (arg1 != (int64) ((int32) arg1) && result / arg1 != arg2) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1089,7 +1094,7 @@ int82div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -1107,7 +1112,7 @@ int82div(PG_FUNCTION_ARGS) result = -arg1; /* overflow check (needed for INT64_MIN) */ if (arg1 != 0 && SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1135,7 +1140,7 @@ int28pl(PG_FUNCTION_ARGS) * better be that sign too. */ if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1156,7 +1161,7 @@ int28mi(PG_FUNCTION_ARGS) * result should be of the same sign as the first input. */ if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1183,7 +1188,7 @@ int28mul(PG_FUNCTION_ARGS) */ if (arg2 != (int64) ((int32) arg2) && result / arg2 != arg1) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); PG_RETURN_INT64(result); @@ -1197,7 +1202,7 @@ int28div(PG_FUNCTION_ARGS) if (arg2 == 0) { - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* ensure compiler realizes we mustn't reach the division (gcc bug) */ @@ -1293,7 +1298,7 @@ int84(PG_FUNCTION_ARGS) /* Test for overflow by reverse-conversion. */ if ((int64) result != arg) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1318,7 +1323,7 @@ int82(PG_FUNCTION_ARGS) /* Test for overflow by reverse-conversion. */ if ((int64) result != arg) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -1353,7 +1358,7 @@ dtoi8(PG_FUNCTION_ARGS) /* Range check */ if (isnan(num) || !FLOAT8_FITS_IN_INT64(num)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1388,7 +1393,7 @@ ftoi8(PG_FUNCTION_ARGS) /* Range check */ if (isnan(num) || !FLOAT4_FITS_IN_INT64(num)) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1405,7 +1410,7 @@ i8tooid(PG_FUNCTION_ARGS) /* Test for overflow by reverse-conversion. */ if ((int64) result != arg) - ereport(ERROR, + ereturn(fcinfo->context, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("OID out of range"))); diff --git a/src/backend/utils/adt/numutils.c b/src/backend/utils/adt/numutils.c index ca5a8a576c..9fb578a85a 100644 --- a/src/backend/utils/adt/numutils.c +++ b/src/backend/utils/adt/numutils.c @@ -31,10 +31,16 @@ * integer (plus whitespace). If 0, the string must end after the integer. * * Unlike plain atoi(), this will throw ereport() upon bad input format or - * overflow. + * overflow; while pg_atoi_safe() instead returns such complaints in *escontext, + * if it's an ErrorSaveContext. */ int32 pg_atoi(char *s, int size, int c) +{ + return pg_atoi_safe(s, size, c, NULL); +} +int32 +pg_atoi_safe(char *s, int size, int c, Node* escontext) { long l; char *badp; @@ -46,7 +52,7 @@ pg_atoi(char *s, int size, int c) if (s == NULL) elog(ERROR, "NULL pointer"); if (*s == 0) - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for integer: \"%s\"", s))); @@ -56,7 +62,7 @@ pg_atoi(char *s, int size, int c) /* We made no progress parsing the string, so bail out */ if (s == badp) - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for integer: \"%s\"", s))); @@ -70,19 +76,19 @@ pg_atoi(char *s, int size, int c) || l < INT_MIN || l > INT_MAX #endif ) - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("value \"%s\" is out of range for type integer", s))); break; case sizeof(int16): if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX) - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("value \"%s\" is out of range for type smallint", s))); break; case sizeof(int8): if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX) - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("value \"%s\" is out of range for 8-bit integer", s))); break; @@ -98,7 +104,7 @@ pg_atoi(char *s, int size, int c) badp++; if (*badp && *badp != c) - ereport(ERROR, + ereturn(escontext, 0, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for integer: \"%s\"", s))); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index 0bca9f7a4b..2f6861a58d 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -294,6 +294,7 @@ extern Datum current_schemas(PG_FUNCTION_ARGS); /* numutils.c */ extern int32 pg_atoi(char *s, int size, int c); +extern int32 pg_atoi_safe(char *s, int size, int c, Node* escontext); extern void pg_itoa(int16 i, char *a); extern void pg_ltoa(int32 l, char *a); extern void pg_lltoa(int64 ll, char *a); diff --git a/src/include/utils/int8.h b/src/include/utils/int8.h index 0e4b949946..8da5f78872 100644 --- a/src/include/utils/int8.h +++ b/src/include/utils/int8.h @@ -24,6 +24,7 @@ extern bool scanint8(const char *str, bool errorOK, int64 *result); +extern bool scanint8_safe(const char *str, bool errorOK, int64 *result, Node* escontext); extern Datum int8in(PG_FUNCTION_ARGS); extern Datum int8out(PG_FUNCTION_ARGS);