Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 31e69cc

Browse files
committed
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.
1 parent 6261c75 commit 31e69cc

File tree

16 files changed

+130
-42
lines changed

16 files changed

+130
-42
lines changed

src/backend/utils/adt/cash.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* workings can be found in the book "Software Solutions in C" by
1010
* Dale Schumacher, Academic Press, ISBN: 0-12-632360-7.
1111
*
12-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.56 2002/09/04 20:31:27 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.57 2003/03/11 21:01:33 tgl Exp $
1313
*/
1414

1515
#include "postgres.h"
@@ -442,7 +442,7 @@ cash_div_flt8(PG_FUNCTION_ARGS)
442442
Cash result;
443443

444444
if (f == 0.0)
445-
elog(ERROR, "cash_div: divide by 0.0 error");
445+
elog(ERROR, "division by zero");
446446

447447
result = rint(c / f);
448448
PG_RETURN_CASH(result);
@@ -492,7 +492,7 @@ cash_div_flt4(PG_FUNCTION_ARGS)
492492
Cash result;
493493

494494
if (f == 0.0)
495-
elog(ERROR, "cash_div: divide by 0.0 error");
495+
elog(ERROR, "division by zero");
496496

497497
result = rint(c / f);
498498
PG_RETURN_CASH(result);
@@ -543,7 +543,7 @@ cash_div_int4(PG_FUNCTION_ARGS)
543543
Cash result;
544544

545545
if (i == 0)
546-
elog(ERROR, "cash_div_int4: divide by 0 error");
546+
elog(ERROR, "division by zero");
547547

548548
result = rint(c / i);
549549

@@ -593,7 +593,7 @@ cash_div_int2(PG_FUNCTION_ARGS)
593593
Cash result;
594594

595595
if (s == 0)
596-
elog(ERROR, "cash_div: divide by 0 error");
596+
elog(ERROR, "division by zero");
597597

598598
result = rint(c / s);
599599
PG_RETURN_CASH(result);

src/backend/utils/adt/char.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.33 2002/06/20 20:29:36 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.34 2003/03/11 21:01:33 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -150,6 +150,9 @@ chardiv(PG_FUNCTION_ARGS)
150150
char arg1 = PG_GETARG_CHAR(0);
151151
char arg2 = PG_GETARG_CHAR(1);
152152

153+
if (arg2 == 0)
154+
elog(ERROR, "division by zero");
155+
153156
PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
154157
}
155158

src/backend/utils/adt/float.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.83 2002/11/08 17:37:52 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.84 2003/03/11 21:01:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -505,7 +505,7 @@ float4div(PG_FUNCTION_ARGS)
505505
double result;
506506

507507
if (arg2 == 0.0)
508-
elog(ERROR, "float4div: divide by zero error");
508+
elog(ERROR, "division by zero");
509509

510510
/* Do division in float8, then check for overflow */
511511
result = (float8) arg1 / (float8) arg2;
@@ -567,7 +567,7 @@ float8div(PG_FUNCTION_ARGS)
567567
float8 result;
568568

569569
if (arg2 == 0.0)
570-
elog(ERROR, "float8div: divide by zero error");
570+
elog(ERROR, "division by zero");
571571

572572
result = arg1 / arg2;
573573

@@ -1753,7 +1753,7 @@ float48div(PG_FUNCTION_ARGS)
17531753
float8 result;
17541754

17551755
if (arg2 == 0.0)
1756-
elog(ERROR, "float48div: divide by zero");
1756+
elog(ERROR, "division by zero");
17571757

17581758
result = arg1 / arg2;
17591759
CheckFloat8Val(result);
@@ -1813,7 +1813,7 @@ float84div(PG_FUNCTION_ARGS)
18131813
float8 result;
18141814

18151815
if (arg2 == 0.0)
1816-
elog(ERROR, "float84div: divide by zero");
1816+
elog(ERROR, "division by zero");
18171817

18181818
result = arg1 / arg2;
18191819

src/backend/utils/adt/geo_ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.74 2003/01/21 19:44:26 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.75 2003/03/11 21:01:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3504,7 +3504,7 @@ point_div(PG_FUNCTION_ARGS)
35043504
div = (p2->x * p2->x) + (p2->y * p2->y);
35053505

35063506
if (div == 0.0)
3507-
elog(ERROR, "point_div: divide by 0.0 error");
3507+
elog(ERROR, "division by zero");
35083508

35093509
result->x = ((p1->x * p2->x) + (p1->y * p2->y)) / div;
35103510
result->y = ((p2->x * p1->y) - (p2->y * p1->x)) / div;

src/backend/utils/adt/int.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.52 2002/08/22 00:01:43 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.53 2003/03/11 21:01:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -551,6 +551,9 @@ int4div(PG_FUNCTION_ARGS)
551551
int32 arg1 = PG_GETARG_INT32(0);
552552
int32 arg2 = PG_GETARG_INT32(1);
553553

554+
if (arg2 == 0)
555+
elog(ERROR, "division by zero");
556+
554557
PG_RETURN_INT32(arg1 / arg2);
555558
}
556559

@@ -611,6 +614,9 @@ int2div(PG_FUNCTION_ARGS)
611614
int16 arg1 = PG_GETARG_INT16(0);
612615
int16 arg2 = PG_GETARG_INT16(1);
613616

