diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2003-03-11 21:01:33 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2003-03-11 21:01:33 +0000 |
commit | 31e69ccb210cf49712f77facbf90661d8bc2eed5 (patch) | |
tree | ec6e0c1a656051db532cdef53a84309d6180c4a1 /src/backend/utils/adt/int.c | |
parent | 6261c75014c9948837d9d025493ef18b8f833f70 (diff) | |
download | postgresql-31e69ccb210cf49712f77facbf90661d8bc2eed5.tar.gz postgresql-31e69ccb210cf49712f77facbf90661d8bc2eed5.zip |
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.
Diffstat (limited to 'src/backend/utils/adt/int.c')
-rw-r--r-- | src/backend/utils/adt/int.c | 26 |
1 files changed, 25 insertions, 1 deletions
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); } |