* Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.32 2001/08/24 14:07:49 petere Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.33 2001/09/07 01:33:44 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
     * Do our own scan, rather than relying on sscanf which might be
     * broken for long long.
     */
-   while (*ptr && isspace((unsigned char) *ptr))       /* skip leading spaces */
+   while (*ptr && isspace((unsigned char) *ptr)) /* skip leading spaces */
        ptr++;
    if (*ptr == '-')            /* handle sign */
        sign = -1, ptr++;
    int64       val = PG_GETARG_INT64(0);
    int32       result;
 
-   if ((val < INT_MIN) || (val > INT_MAX))
-       elog(ERROR, "int8 conversion to int4 is out of range");
-
    result = (int32) val;
 
+   /* Test for overflow by reverse-conversion. */
+   if ((int64) result != val)
+       elog(ERROR, "int8 conversion to int4 is out of range");
+
    PG_RETURN_INT32(result);
 }