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 | |
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')
-rw-r--r-- | src/backend/utils/adt/cash.c | 10 | ||||
-rw-r--r-- | src/backend/utils/adt/char.c | 5 | ||||
-rw-r--r-- | src/backend/utils/adt/float.c | 10 | ||||
-rw-r--r-- | src/backend/utils/adt/geo_ops.c | 4 | ||||
-rw-r--r-- | src/backend/utils/adt/int.c | 26 | ||||
-rw-r--r-- | src/backend/utils/adt/int8.c | 14 | ||||
-rw-r--r-- | src/backend/utils/adt/numeric.c | 4 | ||||
-rw-r--r-- | src/backend/utils/adt/timestamp.c | 4 |
8 files changed, 58 insertions, 19 deletions
diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c index c33bca654e8..f210f4258f0 100644 --- a/src/backend/utils/adt/cash.c +++ b/src/backend/utils/adt/cash.c @@ -9,7 +9,7 @@ * workings can be found in the book "Software Solutions in C" by * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7. * - * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.56 2002/09/04 20:31:27 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.57 2003/03/11 21:01:33 tgl Exp $ */ #include "postgres.h" @@ -442,7 +442,7 @@ cash_div_flt8(PG_FUNCTION_ARGS) Cash result; if (f == 0.0) - elog(ERROR, "cash_div: divide by 0.0 error"); + elog(ERROR, "division by zero"); result = rint(c / f); PG_RETURN_CASH(result); @@ -492,7 +492,7 @@ cash_div_flt4(PG_FUNCTION_ARGS) Cash result; if (f == 0.0) - elog(ERROR, "cash_div: divide by 0.0 error"); + elog(ERROR, "division by zero"); result = rint(c / f); PG_RETURN_CASH(result); @@ -543,7 +543,7 @@ cash_div_int4(PG_FUNCTION_ARGS) Cash result; if (i == 0) - elog(ERROR, "cash_div_int4: divide by 0 error"); + elog(ERROR, "division by zero"); result = rint(c / i); @@ -593,7 +593,7 @@ cash_div_int2(PG_FUNCTION_ARGS) Cash result; if (s == 0) - elog(ERROR, "cash_div: divide by 0 error"); + elog(ERROR, "division by zero"); result = rint(c / s); PG_RETURN_CASH(result); diff --git a/src/backend/utils/adt/char.c b/src/backend/utils/adt/char.c index c31995fdf06..0e2d400aa0d 100644 --- a/src/backend/utils/adt/char.c +++ b/src/backend/utils/adt/char.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.33 2002/06/20 20:29:36 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.34 2003/03/11 21:01:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -150,6 +150,9 @@ chardiv(PG_FUNCTION_ARGS) char arg1 = PG_GETARG_CHAR(0); char arg2 = PG_GETARG_CHAR(1); + if (arg2 == 0) + elog(ERROR, "division by zero"); + PG_RETURN_CHAR((int8) arg1 / (int8) arg2); } diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index c0acc554b8e..4220b4775a9 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.83 2002/11/08 17:37:52 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.84 2003/03/11 21:01:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -505,7 +505,7 @@ float4div(PG_FUNCTION_ARGS) double result; if (arg2 == 0.0) - elog(ERROR, "float4div: divide by zero error"); + elog(ERROR, "division by zero"); /* Do division in float8, then check for overflow */ result = (float8) arg1 / (float8) arg2; @@ -567,7 +567,7 @@ float8div(PG_FUNCTION_ARGS) float8 result; if (arg2 == 0.0) - elog(ERROR, "float8div: divide by zero error"); + elog(ERROR, "division by zero"); result = arg1 / arg2; @@ -1753,7 +1753,7 @@ float48div(PG_FUNCTION_ARGS) float8 result; if (arg2 == 0.0) - elog(ERROR, "float48div: divide by zero"); + elog(ERROR, "division by zero"); result = arg1 / arg2; CheckFloat8Val(result); @@ -1813,7 +1813,7 @@ float84div(PG_FUNCTION_ARGS) float8 result; if (arg2 == 0.0) - elog(ERROR, "float84div: divide by zero"); + elog(ERROR, "division by zero"); result = arg1 / arg2; diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_ops.c index 9fe40d2ccbe..d8d1f7c3afe 100644 --- a/src/backend/utils/adt/geo_ops.c +++ b/src/backend/utils/adt/geo_ops.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.74 2003/01/21 19:44:26 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.75 2003/03/11 21:01:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -3504,7 +3504,7 @@ point_div(PG_FUNCTION_ARGS) div = (p2->x * p2->x) + (p2->y * p2->y); if (div == 0.0) - elog(ERROR, "point_div: divide by 0.0 error"); + elog(ERROR, "division by zero"); result->x = ((p1->x * p2->x) + (p1->y * p2->y)) / div; result->y = ((p2->x * p1->y) - (p2->y * p1->x)) / div; 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); } 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); } diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index f1967019d53..1a21500a8b0 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -5,7 +5,7 @@ * * 1998 Jan Wieck * - * $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.56 2002/10/19 02:08:17 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.57 2003/03/11 21:01:33 tgl Exp $ * * ---------- */ @@ -3266,7 +3266,7 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result) */ ndigits_tmp = var2->ndigits + 1; if (ndigits_tmp == 1) - elog(ERROR, "division by zero on numeric"); + elog(ERROR, "division by zero"); /* * Determine the result sign, weight and number of digits to calculate diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index d3390fd3274..2764fee906a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.79 2003/02/27 21:36:58 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.80 2003/03/11 21:01:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1869,7 +1869,7 @@ interval_div(PG_FUNCTION_ARGS) result = (Interval *) palloc(sizeof(Interval)); if (factor == 0.0) - elog(ERROR, "interval_div: divide by 0.0 error"); + elog(ERROR, "division by zero"); #ifdef HAVE_INT64_TIMESTAMP result->month = (span->month / factor); |