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/int8.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/int8.c')
-rw-r--r-- | src/backend/utils/adt/int8.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 8a346cd8b83..cf164561058 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.42 2002/09/18 21:35:22 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.43 2003/03/11 21:01:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -504,6 +504,9 @@ int8div(PG_FUNCTION_ARGS) int64 val1 = PG_GETARG_INT64(0); int64 val2 = PG_GETARG_INT64(1); + if (val2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT64(val1 / val2); } @@ -528,6 +531,9 @@ int8mod(PG_FUNCTION_ARGS) int64 val2 = PG_GETARG_INT64(1); int64 result; + if (val2 == 0) + elog(ERROR, "division by zero"); + result = val1 / val2; result *= val2; result = val1 - result; @@ -621,6 +627,9 @@ int84div(PG_FUNCTION_ARGS) int64 val1 = PG_GETARG_INT64(0); int32 val2 = PG_GETARG_INT32(1); + if (val2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT64(val1 / val2); } @@ -657,6 +666,9 @@ int48div(PG_FUNCTION_ARGS) int32 val1 = PG_GETARG_INT32(0); int64 val2 = PG_GETARG_INT64(1); + if (val2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_INT64(val1 / val2); } |