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 Unified Diffs Show Whitespace Changes Patch

Changes to src/math.c.

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
static int multWillOverflow(sqlite4_uint64 x, sqlite4_uint64 y){
  sqlite4_uint64 xHi, xLo, yHi, yLo;
  xHi = x>>32;
  yHi = y>>32;
  if( xHi*yHi ) return 1;
  xLo = x & 0xffffffff;
  yLo = y & 0xffffffff;
  if( (xHi*yLo + yHi*xLo)>0xffffffff ) return 1;
  return 0;
}

/*
** Multiply two numbers and return the result.
*/
sqlite4_num sqlite4_num_mul(sqlite4_num A, sqlite4_num B){







|







130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
static int multWillOverflow(sqlite4_uint64 x, sqlite4_uint64 y){
  sqlite4_uint64 xHi, xLo, yHi, yLo;
  xHi = x>>32;
  yHi = y>>32;
  if( xHi*yHi ) return 1;
  xLo = x & 0xffffffff;
  yLo = y & 0xffffffff;
  if( (xHi*yLo + yHi*xLo + (xLo*yLo>>32))>0xffffffff ) return 1;
  return 0;
}

/*
** Multiply two numbers and return the result.
*/
sqlite4_num sqlite4_num_mul(sqlite4_num A, sqlite4_num B){

Changes to test/num.test.

95
96
97
98
99
100
101








102
103
104
105
106
107
108
  8   NaN     -9      NaN
  9   NaN     NaN     NaN
} { 
  do_test num-5.1.$tn {
    sqlite4_num_to_text [sqlite4_num_mul $a $b]
  } $product
}









do_test num-6.1.1 {
  sqlite4_num_to_text [sqlite4_num_div 6 5]
} {1.2}
do_test num-6.1.2 {
  sqlite4_num_compare 2 [sqlite4_num_div 2 1]
} {equal}







>
>
>
>
>
>
>
>







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

do_test num-6.1.1 {
  sqlite4_num_to_text [sqlite4_num_div 6 5]
} {1.2}
do_test num-6.1.2 {
  sqlite4_num_compare 2 [sqlite4_num_div 2 1]
} {equal}