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

Overview
Comment:Merge overflow and rounding fixes for sqlite4_num_to_int64().
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e8db1e81a4d3020ea8a2913534d2963d53dca484
User & Date: dan 2013-06-03 15:23:11.958
Context
2013-06-03
19:25
Remove the use of type 'double' from date.c. check-in: fa869b0982 user: dan tags: trunk
15:23
Merge overflow and rounding fixes for sqlite4_num_to_int64(). check-in: e8db1e81a4 user: dan tags: trunk
11:47
Modify the round() and abs() SQL functions to use decimal arithmetic. check-in: 7b3bb63808 user: dan tags: trunk
2013-06-01
23:42
Handle overflow and rounding cases in sqlite4_num_to_int64. check-in: de7fb192cb user: peterreid tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/math.c.
527
528
529
530
531
532
533




534
535
536
537
538
539
540
541
542


543
544
545
546
547
548
549

550
551
552

553
554
555
556
557

558
559
560
561
562
563
564
565
566
}

/*
** Convert the number passed as the first argument to a signed 64-bit
** integer and return the value. If the second argument is not NULL,
** then set the value that it points to 1 if data was lost as part
** of the conversion, or 0 otherwise.




*/
sqlite4_int64 sqlite4_num_to_int64(sqlite4_num num, int *pbLossy){
  static const i64 L10 = (LARGEST_INT64 / 10);
  i64 iRet;
  int i;
  iRet = num.m;

  if( pbLossy ){
    *pbLossy = (num.m==LARGEST_UINT64 || (num.m+num.sign)>(u64)LARGEST_INT64);


  }
  for(i=num.e; i<0; i++){
    if( pbLossy && (iRet % 10) ) *pbLossy = 1;
    iRet = iRet / 10;
  }
  for(i=0; i<num.e; i++){
    if( pbLossy && iRet>L10 ) *pbLossy = 1;

    iRet = iRet * 10;
  }



#define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
#define LARGEST_UINT64  (0xffffffff|(((u64)0xffffffff)<<32))


  if( num.sign ) iRet = iRet*-1;
  return iRet;
}


/*
** Convert an integer into text in the buffer supplied. The
** text is zero-terminated and right-justified in the buffer.
** A pointer to the first character of text is returned.







>
>
>
>



|



|
|
>
>





|
|
>
|
|
|
>
|
<
<
<

>
|
|







527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561



562
563
564
565
566
567
568
569
570
571
572
}

/*
** Convert the number passed as the first argument to a signed 64-bit
** integer and return the value. If the second argument is not NULL,
** then set the value that it points to 1 if data was lost as part
** of the conversion, or 0 otherwise.
**
** Values round towards 0. If the number is outside the range that a
** signed 64-bit integer can represent, it is clamped to be inside
** that range.
*/
sqlite4_int64 sqlite4_num_to_int64(sqlite4_num num, int *pbLossy){
  static const i64 L10 = (LARGEST_INT64 / 10);
  u64 iRet;
  int i;
  iRet = num.m;

  if( pbLossy ) *pbLossy = 0;
  for(i=0; i<num.e; i++){
    if( iRet>L10 ) goto overflow;
    iRet = iRet * 10;
  }
  for(i=num.e; i<0; i++){
    if( pbLossy && (iRet % 10) ) *pbLossy = 1;
    iRet = iRet / 10;
  }

  if( num.sign ){
    if( iRet>(u64)LARGEST_INT64+1 ) goto overflow;
    return -(i64)iRet;
  }else{
    if( iRet>(u64)LARGEST_INT64 ) goto overflow; 
    return (i64)iRet;
  }




overflow:
  if( pbLossy ) *pbLossy = 1;
  return num.sign ? -LARGEST_INT64-1 : LARGEST_INT64;
}


