1 /* 2 ** 2008 March 19 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** Code for testing all sorts of SQLite interfaces. This code 13 ** implements new SQL functions used by the test scripts. 14 */ 15 #include "sqlite3.h" 16 #include "tcl.h" 17 #include <stdlib.h> 18 #include <string.h> 19 #include <assert.h> 20 21 #include "sqliteInt.h" 22 #include "vdbeInt.h" 23 24 25 /* 26 ** Allocate nByte bytes of space using sqlite3_malloc(). If the 27 ** allocation fails, call sqlite3_result_error_nomem() to notify 28 ** the database handle that malloc() has failed. 29 */ 30 static void *testContextMalloc(sqlite3_context *context, int nByte){ 31 char *z = sqlite3_malloc(nByte); 32 if( !z && nByte>0 ){ 33 sqlite3_result_error_nomem(context); 34 } 35 return z; 36 } 37 38 /* 39 ** This function generates a string of random characters. Used for 40 ** generating test data. 41 */ 42 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 43 static const unsigned char zSrc[] = 44 "abcdefghijklmnopqrstuvwxyz" 45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 46 "0123456789" 47 ".-!,:*^+=_|?/<> "; 48 int iMin, iMax, n, r, i; 49 unsigned char zBuf[1000]; 50 51 /* It used to be possible to call randstr() with any number of arguments, 52 ** but now it is registered with SQLite as requiring exactly 2. 53 */ 54 assert(argc==2); 55 56 iMin = sqlite3_value_int(argv[0]); 57 if( iMin<0 ) iMin = 0; 58 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 59 iMax = sqlite3_value_int(argv[1]); 60 if( iMax<iMin ) iMax = iMin; 61 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 62 n = iMin; 63 if( iMax>iMin ){ 64 sqlite3_randomness(sizeof(r), &r); 65 r &= 0x7fffffff; 66 n += r%(iMax + 1 - iMin); 67 } 68 assert( n<sizeof(zBuf) ); 69 sqlite3_randomness(n, zBuf); 70 for(i=0; i<n; i++){ 71 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 72 } 73 zBuf[n] = 0; 74 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 75 } 76 77 /* 78 ** The following two SQL functions are used to test returning a text 79 ** result with a destructor. Function 'test_destructor' takes one argument 80 ** and returns the same argument interpreted as TEXT. A destructor is 81 ** passed with the sqlite3_result_text() call. 82 ** 83 ** SQL function 'test_destructor_count' returns the number of outstanding 84 ** allocations made by 'test_destructor'; 85 ** 86 ** WARNING: Not threadsafe. 87 */ 88 static int test_destructor_count_var = 0; 89 static void destructor(void *p){ 90 char *zVal = (char *)p; 91 assert(zVal); 92 zVal--; 93 sqlite3_free(zVal); 94 test_destructor_count_var--; 95 } 96 static void test_destructor( 97 sqlite3_context *pCtx, 98 int nArg, 99 sqlite3_value **argv 100 ){ 101 char *zVal; 102 int len; 103 104 test_destructor_count_var++; 105 assert( nArg==1 ); 106 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 107 len = sqlite3_value_bytes(argv[0]); 108 zVal = testContextMalloc(pCtx, len+3); 109 if( !zVal ){ 110 return; 111 } 112 zVal[len+1] = 0; 113 zVal[len+2] = 0; 114 zVal++; 115 memcpy(zVal, sqlite3_value_text(argv[0]), len); 116 sqlite3_result_text(pCtx, zVal, -1, destructor); 117 } 118 #ifndef SQLITE_OMIT_UTF16 119 static void test_destructor16( 120 sqlite3_context *pCtx, 121 int nArg, 122 sqlite3_value **argv 123 ){ 124 char *zVal; 125 int len; 126 127 test_destructor_count_var++; 128 assert( nArg==1 ); 129 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 130 len = sqlite3_value_bytes16(argv[0]); 131 zVal = testContextMalloc(pCtx, len+3); 132 if( !zVal ){ 133 return; 134 } 135 zVal[len+1] = 0; 136 zVal[len+2] = 0; 137 zVal++; 138 memcpy(zVal, sqlite3_value_text16(argv[0]), len); 139 sqlite3_result_text16(pCtx, zVal, -1, destructor); 140 } 141 #endif 142 static void test_destructor_count( 143 sqlite3_context *pCtx, 144 int nArg, 145 sqlite3_value **argv 146 ){ 147 sqlite3_result_int(pCtx, test_destructor_count_var); 148 } 149 150 /* 151 ** The following aggregate function, test_agg_errmsg16(), takes zero 152 ** arguments. It returns the text value returned by the sqlite3_errmsg16() 153 ** API function. 154 */ 155 #ifndef SQLITE_OMIT_BUILTIN_TEST 156 void sqlite3BeginBenignMalloc(void); 157 void sqlite3EndBenignMalloc(void); 158 #else 159 #define sqlite3BeginBenignMalloc() 160 #define sqlite3EndBenignMalloc() 161 #endif 162 static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ 163 } 164 static void test_agg_errmsg16_final(sqlite3_context *ctx){ 165 #ifndef SQLITE_OMIT_UTF16 166 const void *z; 167 sqlite3 * db = sqlite3_context_db_handle(ctx); 168 sqlite3_aggregate_context(ctx, 2048); 169 sqlite3BeginBenignMalloc(); 170 z = sqlite3_errmsg16(db); 171 sqlite3EndBenignMalloc(); 172 sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); 173 #endif 174 } 175 176 /* 177 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 178 ** interface. 179 ** 180 ** The test_auxdata() SQL function attempts to register each of its arguments 181 ** as auxiliary data. If there are no prior registrations of aux data for 182 ** that argument (meaning the argument is not a constant or this is its first 183 ** call) then the result for that argument is 0. If there is a prior 184 ** registration, the result for that argument is 1. The overall result 185 ** is the individual argument results separated by spaces. 186 */ 187 static void free_test_auxdata(void *p) {sqlite3_free(p);} 188 static void test_auxdata( 189 sqlite3_context *pCtx, 190 int nArg, 191 sqlite3_value **argv 192 ){ 193 int i; 194 char *zRet = testContextMalloc(pCtx, nArg*2); 195 if( !zRet ) return; 196 memset(zRet, 0, nArg*2); 197 for(i=0; i<nArg; i++){ 198 char const *z = (char*)sqlite3_value_text(argv[i]); 199 if( z ){ 200 int n; 201 char *zAux = sqlite3_get_auxdata(pCtx, i); 202 if( zAux ){ 203 zRet[i*2] = '1'; 204 assert( strcmp(zAux,z)==0 ); 205 }else { 206 zRet[i*2] = '0'; 207 } 208 n = (int)strlen(z) + 1; 209 zAux = testContextMalloc(pCtx, n); 210 if( zAux ){ 211 memcpy(zAux, z, n); 212 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 213 } 214 zRet[i*2+1] = ' '; 215 } 216 } 217 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 218 } 219 220 /* 221 ** A function to test error reporting from user functions. This function 222 ** returns a copy of its first argument as the error message. If the 223 ** second argument exists, it becomes the error code. 224 */ 225 static void test_error( 226 sqlite3_context *pCtx, 227 int nArg, 228 sqlite3_value **argv 229 ){ 230 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1); 231 if( nArg==2 ){ 232 sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1])); 233 } 234 } 235 236 /* 237 ** Implementation of the counter(X) function. If X is an integer 238 ** constant, then the first invocation will return X. The second X+1. 239 ** and so forth. Can be used (for example) to provide a sequence number 240 ** in a result set. 241 */ 242 static void counterFunc( 243 sqlite3_context *pCtx, /* Function context */ 244 int nArg, /* Number of function arguments */ 245 sqlite3_value **argv /* Values for all function arguments */ 246 ){ 247 int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0); 248 if( pCounter==0 ){ 249 pCounter = sqlite3_malloc( sizeof(*pCounter) ); 250 if( pCounter==0 ){ 251 sqlite3_result_error_nomem(pCtx); 252 return; 253 } 254 *pCounter = sqlite3_value_int(argv[0]); 255 sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free); 256 }else{ 257 ++*pCounter; 258 } 259 sqlite3_result_int(pCtx, *pCounter); 260 } 261 262 263 /* 264 ** This function takes two arguments. It performance UTF-8/16 type 265 ** conversions on the first argument then returns a copy of the second 266 ** argument. 267 ** 268 ** This function is used in cases such as the following: 269 ** 270 ** SELECT test_isolation(x,x) FROM t1; 271 ** 272 ** We want to verify that the type conversions that occur on the 273 ** first argument do not invalidate the second argument. 274 */ 275 static void test_isolation( 276 sqlite3_context *pCtx, 277 int nArg, 278 sqlite3_value **argv 279 ){ 280 #ifndef SQLITE_OMIT_UTF16 281 sqlite3_value_text16(argv[0]); 282 sqlite3_value_text(argv[0]); 283 sqlite3_value_text16(argv[0]); 284 sqlite3_value_text(argv[0]); 285 #endif 286 sqlite3_result_value(pCtx, argv[1]); 287 } 288 289 /* 290 ** Invoke an SQL statement recursively. The function result is the 291 ** first column of the first row of the result set. 292 */ 293 static void test_eval( 294 sqlite3_context *pCtx, 295 int nArg, 296 sqlite3_value **argv 297 ){ 298 sqlite3_stmt *pStmt; 299 int rc; 300 sqlite3 *db = sqlite3_context_db_handle(pCtx); 301 const char *zSql; 302 303 zSql = (char*)sqlite3_value_text(argv[0]); 304 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 305 if( rc==SQLITE_OK ){ 306 rc = sqlite3_step(pStmt); 307 if( rc==SQLITE_ROW ){ 308 sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); 309 } 310 rc = sqlite3_finalize(pStmt); 311 } 312 if( rc ){ 313 char *zErr; 314 assert( pStmt==0 ); 315 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); 316 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); 317 sqlite3_result_error_code(pCtx, rc); 318 } 319 } 320 321 322 /* 323 ** convert one character from hex to binary 324 */ 325 static int testHexChar(char c){ 326 if( c>='0' && c<='9' ){ 327 return c - '0'; 328 }else if( c>='a' && c<='f' ){ 329 return c - 'a' + 10; 330 }else if( c>='A' && c<='F' ){ 331 return c - 'A' + 10; 332 } 333 return 0; 334 } 335 336 /* 337 ** Convert hex to binary. 338 */ 339 static void testHexToBin(const char *zIn, char *zOut){ 340 while( zIn[0] && zIn[1] ){ 341 *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); 342 zIn += 2; 343 } 344 } 345 346 /* 347 ** hex_to_utf16be(HEX) 348 ** 349 ** Convert the input string from HEX into binary. Then return the 350 ** result using sqlite3_result_text16le(). 351 */ 352 #ifndef SQLITE_OMIT_UTF16 353 static void testHexToUtf16be( 354 sqlite3_context *pCtx, 355 int nArg, 356 sqlite3_value **argv 357 ){ 358 int n; 359 const char *zIn; 360 char *zOut; 361 assert( nArg==1 ); 362 n = sqlite3_value_bytes(argv[0]); 363 zIn = (const char*)sqlite3_value_text(argv[0]); 364 zOut = sqlite3_malloc( n/2 ); 365 if( zOut==0 ){ 366 sqlite3_result_error_nomem(pCtx); 367 }else{ 368 testHexToBin(zIn, zOut); 369 sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); 370 } 371 } 372 #endif 373 374 /* 375 ** hex_to_utf8(HEX) 376 ** 377 ** Convert the input string from HEX into binary. Then return the 378 ** result using sqlite3_result_text16le(). 379 */ 380 static void testHexToUtf8( 381 sqlite3_context *pCtx, 382 int nArg, 383 sqlite3_value **argv 384 ){ 385 int n; 386 const char *zIn; 387 char *zOut; 388 assert( nArg==1 ); 389 n = sqlite3_value_bytes(argv[0]); 390 zIn = (const char*)sqlite3_value_text(argv[0]); 391 zOut = sqlite3_malloc( n/2 ); 392 if( zOut==0 ){ 393 sqlite3_result_error_nomem(pCtx); 394 }else{ 395 testHexToBin(zIn, zOut); 396 sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free); 397 } 398 } 399 400 /* 401 ** hex_to_utf16le(HEX) 402 ** 403 ** Convert the input string from HEX into binary. Then return the 404 ** result using sqlite3_result_text16le(). 405 */ 406 #ifndef SQLITE_OMIT_UTF16 407 static void testHexToUtf16le( 408 sqlite3_context *pCtx, 409 int nArg, 410 sqlite3_value **argv 411 ){ 412 int n; 413 const char *zIn; 414 char *zOut; 415 assert( nArg==1 ); 416 n = sqlite3_value_bytes(argv[0]); 417 zIn = (const char*)sqlite3_value_text(argv[0]); 418 zOut = sqlite3_malloc( n/2 ); 419 if( zOut==0 ){ 420 sqlite3_result_error_nomem(pCtx); 421 }else{ 422 testHexToBin(zIn, zOut); 423 sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); 424 } 425 } 426 #endif 427 428 /* 429 ** SQL function: real2hex(X) 430 ** 431 ** If argument X is a real number, then convert it into a string which is 432 ** the big-endian hexadecimal representation of the ieee754 encoding of 433 ** that number. If X is not a real number, return NULL. 434 */ 435 static void real2hex( 436 sqlite3_context *context, 437 int argc, 438 sqlite3_value **argv 439 ){ 440 union { 441 sqlite3_uint64 i; 442 double r; 443 unsigned char x[8]; 444 } v; 445 char zOut[20]; 446 int i; 447 int bigEndian; 448 v.i = 1; 449 bigEndian = v.x[0]==0; 450 v.r = sqlite3_value_double(argv[0]); 451 for(i=0; i<8; i++){ 452 if( bigEndian ){ 453 zOut[i*2] = "0123456789abcdef"[v.x[i]>>4]; 454 zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; 455 }else{ 456 zOut[14-i*2] = "0123456789abcdef"[v.x[i]>>4]; 457 zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; 458 } 459 } 460 zOut[16] = 0; 461 sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); 462 } 463 464 /* 465 ** tclcmd: test_decode(record) 466 ** 467 ** This function implements an SQL user-function that accepts a blob 468 ** containing a formatted database record as its only argument. It returns 469 ** a tcl list (type SQLITE_TEXT) containing each of the values stored 470 ** in the record. 471 */ 472 static void test_decode( 473 sqlite3_context *context, 474 int argc, 475 sqlite3_value **argv 476 ){ 477 sqlite3 *db = sqlite3_context_db_handle(context); 478 u8 *pRec; 479 u8 *pEndHdr; /* Points to one byte past record header */ 480 u8 *pHdr; /* Current point in record header */ 481 u8 *pBody; /* Current point in record data */ 482 u64 nHdr; /* Bytes in record header */ 483 Tcl_Obj *pRet; /* Return value */ 484 485 pRet = Tcl_NewObj(); 486 Tcl_IncrRefCount(pRet); 487 488 assert( argc==1 ); 489 pRec = (u8*)sqlite3_value_blob(argv[0]); 490 491 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); 492 pBody = pEndHdr = &pRec[nHdr]; 493 while( pHdr<pEndHdr ){ 494 Tcl_Obj *pVal = 0; 495 u64 iSerialType; 496 Mem mem; 497 498 memset(&mem, 0, sizeof(mem)); 499 mem.db = db; 500 mem.enc = SQLITE_UTF8; 501 pHdr += sqlite3GetVarint(pHdr, &iSerialType); 502 pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); 503 504 sqlite3VdbeMemStoreType(&mem); 505 switch( sqlite3_value_type(&mem) ){ 506 case SQLITE_TEXT: 507 pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1); 508 break; 509 510 case SQLITE_BLOB: { 511 char hexdigit[] = { 512 '0', '1', '2', '3', '4', '5', '6', '7', 513 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 514 }; 515 int n = sqlite3_value_bytes(&mem); 516 u8 *z = (u8*)sqlite3_value_blob(&mem); 517 int i; 518 pVal = Tcl_NewStringObj("x'", -1); 519 for(i=0; i<n; i++){ 520 char hex[3]; 521 hex[0] = hexdigit[((z[i] >> 4) & 0x0F)]; 522 hex[1] = hexdigit[(z[i] & 0x0F)]; 523 hex[2] = '\0'; 524 Tcl_AppendStringsToObj(pVal, hex, 0); 525 } 526 Tcl_AppendStringsToObj(pVal, "'", 0); 527 break; 528 } 529 530 case SQLITE_FLOAT: 531 pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem)); 532 break; 533 534 case SQLITE_INTEGER: 535 pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem)); 536 break; 537 538 case SQLITE_NULL: 539 pVal = Tcl_NewStringObj("NULL", -1); 540 break; 541 542 default: 543 assert( 0 ); 544 } 545 546 Tcl_ListObjAppendElement(0, pRet, pVal); 547 548 if( mem.zMalloc ){ 549 sqlite3DbFree(db, mem.zMalloc); 550 } 551 } 552 553 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); 554 Tcl_DecrRefCount(pRet); 555 } 556 557 558 static int registerTestFunctions(sqlite3 *db){ 559 static const struct { 560 char *zName; 561 signed char nArg; 562 unsigned char eTextRep; /* 1: UTF-16. 0: UTF-8 */ 563 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 564 } aFuncs[] = { 565 { "randstr", 2, SQLITE_UTF8, randStr }, 566 { "test_destructor", 1, SQLITE_UTF8, test_destructor}, 567 #ifndef SQLITE_OMIT_UTF16 568 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, 569 { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, 570 { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, 571 #endif 572 { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, 573 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, 574 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, 575 { "test_error", 1, SQLITE_UTF8, test_error}, 576 { "test_error", 2, SQLITE_UTF8, test_error}, 577 { "test_eval", 1, SQLITE_UTF8, test_eval}, 578 { "test_isolation", 2, SQLITE_UTF8, test_isolation}, 579 { "test_counter", 1, SQLITE_UTF8, counterFunc}, 580 { "real2hex", 1, SQLITE_UTF8, real2hex}, 581 { "test_decode", 1, SQLITE_UTF8, test_decode}, 582 }; 583 int i; 584 585 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 586 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 587 aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); 588 } 589 590 sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, 591 test_agg_errmsg16_step, test_agg_errmsg16_final); 592 593 return SQLITE_OK; 594 } 595 596 /* 597 ** TCLCMD: autoinstall_test_functions 598 ** 599 ** Invoke this TCL command to use sqlite3_auto_extension() to cause 600 ** the standard set of test functions to be loaded into each new 601 ** database connection. 602 */ 603 static int autoinstall_test_funcs( 604 void * clientData, 605 Tcl_Interp *interp, 606 int objc, 607 Tcl_Obj *CONST objv[] 608 ){ 609 extern int Md5_Register(sqlite3*); 610 int rc = sqlite3_auto_extension((void*)registerTestFunctions); 611 if( rc==SQLITE_OK ){ 612 rc = sqlite3_auto_extension((void*)Md5_Register); 613 } 614 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 615 return TCL_OK; 616 } 617 618 /* 619 ** A bogus step function and finalizer function. 620 */ 621 static void tStep(sqlite3_context *a, int b, sqlite3_value **c){} 622 static void tFinal(sqlite3_context *a){} 623 624 625 /* 626 ** tclcmd: abuse_create_function 627 ** 628 ** Make various calls to sqlite3_create_function that do not have valid 629 ** parameters. Verify that the error condition is detected and reported. 630 */ 631 static int abuse_create_function( 632 void * clientData, 633 Tcl_Interp *interp, 634 int objc, 635 Tcl_Obj *CONST objv[] 636 ){ 637 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); 638 sqlite3 *db; 639 int rc; 640 int mxArg; 641 642 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; 643 644 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal); 645 if( rc!=SQLITE_MISUSE ) goto abuse_err; 646 647 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0); 648 if( rc!=SQLITE_MISUSE ) goto abuse_err; 649 650 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal); 651 if( rc!=SQLITE_MISUSE) goto abuse_err; 652 653 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal); 654 if( rc!=SQLITE_MISUSE ) goto abuse_err; 655 656 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0); 657 if( rc!=SQLITE_MISUSE ) goto abuse_err; 658 659 rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); 660 if( rc!=SQLITE_MISUSE ) goto abuse_err; 661 662 rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0); 663 if( rc!=SQLITE_MISUSE ) goto abuse_err; 664 665 rc = sqlite3_create_function(db, "funcxx" 666 "_123456789_123456789_123456789_123456789_123456789" 667 "_123456789_123456789_123456789_123456789_123456789" 668 "_123456789_123456789_123456789_123456789_123456789" 669 "_123456789_123456789_123456789_123456789_123456789" 670 "_123456789_123456789_123456789_123456789_123456789", 671 1, SQLITE_UTF8, 0, tStep, 0, 0); 672 if( rc!=SQLITE_MISUSE ) goto abuse_err; 673 674 /* This last function registration should actually work. Generate 675 ** a no-op function (that always returns NULL) and which has the 676 ** maximum-length function name and the maximum number of parameters. 677 */ 678 sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000); 679 mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); 680 rc = sqlite3_create_function(db, "nullx" 681 "_123456789_123456789_123456789_123456789_123456789" 682 "_123456789_123456789_123456789_123456789_123456789" 683 "_123456789_123456789_123456789_123456789_123456789" 684 "_123456789_123456789_123456789_123456789_123456789" 685 "_123456789_123456789_123456789_123456789_123456789", 686 mxArg, SQLITE_UTF8, 0, tStep, 0, 0); 687 if( rc!=SQLITE_OK ) goto abuse_err; 688 689 return TCL_OK; 690 691 abuse_err: 692 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", 693 (char*)0); 694 return TCL_ERROR; 695 } 696 697 /* 698 ** Register commands with the TCL interpreter. 699 */ 700 int Sqlitetest_func_Init(Tcl_Interp *interp){ 701 static struct { 702 char *zName; 703 Tcl_ObjCmdProc *xProc; 704 } aObjCmd[] = { 705 { "autoinstall_test_functions", autoinstall_test_funcs }, 706 { "abuse_create_function", abuse_create_function }, 707 }; 708 int i; 709 extern int Md5_Register(sqlite3*); 710 711 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ 712 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); 713 } 714 sqlite3_initialize(); 715 sqlite3_auto_extension((void*)registerTestFunctions); 716 sqlite3_auto_extension((void*)Md5_Register); 717 return TCL_OK; 718 } 719