SQLite4
Check-in [2eba06d522]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix undetected multiplication overflow
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2eba06d52264b17f5a2562483248e15d60804f61
User & Date: peterreid 2013-10-23 22:43:10
Context
2013-10-23
22:54
Prevent unnecessary rounding before multiplying check-in: 68dfaa3491 user: peterreid tags: trunk
22:43
Fix undetected multiplication overflow check-in: 2eba06d522 user: peterreid tags: trunk
20:28
Add connect/disconnect locking. And locking primitives for single-process mode. check-in: f7fc6aeec8 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/math.c.

   130    130   static int multWillOverflow(sqlite4_uint64 x, sqlite4_uint64 y){
   131    131     sqlite4_uint64 xHi, xLo, yHi, yLo;
   132    132     xHi = x>>32;
   133    133     yHi = y>>32;
   134    134     if( xHi*yHi ) return 1;
   135    135     xLo = x & 0xffffffff;
   136    136     yLo = y & 0xffffffff;
   137         -  if( (xHi*yLo + yHi*xLo)>0xffffffff ) return 1;
          137  +  if( (xHi*yLo + yHi*xLo + (xLo*yLo>>32))>0xffffffff ) return 1;
   138    138     return 0;
   139    139   }
   140    140   
   141    141   /*
   142    142   ** Multiply two numbers and return the result.
   143    143   */
   144    144   sqlite4_num sqlite4_num_mul(sqlite4_num A, sqlite4_num B){

Changes to test/num.test.

    95     95     8   NaN     -9      NaN
    96     96     9   NaN     NaN     NaN
    97     97   } { 
    98     98     do_test num-5.1.$tn {
    99     99       sqlite4_num_to_text [sqlite4_num_mul $a $b]
   100    100     } $product
   101    101   }
          102  +# 5.2: Overflow that depends on the product of the two least 
          103  +# significant halves
          104  +do_test num-5.2.1 {
          105  +  sqlite4_num_mul 3 6148914691236517211
          106  +} {sign:0 approx:1 e:1 m:1844674407370955163}
          107  +do_test num-5.2.2 {
          108  +  sqlite4_num_mul 3 6148914690091192593
          109  +} {sign:0 approx:0 e:0 m:18446744070273577779}
   102    110   
   103    111   do_test num-6.1.1 {
   104    112     sqlite4_num_to_text [sqlite4_num_div 6 5]
   105    113   } {1.2}
   106    114   do_test num-6.1.2 {
   107    115     sqlite4_num_compare 2 [sqlite4_num_div 2 1]
   108    116   } {equal}