@ -681,18 +681,6 @@ int4mul(PG_FUNCTION_ARGS)
int32 arg2 = PG_GETARG_INT32 ( 1 ) ;
int32 arg2 = PG_GETARG_INT32 ( 1 ) ;
int32 result ;
int32 result ;
# ifdef WIN32
/*
* Win32 doesn ' t throw a catchable exception for SELECT - 2147483648 *
* ( - 1 ) ; - - INT_MIN
*/
if ( arg2 = = - 1 & & arg1 = = INT_MIN )
ereport ( ERROR ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
errmsg ( " integer out of range " ) ) ) ;
# endif
result = arg1 * arg2 ;
result = arg1 * arg2 ;
/*
/*
@ -709,7 +697,8 @@ int4mul(PG_FUNCTION_ARGS)
if ( ! ( arg1 > = ( int32 ) SHRT_MIN & & arg1 < = ( int32 ) SHRT_MAX & &
if ( ! ( arg1 > = ( int32 ) SHRT_MIN & & arg1 < = ( int32 ) SHRT_MAX & &
arg2 > = ( int32 ) SHRT_MIN & & arg2 < = ( int32 ) SHRT_MAX ) & &
arg2 > = ( int32 ) SHRT_MIN & & arg2 < = ( int32 ) SHRT_MAX ) & &
arg2 ! = 0 & &
arg2 ! = 0 & &
( result / arg2 ! = arg1 | | ( arg2 = = - 1 & & arg1 < 0 & & result < 0 ) ) )
( ( arg2 = = - 1 & & arg1 < 0 & & result < 0 ) | |
result / arg2 ! = arg1 ) )
ereport ( ERROR ,
ereport ( ERROR ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
errmsg ( " integer out of range " ) ) ) ;
errmsg ( " integer out of range " ) ) ) ;
@ -732,30 +721,27 @@ int4div(PG_FUNCTION_ARGS)
PG_RETURN_NULL ( ) ;
PG_RETURN_NULL ( ) ;
}
}
# ifdef WIN32
/*
/*
* Win32 doesn ' t throw a catchable exception for SELECT - 2147483648 /
* INT_MIN / - 1 is problematic , since the result can ' t be represented on a
* ( - 1 ) ; - - INT_MIN
* two ' s - complement machine . Some machines produce INT_MIN , some produce
* zero , some throw an exception . We can dodge the problem by recognizing
* that division by - 1 is the same as negation .
*/
*/
if ( arg2 = = - 1 & & arg1 = = INT_MIN )
if ( arg2 = = - 1 )
{
result = - arg1 ;
/* overflow check (needed for INT_MIN) */
if ( arg1 ! = 0 & & SAMESIGN ( result , arg1 ) )
ereport ( ERROR ,
ereport ( ERROR ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
errmsg ( " integer out of range " ) ) ) ;
errmsg ( " integer out of range " ) ) ) ;
# endif
PG_RETURN_INT32 ( result ) ;
}
/* No overflow is possible */
result = arg1 / arg2 ;
result = arg1 / arg2 ;
/*
* Overflow check . The only possible overflow case is for arg1 = INT_MIN ,
* arg2 = - 1 , where the correct result is - INT_MIN , which can ' t be
* represented on a two ' s - complement machine . Most machines produce
* INT_MIN but it seems some produce zero .
*/
if ( arg2 = = - 1 & & arg1 < 0 & & result < = 0 )
ereport ( ERROR ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
errmsg ( " integer out of range " ) ) ) ;
PG_RETURN_INT32 ( result ) ;
PG_RETURN_INT32 ( result ) ;
}
}
@ -877,21 +863,30 @@ int2div(PG_FUNCTION_ARGS)
PG_RETURN_NULL ( ) ;
PG_RETURN_NULL ( ) ;
}
}
result = arg1 / arg2 ;
/*
/*
* Overflow check . The only possible overflow case is for arg1 =
* SHRT_MIN / - 1 is problematic , since the result can ' t be represented on
* SHRT_MIN , arg2 = - 1 , where the correct result is - SHRT_MIN , which can ' t
* a two ' s - complement machine . Some machines produce SHRT_MIN , some
* be represented on a two ' s - complement machine . Most machines produce
* produce zero , some throw an exception . We can dodge the problem by
* SHRT_MIN but it seems some produce zero .
* recognizing that division by - 1 is the same as negation .
*/
*/
if ( arg2 = = - 1 & & arg1 < 0 & & result < = 0 )
if ( arg2 = = - 1 )
{
result = - arg1 ;
/* overflow check (needed for SHRT_MIN) */
if ( arg1 ! = 0 & & SAMESIGN ( result , arg1 ) )
ereport ( ERROR ,
ereport ( ERROR ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
errmsg ( " smallint out of range " ) ) ) ;
errmsg ( " smallint out of range " ) ) ) ;
PG_RETURN_INT16 ( result ) ;
PG_RETURN_INT16 ( result ) ;
}
}
/* No overflow is possible */
result = arg1 / arg2 ;
PG_RETURN_INT16 ( result ) ;
}
Datum
Datum
int24pl ( PG_FUNCTION_ARGS )
int24pl ( PG_FUNCTION_ARGS )
{
{
@ -1065,21 +1060,30 @@ int42div(PG_FUNCTION_ARGS)
PG_RETURN_NULL ( ) ;
PG_RETURN_NULL ( ) ;
}
}
result = arg1 / arg2 ;
/*
/*
* Overflow check . The only possible overflow case is for arg1 = INT_MIN ,
* INT_MIN / - 1 is problematic , since the result can ' t be represented on a
* arg2 = - 1 , where the correct result is - INT_MIN , which can ' t b e
* two ' s - complement machine . Some machines produce INT_MIN , some produc e
* represented on a two ' s - complement machine . Most machines produce
* zero , some throw an exception . We can dodge the problem by recognizing
* INT_MIN but it seems some produce zero .
* that division by - 1 is the same as negation .
*/
*/
if ( arg2 = = - 1 & & arg1 < 0 & & result < = 0 )
if ( arg2 = = - 1 )
{
result = - arg1 ;
/* overflow check (needed for INT_MIN) */
if ( arg1 ! = 0 & & SAMESIGN ( result , arg1 ) )
ereport ( ERROR ,
ereport ( ERROR ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
( errcode ( ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE ) ,
errmsg ( " integer out of range " ) ) ) ;
errmsg ( " integer out of range " ) ) ) ;
PG_RETURN_INT32 ( result ) ;
PG_RETURN_INT32 ( result ) ;
}
}
/* No overflow is possible */
result = arg1 / arg2 ;
PG_RETURN_INT32 ( result ) ;
}
Datum
Datum
int4mod ( PG_FUNCTION_ARGS )
int4mod ( PG_FUNCTION_ARGS )
{
{