From 31e69ccb210cf49712f77facbf90661d8bc2eed5 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 11 Mar 2003 21:01:33 +0000 Subject: Add explicit tests for division by zero to all user-accessible integer division and modulo functions, to avoid problems on OS X (which fails to trap 0 divide at all) and Windows (which traps it in some bizarre nonstandard fashion). Standardize on 'division by zero' as the one true spelling of this error message. Add regression tests as suggested by Neil Conway. --- src/backend/utils/adt/int.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/backend/utils/adt/int.c') diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index 859e78b58cb..73687d55ec9 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.52 2002/08/22 00:01:43 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.53 2003/03/11 21:01:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -551,6 +551,9 @@ int4div(PG_FUNCTION_ARGS) int32 arg1 = PG_GETARG_INT32(0); int32 arg2 = PG_GETARG_INT32(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT32(arg1 / arg2); } @@ -611,6 +614,9 @@ int2div(PG_FUNCTION_ARGS) int16 arg1 = PG_GETARG_INT16(0); int16 arg2 = PG_GETARG_INT16(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT16(arg1 / arg2); } @@ -647,6 +653,9 @@ int24div(PG_FUNCTION_ARGS) int16 arg1 = PG_GETARG_INT16(0); int32 arg2 = PG_GETARG_INT32(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT32(arg1 / arg2); } @@ -683,6 +692,9 @@ int42div(PG_FUNCTION_ARGS) int32 arg1 = PG_GETARG_INT32(0); int16 arg2 = PG_GETARG_INT16(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT32(arg1 / arg2); } @@ -692,6 +704,9 @@ int4mod(PG_FUNCTION_ARGS) int32 arg1 = PG_GETARG_INT32(0); int32 arg2 = PG_GETARG_INT32(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT32(arg1 % arg2); } @@ -701,6 +716,9 @@ int2mod(PG_FUNCTION_ARGS) int16 arg1 = PG_GETARG_INT16(0); int16 arg2 = PG_GETARG_INT16(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT16(arg1 % arg2); } @@ -710,6 +728,9 @@ int24mod(PG_FUNCTION_ARGS) int16 arg1 = PG_GETARG_INT16(0); int32 arg2 = PG_GETARG_INT32(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT32(arg1 % arg2); } @@ -719,6 +740,9 @@ int42mod(PG_FUNCTION_ARGS) int32 arg1 = PG_GETARG_INT32(0); int16 arg2 = PG_GETARG_INT16(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT32(arg1 % arg2); } -- cgit v1.2.3