/*
** Convert an integer into text in the buffer supplied. The
** text is zero-terminated and right-justified in the buffer.
** A pointer to the first character of text is returned.
Changes to test/num.test.
155
156
157
158
159
160
161























162
163
164
165
166
  5.1   1   {sign:0 approx:0 e:0 m:1}
  5.2   1.0 {sign:0 approx:0 e:-1 m:10}
  5.3   1.  {sign:0 approx:0 e:0 m:1}
  5.4   1e0 {sign:0 approx:0 e:0 m:1}
} {
  do_test num-9.1.$tn { sqlite4_num_from_text $in } [list {*}$out]
}
























#-------------------------------------------------------------------------
finish_test









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  5.1   1   {sign:0 approx:0 e:0 m:1}
  5.2   1.0 {sign:0 approx:0 e:-1 m:10}
  5.3   1.  {sign:0 approx:0 e:0 m:1}
  5.4   1e0 {sign:0 approx:0 e:0 m:1}
} {
  do_test num-9.1.$tn { sqlite4_num_from_text $in } [list {*}$out]
}

foreach {tn in out} {
  0     50                                            50
  1     -94                                           -94
  2     {sign:0 approx:0 e:4 m:2}                     20000
  3     9223372036854775807                           9223372036854775807
  4     -9223372036854775808                          -9223372036854775808
  5     {sign:0 approx:0 e:-1 m:51}                   ~5
  6     {sign:0 approx:0 e:0 m:9223372036854775808}  ~9223372036854775807
  7     9223372036854775808                          ~9223372036854775807
  8     10000000000000000000                         ~9223372036854775807
  9     -10000000000000000000                        ~-9223372036854775808
  10    {sign:0 approx:0 e:50 m:244}                 ~9223372036854775807
  11    {sign:1 approx:0 e:50 m:34220}               ~-9223372036854775808
  12    50.1                                         ~50
  13    10.9                                         ~10
  14    .995                                         ~0
  15    -93.9                                        ~-93
  16    -12.1                                        ~-12
} {
  do_test num-10.1.$tn { sqlite4_num_to_int64 $in } [list {*}$out]
}


#-------------------------------------------------------------------------
finish_test


Changes to test/test_mem.c.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string.h>

#include "sqliteInt.h"
#include "testInt.h"

#define MIN(x,y) ((x)<(y) ? (x) : (y))

typedef unsigned int  u32;
typedef unsigned char u8;
typedef long long int i64;
typedef unsigned long long int u64;

#if defined(__GLIBC__)
  extern int backtrace(void**,int);
  extern void backtrace_symbols_fd(void*const*,int,int);
# define TM_BACKTRACE 12
#else
# define backtrace(A,B) 1
# define backtrace_symbols_fd(A,B,C)







<
<
<
<
<







14
15
16
17
18
19
20





21
22
23
24
25
26
27
#include <string.h>

#include "sqliteInt.h"
#include "testInt.h"

#define MIN(x,y) ((x)<(y) ? (x) : (y))






#if defined(__GLIBC__)
  extern int backtrace(void**,int);
  extern void backtrace_symbols_fd(void*const*,int,int);
# define TM_BACKTRACE 12
#else
# define backtrace(A,B) 1
# define backtrace_symbols_fd(A,B,C)
Changes to test/test_num.c.
235
236
237
238
239
240
241
























242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

260
261
262
263
264
265
266

  ret = sqlite4_num_from_double(val);

  Tcl_ResetResult(interp);
  append_num_result(interp, ret);
  return TCL_OK;
}

























/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest_num_init(Tcl_Interp *interp){
  static struct {
     char *zName;
     Tcl_CmdProc *xProc;
  } aCmd[] = {
     { "sqlite4_num_compare",           (Tcl_CmdProc*)test_num_compare      }, 
     { "sqlite4_num_from_text",         (Tcl_CmdProc*)test_num_from_text    }, 
     { "sqlite4_num_to_text",           (Tcl_CmdProc*)test_num_to_text      },
     { "sqlite4_num_add",               (Tcl_CmdProc*)test_num_add          },
     { "sqlite4_num_sub",               (Tcl_CmdProc*)test_num_sub          },
     { "sqlite4_num_mul",               (Tcl_CmdProc*)test_num_mul          },
     { "sqlite4_num_div",               (Tcl_CmdProc*)test_num_div          },
     { "sqlite4_num_isinf",             (Tcl_CmdProc*)test_num_isinf        },
     { "sqlite4_num_isnan",             (Tcl_CmdProc*)test_num_isnan        },

  };

  static struct {
     char *zName;
     Tcl_ObjCmdProc *xProc;
     void *clientData;
  } aObjCmd[] = {







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


















>







235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

  ret = sqlite4_num_from_double(val);

  Tcl_ResetResult(interp);
  append_num_result(interp, ret);
  return TCL_OK;
}


static int test_num_to_int64(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  sqlite4_num A;
  int lossy;
  sqlite4_int64 iVal;
  char buf[50];
  if( argc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
      " NUM\"", 0);
    return TCL_ERROR;
  }
  A = test_parse_num(argv[1]);
  iVal = sqlite4_num_to_int64(A, &lossy);
  sprintf( buf, "%s%lld", lossy?"~":"", iVal );
  Tcl_AppendResult(interp, buf, 0);
  return TCL_OK;
}


/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest_num_init(Tcl_Interp *interp){
  static struct {
     char *zName;
     Tcl_CmdProc *xProc;
  } aCmd[] = {
     { "sqlite4_num_compare",           (Tcl_CmdProc*)test_num_compare      }, 
     { "sqlite4_num_from_text",         (Tcl_CmdProc*)test_num_from_text    }, 
     { "sqlite4_num_to_text",           (Tcl_CmdProc*)test_num_to_text      },
     { "sqlite4_num_add",               (Tcl_CmdProc*)test_num_add          },
     { "sqlite4_num_sub",               (Tcl_CmdProc*)test_num_sub          },
     { "sqlite4_num_mul",               (Tcl_CmdProc*)test_num_mul          },
     { "sqlite4_num_div",               (Tcl_CmdProc*)test_num_div          },
     { "sqlite4_num_isinf",             (Tcl_CmdProc*)test_num_isinf        },
     { "sqlite4_num_isnan",             (Tcl_CmdProc*)test_num_isnan        },
     { "sqlite4_num_to_int64",          (Tcl_CmdProc*)test_num_to_int64     },
  };

  static struct {
     char *zName;
     Tcl_ObjCmdProc *xProc;
     void *clientData;
  } aObjCmd[] = {