]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Integer overflow in SUM causes an exception. Ticket #1669. (CVS 3064)
authordrh <drh@noemail.net>
Thu, 9 Feb 2006 17:47:42 +0000 (17:47 +0000)
committerdrh <drh@noemail.net>
Thu, 9 Feb 2006 17:47:42 +0000 (17:47 +0000)
FossilOrigin-Name: c72b946198128cbceb12dffbdf4706d9fda0fd72

manifest
manifest.uuid
src/func.c
test/func.test

index a92d55f323a6e17a31b21358d000be1025a034be..d7b8d4fddba2ff750a0985dc1af898ca3af10cf6 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Back\sout\scheck-in\s(3058)\s-\sit\sbreaks\stoo\smuch\sapplication\scode.\s(CVS\s3063)
-D 2006-02-09T16:52:24
+C Integer\soverflow\sin\sSUM\scauses\san\sexception.\s\sTicket\s#1669.\s(CVS\s3064)
+D 2006-02-09T17:47:42
 F Makefile.in 5d8dff443383918b700e495de42ec65bc1c8865b
 F Makefile.linux-gcc 74ba0eadf88748a9ce3fd03d2a3ede2e6715baec
 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -43,7 +43,7 @@ F src/date.c cd2bd5d1ebc6fa12d6312f69789ae5b0a2766f2e
 F src/delete.c 56ab34c3a384caa5d5ea06f5739944957e2e4213
 F src/experimental.c 1b2d1a6cd62ecc39610e97670332ca073c50792b
 F src/expr.c 1149c3380bfce27703f5e9bec7dfb8e51baaf9d9
-F src/func.c b21d89f5a11634847c9ab3ead42e75586db538b2
+F src/func.c aad8c94768fe6b36563416facc19a198b5d7f8ed
 F src/hash.c 8747cf51d12de46512880dfcf1b68b4e24072863
 F src/hash.h 1b0c445e1c89ff2aaad9b4605ba61375af001e84
 F src/insert.c 7e931b7f06afbcefcbbaab175c02eff8268db33f
@@ -164,7 +164,7 @@ F test/enc3.test 890508efff6677345e93bf2a8adb0489b30df030
 F test/expr.test 4e65cade931e14a0194eee41e33707e7af5f397a
 F test/fkey1.test 153004438d51e6769fb1ce165f6313972d6263ce
 F test/format4.test 9f31d41d4f926cab97b2ebe6be00a6ab12dece87
-F test/func.test ef312ca92480e5bfa699c30643e4174247db686d
+F test/func.test 458898c38bbb94e59ba4d96eff05731579acec33
 F test/hook.test 7e7645fd9a033f79cce8fdff151e32715e7ec50a
 F test/in.test 40feeebc7e38576255051aad428322be1545e0f1
 F test/index.test c478459611ded74745fee57f99f424da8a5f5fbd
@@ -351,7 +351,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
-P 1ac72f68c0e9fd63decc97c166f49b405a9d323c
-R 8e4c508c267bfbc23c6be5d5e383bf10
+P 731f1e3245fafbda24355d5bdc5b249c4229305f
+R 22cd9586db53f15d6d1d14e20e2bf24f
 U drh
-Z 7f3d92afc83bfa30fa7e5b6bf900d3a0
+Z 643c5022622d4cd302ee4b3efceecf09
index 33dde44aef53920a9b6c41c9abba780b159b503c..5cd4f5e273e1263b6c3849eb884b080dacdb83c9 100644 (file)
@@ -1 +1 @@
-731f1e3245fafbda24355d5bdc5b249c4229305f
\ No newline at end of file
+c72b946198128cbceb12dffbdf4706d9fda0fd72
\ No newline at end of file
index 35b1ce9a0cd99e045d9269b58563345e58c57012..5cd4c9ea87658a622e72af3719c887be1eb5177f 100644 (file)
@@ -16,7 +16,7 @@
 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
 ** All other code has file scope.
 **
-** $Id: func.c,v 1.118 2006/02/09 13:38:20 drh Exp $
+** $Id: func.c,v 1.119 2006/02/09 17:47:42 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -849,10 +849,13 @@ static void sumFinalize(sqlite3_context *context){
   SumCtx *p;
   p = sqlite3_aggregate_context(context, 0);
   if( p && p->cnt>0 ){
+    i64 iVal = (i64)p->sum;
     if( p->seenFloat ){
       sqlite3_result_double(context, p->sum);
+    }else if( p->sum==(LONGDOUBLE_TYPE)iVal ){
+      sqlite3_result_int64(context, iVal);
     }else{
-      sqlite3_result_int64(context, (i64)p->sum);
+      sqlite3_result_error(context, "integer overflow", -1);
     }
   }
 }
index cf4b7383a86f16c85fb784ee39adc356f3190b09..d2b274de9d00bf18b8ab908cdbb799ae7de7cdea 100644 (file)
@@ -11,7 +11,7 @@
 # This file implements regression tests for SQLite library.  The
 # focus of this file is testing built-in functions.
 #
-# $Id: func.test,v 1.45 2006/02/09 13:38:21 drh Exp $
+# $Id: func.test,v 1.46 2006/02/09 17:47:42 drh Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -513,30 +513,32 @@ do_test func-18.2 {
 
 # The sum of nothing is NULL.  But the sum of all NULLs is NULL.
 #
+# The TOTAL of nothing is 0.0.
+#
 do_test func-18.3 {
   execsql {
     DELETE FROM t5;
-    SELECT sum(x) FROM t5;
+    SELECT sum(x), total(x) FROM t5;
   }
-} {{}}
+} {{} 0.0}
 do_test func-18.4 {
   execsql {
     INSERT INTO t5 VALUES(NULL);
-    SELECT sum(x) FROM t5
+    SELECT sum(x), total(x) FROM t5
   }
-} {{}}
+} {{} 0.0}
 do_test func-18.5 {
   execsql {
     INSERT INTO t5 VALUES(NULL);
-    SELECT sum(x) FROM t5
+    SELECT sum(x), total(x) FROM t5
   }
-} {{}}
+} {{} 0.0}
 do_test func-18.6 {
   execsql {
     INSERT INTO t5 VALUES(123);
-    SELECT sum(x) FROM t5
+    SELECT sum(x), total(x) FROM t5
   }
-} {123}
+} {123 123.0}
 
 # Ticket #1664: 64-bit overflow in sum()
 #
@@ -549,4 +551,28 @@ do_test func-18.10 {
   }
 } 0
 
+# Ticket #1669: If an integer SUM overflows, throw an error, thus
+# making the SQL-standard version SUM() even more useless than it
+# was before.  
+#
+# The non-standard TOTAL() function continues to give a helpful result.
+#
+do_test func-18.11 {
+  execsql {
+    SELECT typeof(sum(x)) FROM t6
+  }
+} integer
+do_test func-18.12 {
+  catchsql {
+    INSERT INTO t6 VALUES(1<<62);
+    SELECT sum(x) - ((1<<62)*2.0+1) from t6;
+  }
+} {1 {integer overflow}}
+do_test func-18.13 {
+  execsql {
+    SELECT total(x) - ((1<<62)*2.0+1) FROM t6
+  }
+} 0.0
+
+
 finish_test