Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Rework the string to integer conversion logic to address tickets #2344 and #2454. (CVS 4123) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
5b3a490649ab88c168e3e5bf2efbc7f6 |
User & Date: | drh 2007-06-25 17:28:01.000 |
Context
2007-06-26
| ||
00:37 | Change the name of sqlite3_atoi64 back to sqlite3Atoi64 so that it is clearly and internal API. Modify the amalgamation to use a macro SQLITE_PRIVATE instead of the keyword "static" so the macro can be overridden to by nothing. (CVS 4124) (check-in: 96190cf13d user: drh tags: trunk) | |
2007-06-25
| ||
17:28 | Rework the string to integer conversion logic to address tickets #2344 and #2454. (CVS 4123) (check-in: 5b3a490649 user: drh tags: trunk) | |
16:29 | Fix for #2445. A bug in the lookupName() logic that could cause a crash when a WHERE clause used an alias to refer to an expression in the result-set of the SELECT, and that expression was itself a reference to a table column. (CVS 4122) (check-in: 044ca1c72a user: danielk1977 tags: trunk) | |
Changes
Changes to src/sqliteInt.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** ** @(#) $Id: sqliteInt.h,v 1.576 2007/06/25 17:28:01 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ #include "sqliteLimit.h" #if defined(SQLITE_TCL) || defined(TCLSH) |
︙ | ︙ | |||
1831 1832 1833 1834 1835 1836 1837 | int sqlite3GetVarint32(const unsigned char *, u32 *); int sqlite3VarintLen(u64 v); void sqlite3IndexAffinityStr(Vdbe *, Index *); void sqlite3TableAffinityStr(Vdbe *, Table *); char sqlite3CompareAffinity(Expr *pExpr, char aff2); int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity); char sqlite3ExprAffinity(Expr *pExpr); | | | 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 | int sqlite3GetVarint32(const unsigned char *, u32 *); int sqlite3VarintLen(u64 v); void sqlite3IndexAffinityStr(Vdbe *, Index *); void sqlite3TableAffinityStr(Vdbe *, Table *); char sqlite3CompareAffinity(Expr *pExpr, char aff2); int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity); char sqlite3ExprAffinity(Expr *pExpr); int sqlite3_atoi64(const char*, i64*); void sqlite3Error(sqlite3*, int, const char*,...); void *sqlite3HexToBlob(const char *z); int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); const char *sqlite3ErrStr(int); int sqlite3ReadSchema(Parse *pParse); CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char *,int,int); CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName); |
︙ | ︙ |
Changes to src/test1.c.
︙ | ︙ | |||
9 10 11 12 13 14 15 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing all sorts of SQLite interfaces. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** | | | 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing all sorts of SQLite interfaces. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** ** $Id: test1.c,v 1.257 2007/06/25 17:28:02 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" #include "os.h" #include <stdlib.h> #include <string.h> |
︙ | ︙ | |||
1078 1079 1080 1081 1082 1083 1084 | /* ** If zNum represents an integer that will fit in 64-bits, then set ** *pValue to that integer and return true. Otherwise return false. */ static int sqlite3GetInt64(const char *zNum, i64 *pValue){ if( sqlite3FitsIn64Bits(zNum) ){ | | | 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 | /* ** If zNum represents an integer that will fit in 64-bits, then set ** *pValue to that integer and return true. Otherwise return false. */ static int sqlite3GetInt64(const char *zNum, i64 *pValue){ if( sqlite3FitsIn64Bits(zNum) ){ sqlite3_atoi64(zNum, pValue); return 1; } return 0; } /* ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER |
︙ | ︙ |
Changes to src/util.c.
︙ | ︙ | |||
10 11 12 13 14 15 16 | ** ************************************************************************* ** Utility functions used throughout sqlite. ** ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** | | | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ** ************************************************************************* ** Utility functions used throughout sqlite. ** ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** ** $Id: util.c,v 1.206 2007/06/25 17:28:02 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <stdarg.h> #include <ctype.h> |
︙ | ︙ | |||
294 295 296 297 298 299 300 301 302 303 304 305 | } *pResult = sign<0 ? -v1 : v1; return z - zBegin; #else return sqlite3Atoi64(z, pResult); #endif /* SQLITE_OMIT_FLOATING_POINT */ } /* ** Return TRUE if zNum is a 64-bit signed integer and write ** the value of the integer into *pNum. If zNum is not an integer ** or is an integer that is too large to be expressed with 64 bits, | > > > > > > > > > > > > > > > > > > > > > > > | < | > > > > | | < | < < < < < < < < < < < < < < < | | | > | < < < < < < | < > | > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > | > > > | > > > > > > > > > > > > | 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | } *pResult = sign<0 ? -v1 : v1; return z - zBegin; #else return sqlite3Atoi64(z, pResult); #endif /* SQLITE_OMIT_FLOATING_POINT */ } /* ** Compare the 19-character string zNum against the text representation ** value 2^63: 9223372036854775808. Return negative, zero, or positive ** if zNum is less than, equal to, or greater than the string. ** ** Unlike memcmp() this routine is guaranteed to return the difference ** in the values of the last digit if the only difference is in the ** last digit. So, for example, ** ** compare2pow63("9223372036854775800") ** ** will return -8. */ static int compare2pow63(const char *zNum){ int c; c = memcmp(zNum,"922337203685477580",18); if( c==0 ){ c = zNum[18] - '8'; } return c; } /* ** Return TRUE if zNum is a 64-bit signed integer and write ** the value of the integer into *pNum. If zNum is not an integer ** or is an integer that is too large to be expressed with 64 bits, ** then return false. ** ** When this routine was originally written it dealt with only ** 32-bit numbers. At that time, it was much faster than the ** atoi() library routine in RedHat 7.2. */ int sqlite3_atoi64(const char *zNum, i64 *pNum){ i64 v = 0; int neg; int i, c; while( isspace(*(u8*)zNum) ) zNum++; if( *zNum=='-' ){ neg = 1; zNum++; }else if( *zNum=='+' ){ neg = 0; zNum++; }else{ neg = 0; } while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */ for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ v = v*10 + c - '0'; } *pNum = neg ? -v : v; if( c!=0 || i==0 || i>19 ){ /* zNum is empty or contains non-numeric text or is longer ** than 19 digits (thus guaranting that it is too large) */ return 0; }else if( i<19 ){ /* Less than 19 digits, so we know that it fits in 64 bits */ return 1; }else{ /* 19-digit numbers must be no larger than 9223372036854775807 if positive ** or 9223372036854775808 if negative. Note that 9223372036854665808 ** is 2^63. */ return compare2pow63(zNum)<neg; } } /* ** The string zNum represents an integer. There might be some other ** information following the integer too, but that part is ignored. ** If the integer that the prefix of zNum represents will fit in a ** 64-bit signed integer, return TRUE. Otherwise return FALSE. ** ** This routine returns FALSE for the string -9223372036854775808 even that ** that number will, in theory fit in a 64-bit integer. Positive ** 9223373036854775808 will not fit in 64 bits. So it seems safer to return ** false. */ int sqlite3FitsIn64Bits(const char *zNum){ int i, c; int neg = 0; if( *zNum=='-' ){ neg = 1; zNum++; }else if( *zNum=='+' ){ zNum++; } while( *zNum=='0' ){ zNum++; /* Skip leading zeros. Ticket #2454 */ } for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} if( i<19 ){ /* Guaranteed to fit if less than 19 digits */ return 1; }else if( i>19 ){ /* Guaranteed to be too big if greater than 19 digits */ return 0; }else{ /* Compare against 2^63. */ return compare2pow63(zNum)<neg; } } /* ** If zNum represents an integer that will fit in 32-bits, then set ** *pValue to that integer and return true. Otherwise return false. ** ** Any non-numeric characters that following zNum are ignored. ** This is different from sqlite3_atoi64() which requires the ** input number to be zero-terminated. */ int sqlite3GetInt32(const char *zNum, int *pValue){ sqlite_int64 v = 0; int i, c; int neg = 0; if( zNum[0]=='-' ){ neg = 1; zNum++; }else if( zNum[0]=='+' ){ zNum++; } while( zNum[0]=='0' ) zNum++; for(i=0; i<10 && (c = zNum[i] - '0')>=0 && c<=9; i++){ v = v*10 + c; } if( i>9 ){ return 0; } if( v-neg>2147483647 ){ return 0; } if( neg ){ v = -v; } *pValue = (int)v; return 1; } /* ** Check to make sure we have a valid db pointer. This test is not ** foolproof but it does provide some measure of protection against ** misuse of the interface such as passing in db pointers that are ** NULL or which have been previously closed. If this routine returns ** TRUE it means that the db pointer is invalid and should not be |
︙ | ︙ |
Changes to src/vdbe.c.
︙ | ︙ | |||
39 40 41 42 43 44 45 | ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** | | | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** ** $Id: vdbe.c,v 1.631 2007/06/25 17:28:02 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include <math.h> #include "vdbeInt.h" |
︙ | ︙ | |||
223 224 225 226 227 228 229 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ int realnum; sqlite3VdbeMemNulTerminate(pRec); if( (pRec->flags&MEM_Str) && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ i64 value; sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); | | | 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ int realnum; sqlite3VdbeMemNulTerminate(pRec); if( (pRec->flags&MEM_Str) && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ i64 value; sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); if( !realnum && sqlite3_atoi64(pRec->z, &value) ){ sqlite3VdbeMemRelease(pRec); pRec->u.i = value; pRec->flags = MEM_Int; }else{ sqlite3VdbeMemRealify(pRec); } } |
︙ | ︙ |
Changes to src/vdbemem.c.
︙ | ︙ | |||
303 304 305 306 307 308 309 | i64 value; pMem->flags |= MEM_Str; if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) || sqlite3VdbeMemNulTerminate(pMem) ){ return 0; } assert( pMem->z ); | | | 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | i64 value; pMem->flags |= MEM_Str; if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) || sqlite3VdbeMemNulTerminate(pMem) ){ return 0; } assert( pMem->z ); sqlite3_atoi64(pMem->z, &value); return value; }else{ return 0; } } /* |
︙ | ︙ |
Changes to test/expr.test.
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 2001 September 15 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing expressions. # | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # 2001 September 15 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing expressions. # # $Id: expr.test,v 1.56 2007/06/25 17:28:02 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Create a table to work with. # execsql {CREATE TABLE test1(i1 int, i2 int, r1 real, r2 real, t1 text, t2 text)} |
︙ | ︙ | |||
132 133 134 135 136 137 138 | test_expr expr-1.101 {i1=0, i2=''} {i1=i2} 0 # Check for proper handling of 64-bit integer values. # test_expr expr-1.102 {i1=40, i2=1} {i2<<i1} 1099511627776 test_expr expr-1.103 {i1=0} {(-2147483648.0 % -1)} 0.0 | | | | 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | test_expr expr-1.101 {i1=0, i2=''} {i1=i2} 0 # Check for proper handling of 64-bit integer values. # test_expr expr-1.102 {i1=40, i2=1} {i2<<i1} 1099511627776 test_expr expr-1.103 {i1=0} {(-2147483648.0 % -1)} 0.0 test_expr expr-1.104 {i1=0} {(-9223372036854775808.0 % -1)} 0.0 test_expr expr-1.105 {i1=0} {(-9223372036854775808.0 / -1)>1} 1 test_expr expr-2.1 {r1=1.23, r2=2.34} {r1+r2} 3.57 test_expr expr-2.2 {r1=1.23, r2=2.34} {r1-r2} -1.11 test_expr expr-2.3 {r1=1.23, r2=2.34} {r1*r2} 2.8782 set tcl_precision 15 test_expr expr-2.4 {r1=1.23, r2=2.34} {r1/r2} 0.525641025641026 test_expr expr-2.5 {r1=1.23, r2=2.34} {r2/r1} 1.90243902439024 |
︙ | ︙ | |||
635 636 637 638 639 640 641 642 643 | # If we specify an integer constant that is bigger than the largest # possible integer, code the integer as a real number. # do_test expr-11.1 { execsql {SELECT typeof(9223372036854775807)} } {integer} do_test expr-11.2 { execsql {SELECT typeof(9223372036854775808)} } {real} | > > > > > > > > > | > > > | > > > > > > > > > > > > > > > | 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | # If we specify an integer constant that is bigger than the largest # possible integer, code the integer as a real number. # do_test expr-11.1 { execsql {SELECT typeof(9223372036854775807)} } {integer} do_test expr-11.2 { execsql {SELECT typeof(00000009223372036854775807)} } {integer} do_test expr-11.3 { execsql {SELECT typeof(+9223372036854775807)} } {integer} do_test expr-11.4 { execsql {SELECT typeof(+000000009223372036854775807)} } {integer} do_test expr-11.5 { execsql {SELECT typeof(9223372036854775808)} } {real} do_test expr-11.6 { execsql {SELECT typeof(00000009223372036854775808)} } {real} do_test expr-11.7 { execsql {SELECT typeof(+9223372036854775808)} } {real} do_test expr-11.8 { execsql {SELECT typeof(+0000009223372036854775808)} } {real} do_test expr-11.11 { execsql {SELECT typeof(-9223372036854775808)} } {integer} do_test expr-11.12 { execsql {SELECT typeof(-00000009223372036854775808)} } {integer} do_test expr-11.13 { execsql {SELECT typeof(-9223372036854775809)} } {real} do_test expr-11.14 { execsql {SELECT typeof(-00000009223372036854775809)} } {real} # These two statements used to leak memory (because of missing %destructor # directives in parse.y). do_test expr-12.1 { catchsql { SELECT (CASE a>4 THEN 1 ELSE 0 END) FROM test1; |
︙ | ︙ |