b96350
diff -ur sqlite-src.old/src/util.c sqlite-src-3071700/src/util.c
b96350
--- sqlite-src.old/src/util.c	2013-11-28 09:57:32.167493980 +0100
b96350
+++ sqlite-src-3071700/src/util.c	2013-11-28 09:59:01.877811972 +0100
b96350
@@ -511,7 +511,7 @@
b96350
     u = u*10 + c - '0';
b96350
   }
b96350
   if( u>LARGEST_INT64 ){
b96350
-    *pNum = SMALLEST_INT64;
b96350
+    *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
b96350
   }else if( neg ){
b96350
     *pNum = -(i64)u;
b96350
   }else{
b96350
@@ -542,7 +542,6 @@
b96350
       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
b96350
       ** special case 2 overflow if positive */
b96350
       assert( u-1==LARGEST_INT64 );
b96350
-      assert( (*pNum)==SMALLEST_INT64 );
b96350
       return neg ? 0 : 2;
b96350
     }
b96350
   }
b96350
diff -ur sqlite-src.old/src/vdbe.c sqlite-src-3071700/src/vdbe.c
b96350
--- sqlite-src.old/src/vdbe.c	2013-11-28 09:57:32.162493963 +0100
b96350
+++ sqlite-src-3071700/src/vdbe.c	2013-11-28 10:04:01.533814781 +0100
b96350
@@ -3465,7 +3465,9 @@
b96350
         ** point number. */
b96350
         assert( (pIn3->flags & MEM_Real)!=0 );
b96350
 
b96350
-        if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
b96350
+        if( (iKey==SMALLEST_INT64 && pIn3->r<(double)iKey)
b96350
+         || (iKey==LARGEST_INT64 && pIn3->r>(double)iKey)
b96350
+        ){
b96350
           /* The P3 value is too large in magnitude to be expressed as an
b96350
           ** integer. */
b96350
           res = 1;
b96350
diff -ur sqlite-src.old/src/vdbemem.c sqlite-src-3071700/src/vdbemem.c
b96350
--- sqlite-src.old/src/vdbemem.c	2013-11-28 09:57:32.162493963 +0100
b96350
+++ sqlite-src-3071700/src/vdbemem.c	2013-11-28 10:00:14.877065531 +0100
b96350
@@ -303,15 +303,8 @@
b96350
 
b96350
 /*
b96350
 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
b96350
-** If the double is too large, return 0x8000000000000000.
b96350
-**
b96350
-** Most systems appear to do this simply by assigning
b96350
-** variables and without the extra range tests.  But
b96350
-** there are reports that windows throws an expection
b96350
-** if the floating point value is out of range. (See ticket #2880.)
b96350
-** Because we do not completely understand the problem, we will
b96350
-** take the conservative approach and always do range tests
b96350
-** before attempting the conversion.
b96350
+** If the double is out of range of a 64-bit signed integer then
b96350
+** return the closest available 64-bit signed integer.
b96350
 */
b96350
 static i64 doubleToInt64(double r){
b96350
 #ifdef SQLITE_OMIT_FLOATING_POINT
b96350
@@ -328,14 +321,10 @@
b96350
   static const i64 maxInt = LARGEST_INT64;
b96350
   static const i64 minInt = SMALLEST_INT64;
b96350
 
b96350
-  if( r<(double)minInt ){
b96350
-    return minInt;
b96350
-  }else if( r>(double)maxInt ){
b96350
-    /* minInt is correct here - not maxInt.  It turns out that assigning
b96350
-    ** a very large positive number to an integer results in a very large
b96350
-    ** negative integer.  This makes no sense, but it is what x86 hardware
b96350
-    ** does so for compatibility we will do the same in software. */
b96350
+  if( r<=(double)minInt ){
b96350
     return minInt;
b96350
+  }else if( r>=(double)maxInt ){
b96350
+    return maxInt;
b96350
   }else{
b96350
     return (i64)r;
b96350
   }
b96350
@@ -417,17 +406,11 @@
b96350
   **
b96350
   ** The second and third terms in the following conditional enforces
b96350
   ** the second condition under the assumption that addition overflow causes
b96350
-  ** values to wrap around.  On x86 hardware, the third term is always
b96350
-  ** true and could be omitted.  But we leave it in because other
b96350
-  ** architectures might behave differently.
b96350
+  ** values to wrap around.
b96350
   */
b96350
   if( pMem->r==(double)pMem->u.i
b96350
    && pMem->u.i>SMALLEST_INT64
b96350
-#if defined(__i486__) || defined(__x86_64__)
b96350
-   && ALWAYS(pMem->u.i
b96350
-#else
b96350
    && pMem->u.i
b96350
-#endif
b96350
   ){
b96350
     pMem->flags |= MEM_Int;
b96350
   }
b96350
diff -ur sqlite-src.old/test/autoinc.test sqlite-src-3071700/test/autoinc.test
b96350
--- sqlite-src.old/test/autoinc.test	2013-11-28 09:57:32.145493901 +0100
b96350
+++ sqlite-src-3071700/test/autoinc.test	2013-11-28 10:00:25.973101898 +0100
b96350
@@ -216,7 +216,7 @@
b96350
 } {t1 1238}
b96350
 do_test autoinc-2.28 {
b96350
   execsql {
b96350
-    UPDATE sqlite_sequence SET seq='12345678901234567890'
b96350
+    UPDATE sqlite_sequence SET seq='-12345678901234567890'
b96350
       WHERE name='t1';
b96350
     INSERT INTO t1 VALUES(NULL,6);
b96350
     SELECT * FROM t1;
b96350
diff -ur sqlite-src.old/test/e_expr.test sqlite-src-3071700/test/e_expr.test
b96350
--- sqlite-src.old/test/e_expr.test	2013-11-28 09:57:32.130493848 +0100
b96350
+++ sqlite-src-3071700/test/e_expr.test	2013-11-28 10:00:32.053121919 +0100
b96350
@@ -1606,14 +1606,14 @@
b96350
 # an INTEGER then the result of the cast is the largest negative
b96350
 # integer: -9223372036854775808.
b96350
 #
b96350
-do_expr_test e_expr-31.2.1 { CAST(2e+50 AS INT) } integer -9223372036854775808
b96350
+do_expr_test e_expr-31.2.1 { CAST(2e+50 AS INT) } integer 9223372036854775807
b96350
 do_expr_test e_expr-31.2.2 { CAST(-2e+50 AS INT) } integer -9223372036854775808
b96350
 do_expr_test e_expr-31.2.3 { 
b96350
   CAST(-9223372036854775809.0 AS INT)
b96350
 } integer -9223372036854775808
b96350
 do_expr_test e_expr-31.2.4 { 
b96350
   CAST(9223372036854775809.0 AS INT)
b96350
-} integer -9223372036854775808
b96350
+} integer 9223372036854775807
b96350
 
b96350
 
b96350
 # EVIDENCE-OF: R-09295-61337 Casting a TEXT or BLOB value into NUMERIC