617+
if (arg2 == 0)
618+
elog(ERROR, "division by zero");
619+
614620
PG_RETURN_INT16(arg1 / arg2);
615621
}
616622

@@ -647,6 +653,9 @@ int24div(PG_FUNCTION_ARGS)
647653
int16 arg1 = PG_GETARG_INT16(0);
648654
int32 arg2 = PG_GETARG_INT32(1);
649655

656+
if (arg2 == 0)
657+
elog(ERROR, "division by zero");
658+
650659
PG_RETURN_INT32(arg1 / arg2);
651660
}
652661

@@ -683,6 +692,9 @@ int42div(PG_FUNCTION_ARGS)
683692
int32 arg1 = PG_GETARG_INT32(0);
684693
int16 arg2 = PG_GETARG_INT16(1);
685694

695+
if (arg2 == 0)
696+
elog(ERROR, "division by zero");
697+
686698
PG_RETURN_INT32(arg1 / arg2);
687699
}
688700

@@ -692,6 +704,9 @@ int4mod(PG_FUNCTION_ARGS)
692704
int32 arg1 = PG_GETARG_INT32(0);
693705
int32 arg2 = PG_GETARG_INT32(1);
694706

707+
if (arg2 == 0)
708+
elog(ERROR, "division by zero");
709+
695710
PG_RETURN_INT32(arg1 % arg2);
696711
}
697712

@@ -701,6 +716,9 @@ int2mod(PG_FUNCTION_ARGS)
701716
int16 arg1 = PG_GETARG_INT16(0);
702717
int16 arg2 = PG_GETARG_INT16(1);
703718

719+
if (arg2 == 0)
720+
elog(ERROR, "division by zero");
721+
704722
PG_RETURN_INT16(arg1 % arg2);
705723
}
706724

@@ -710,6 +728,9 @@ int24mod(PG_FUNCTION_ARGS)
710728
int16 arg1 = PG_GETARG_INT16(0);
711729
int32 arg2 = PG_GETARG_INT32(1);
712730

731+
if (arg2 == 0)
732+
elog(ERROR, "division by zero");
733+
713734
PG_RETURN_INT32(arg1 % arg2);
714735
}
715736

@@ -719,6 +740,9 @@ int42mod(PG_FUNCTION_ARGS)
719740
int32 arg1 = PG_GETARG_INT32(0);
720741
int16 arg2 = PG_GETARG_INT16(1);
721742

743+
if (arg2 == 0)
744+
elog(ERROR, "division by zero");
745+
722746
PG_RETURN_INT32(arg1 % arg2);
723747
}
724748

src/backend/utils/adt/int8.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.42 2002/09/18 21:35:22 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.43 2003/03/11 21:01:33 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -504,6 +504,9 @@ int8div(PG_FUNCTION_ARGS)
504504
int64 val1 = PG_GETARG_INT64(0);
505505
int64 val2 = PG_GETARG_INT64(1);
506506

507+
if (val2 == 0)
508+
elog(ERROR, "division by zero");
509+
507510
PG_RETURN_INT64(val1 / val2);
508511
}
509512

@@ -528,6 +531,9 @@ int8mod(PG_FUNCTION_ARGS)
528531
int64 val2 = PG_GETARG_INT64(1);
529532
int64 result;
530533

534+
if (val2 == 0)
535+
elog(ERROR, "division by zero");
536+
531537
result = val1 / val2;
532538
result *= val2;
533539
result = val1 - result;
@@ -621,6 +627,9 @@ int84div(PG_FUNCTION_ARGS)
621627
int64 val1 = PG_GETARG_INT64(0);
622628
int32 val2 = PG_GETARG_INT32(1);
623629

630+
if (val2 == 0)
631+
elog(ERROR, "division by zero");
632+
624633
PG_RETURN_INT64(val1 / val2);
625634
}
626635

@@ -657,6 +666,9 @@ int48div(PG_FUNCTION_ARGS)
657666
int32 val1 = PG_GETARG_INT32(0);
658667
int64 val2 = PG_GETARG_INT64(1);
659668

669+
if (val2 == 0)
670+
elog(ERROR, "division by zero");
671+
660672
PG_RETURN_INT64(val1 / val2);
661673
}
662674

src/backend/utils/adt/numeric.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* 1998 Jan Wieck
77
*
8-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.56 2002/10/19 02:08:17 momjian Exp $
8+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.57 2003/03/11 21:01:33 tgl Exp $
99
*
1010
* ----------
1111
*/
@@ -3266,7 +3266,7 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
32663266
*/
32673267
ndigits_tmp = var2->ndigits + 1;
32683268
if (ndigits_tmp == 1)
3269-
elog(ERROR, "division by zero on numeric");
3269+
elog(ERROR, "division by zero");
32703270

32713271
/*
32723272
* Determine the result sign, weight and number of digits to calculate

src/backend/utils/adt/timestamp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.79 2003/02/27 21:36:58 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.80 2003/03/11 21:01:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1869,7 +1869,7 @@ interval_div(PG_FUNCTION_ARGS)
18691869
result = (Interval *) palloc(sizeof(Interval));
18701870

18711871
if (factor == 0.0)
1872-
elog(ERROR, "interval_div: divide by 0.0 error");
1872+
elog(ERROR, "division by zero");
18731873

18741874
#ifdef HAVE_INT64_TIMESTAMP
18751875
result->month = (span->month / factor);

0 commit comments

Comments
 (0)