/ Check-in [a350040a]
Login

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

Overview
Comment:Merge enhancements from trunk, especially the CLI fixes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | aux-data-in-rtree
Files: files | file ages | folders
SHA3-256: a350040a3bb962823f92908fb31cade52baf13eab90ef608ca3a8349e4c28c9d
User & Date: drh 2018-05-18 17:17:36
Context
2018-05-18
17:58
Add support for auxiliary columns to the rtree extension. check-in: c6071ac9 user: drh tags: trunk
17:17
Merge enhancements from trunk, especially the CLI fixes. Closed-Leaf check-in: a350040a user: drh tags: aux-data-in-rtree
17:11
In the CLI, detect and report errors on sqlite3_close(). Clear global variables prior to exit to so that valgrind can better detect resource leaks. check-in: e3b2e0a0 user: drh tags: trunk
16:53
Avoid unnecessary sqlite3_finalize() operations. check-in: 16f71032 user: drh tags: aux-data-in-rtree
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/shell.c.in.

  3543   3543   */
  3544   3544   int deduceDatabaseType(const char *zName, int dfltZip){
  3545   3545     FILE *f = fopen(zName, "rb");
  3546   3546     size_t n;
  3547   3547     int rc = SHELL_OPEN_UNSPEC;
  3548   3548     char zBuf[100];
  3549   3549     if( f==0 ){
  3550         -    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
         3550  +    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
         3551  +       return SHELL_OPEN_ZIPFILE;
         3552  +    }else{
  3551   3553       return SHELL_OPEN_NORMAL;
  3552   3554     }
         3555  +  }
  3553   3556     fseek(f, -25, SEEK_END);
  3554   3557     n = fread(zBuf, 25, 1, f);
  3555   3558     if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
  3556   3559       rc = SHELL_OPEN_APPENDVFS;
  3557   3560     }else{
  3558   3561       fseek(f, -22, SEEK_END);
  3559   3562       n = fread(zBuf, 22, 1, f);
................................................................................
  3564   3567         rc = SHELL_OPEN_ZIPFILE;
  3565   3568       }
  3566   3569     }
  3567   3570     fclose(f);
  3568   3571     return rc;  
  3569   3572   }
  3570   3573   
         3574  +/* Flags for open_db().
         3575  +**
         3576  +** The default behavior of open_db() is to exit(1) if the database fails to
         3577  +** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
         3578  +** but still returns without calling exit.
         3579  +**
         3580  +** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
         3581  +** ZIP archive if the file does not exist or is empty and its name matches
         3582  +** the *.zip pattern.
         3583  +*/
         3584  +#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
         3585  +#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
         3586  +
  3571   3587   /*
  3572   3588   ** Make sure the database is open.  If it is not, then open it.  If
  3573   3589   ** the database fails to open, print an error message and exit.
  3574   3590   */
  3575         -static void open_db(ShellState *p, int keepAlive){
         3591  +static void open_db(ShellState *p, int openFlags){
  3576   3592     if( p->db==0 ){
  3577   3593       if( p->openMode==SHELL_OPEN_UNSPEC ){
  3578   3594         if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
  3579   3595           p->openMode = SHELL_OPEN_NORMAL;
  3580         -      }else if( access(p->zDbFilename,0)==0 ){
  3581         -        p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
         3596  +      }else{
         3597  +        p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 
         3598  +                             (openFlags & OPEN_DB_ZIPFILE)!=0);
  3582   3599         }
  3583   3600       }
  3584   3601       switch( p->openMode ){
  3585   3602         case SHELL_OPEN_APPENDVFS: {
  3586   3603           sqlite3_open_v2(p->zDbFilename, &p->db, 
  3587   3604              SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
  3588   3605           break;
................................................................................
  3601   3618           break;
  3602   3619         }
  3603   3620       }
  3604   3621       globalDb = p->db;
  3605   3622       if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
  3606   3623         utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
  3607   3624             p->zDbFilename, sqlite3_errmsg(p->db));
  3608         -      if( keepAlive ) return;
         3625  +      if( openFlags & OPEN_DB_KEEPALIVE ) return;
  3609   3626         exit(1);
  3610   3627       }
  3611   3628   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  3612   3629       sqlite3_enable_load_extension(p->db, 1);
  3613   3630   #endif
  3614   3631       sqlite3_fileio_init(p->db, 0, 0);
  3615   3632       sqlite3_shathree_init(p->db, 0, 0);
................................................................................
  3634   3651         char *zSql = sqlite3_mprintf(
  3635   3652            "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
  3636   3653         sqlite3_exec(p->db, zSql, 0, 0, 0);
  3637   3654         sqlite3_free(zSql);
  3638   3655       }
  3639   3656     }
  3640   3657   }
         3658  +
         3659  +/*
         3660  +** Attempt to close the databaes connection.  Report errors.
         3661  +*/
         3662  +void close_db(sqlite3 *db){
         3663  +  int rc = sqlite3_close(db);
         3664  +  if( rc ){
         3665  +    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
         3666  +        rc, sqlite3_errmsg(db));
         3667  +  } 
         3668  +}
  3641   3669   
  3642   3670   #if HAVE_READLINE || HAVE_EDITLINE
  3643   3671   /*
  3644   3672   ** Readline completion callbacks
  3645   3673   */
  3646   3674   static char *readline_completion_generator(const char *text, int state){
  3647   3675     static sqlite3_stmt *pStmt = 0;
................................................................................
  4215   4243       sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
  4216   4244       sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
  4217   4245       tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
  4218   4246       tryToCloneSchema(p, newDb, "type!='table'", 0);
  4219   4247       sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
  4220   4248       sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
  4221   4249     }
  4222         -  sqlite3_close(newDb);
         4250  +  close_db(newDb);
  4223   4251   }
  4224   4252   
  4225   4253   /*
  4226   4254   ** Change the output file back to stdout.
  4227   4255   **
  4228   4256   ** If the p->doXdgOpen flag is set, that means the output was being
  4229   4257   ** redirected to a temporary file named by p->zTempFile.  In that case,
................................................................................
  4870   4898   typedef struct ArCommand ArCommand;
  4871   4899   struct ArCommand {
  4872   4900     u8 eCmd;                        /* An AR_CMD_* value */
  4873   4901     u8 bVerbose;                    /* True if --verbose */
  4874   4902     u8 bZip;                        /* True if the archive is a ZIP */
  4875   4903     u8 bDryRun;                     /* True if --dry-run */
  4876   4904     u8 bAppend;                     /* True if --append */
         4905  +  u8 fromCmdLine;                 /* Run from -A instead of .archive */
  4877   4906     int nArg;                       /* Number of command arguments */
  4878   4907     char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
  4879   4908     const char *zFile;              /* --file argument, or NULL */
  4880   4909     const char *zDir;               /* --directory argument, or NULL */
  4881   4910     char **azArg;                   /* Array of command arguments */
  4882   4911     ShellState *p;                  /* Shell state */
  4883   4912     sqlite3 *db;                    /* Database containing the archive */
................................................................................
  4916   4945     return SQLITE_ERROR;
  4917   4946   }
  4918   4947   
  4919   4948   /*
  4920   4949   ** Print an error message for the .ar command to stderr and return 
  4921   4950   ** SQLITE_ERROR.
  4922   4951   */
  4923         -static int arErrorMsg(const char *zFmt, ...){
         4952  +static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
  4924   4953     va_list ap;
  4925   4954     char *z;
  4926   4955     va_start(ap, zFmt);
  4927   4956     z = sqlite3_vmprintf(zFmt, ap);
  4928   4957     va_end(ap);
  4929         -  raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z);
         4958  +  utf8_printf(stderr, "Error: %s\n", z);
         4959  +  if( pAr->fromCmdLine ){
         4960  +    utf8_printf(stderr, "Use \"-A\" for more help\n");
         4961  +  }else{
         4962  +    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
         4963  +  }
  4930   4964     sqlite3_free(z);
  4931   4965     return SQLITE_ERROR;
  4932   4966   }
  4933   4967   
  4934   4968   /*
  4935   4969   ** Values for ArCommand.eCmd.
  4936   4970   */
................................................................................
  4953   4987     switch( eSwitch ){
  4954   4988       case AR_CMD_CREATE:
  4955   4989       case AR_CMD_EXTRACT:
  4956   4990       case AR_CMD_LIST:
  4957   4991       case AR_CMD_UPDATE:
  4958   4992       case AR_CMD_HELP:
  4959   4993         if( pAr->eCmd ){
  4960         -        return arErrorMsg("multiple command options");
         4994  +        return arErrorMsg(pAr, "multiple command options");
  4961   4995         }
  4962   4996         pAr->eCmd = eSwitch;
  4963   4997         break;
  4964   4998   
  4965   4999       case AR_SWITCH_DRYRUN:
  4966   5000         pAr->bDryRun = 1;
  4967   5001         break;
................................................................................
  5013   5047     int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
  5014   5048     struct ArSwitch *pEnd = &aSwitch[nSwitch];
  5015   5049   
  5016   5050     if( nArg<=1 ){
  5017   5051       return arUsage(stderr);
  5018   5052     }else{
  5019   5053       char *z = azArg[1];
  5020         -    memset(pAr, 0, sizeof(ArCommand));
  5021         -
  5022   5054       if( z[0]!='-' ){
  5023   5055         /* Traditional style [tar] invocation */
  5024   5056         int i;
  5025   5057         int iArg = 2;
  5026   5058         for(i=0; z[i]; i++){
  5027   5059           const char *zArg = 0;
  5028   5060           struct ArSwitch *pOpt;
  5029   5061           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
  5030   5062             if( z[i]==pOpt->cShort ) break;
  5031   5063           }
  5032   5064           if( pOpt==pEnd ){
  5033         -          return arErrorMsg("unrecognized option: %c", z[i]);
         5065  +          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
  5034   5066           }
  5035   5067           if( pOpt->bArg ){
  5036   5068             if( iArg>=nArg ){
  5037         -            return arErrorMsg("option requires an argument: %c",z[i]);
         5069  +            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
  5038   5070             }
  5039   5071             zArg = azArg[iArg++];
  5040   5072           }
  5041   5073           if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
  5042   5074         }
  5043   5075         pAr->nArg = nArg-iArg;
  5044   5076         if( pAr->nArg>0 ){
................................................................................
  5064   5096             for(i=1; i<n; i++){
  5065   5097               const char *zArg = 0;
  5066   5098               struct ArSwitch *pOpt;
  5067   5099               for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
  5068   5100                 if( z[i]==pOpt->cShort ) break;
  5069   5101               }
  5070   5102               if( pOpt==pEnd ){
  5071         -              return arErrorMsg("unrecognized option: %c\n", z[i]);
         5103  +              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
  5072   5104               }
  5073   5105               if( pOpt->bArg ){
  5074   5106                 if( i<(n-1) ){
  5075   5107                   zArg = &z[i+1];
  5076   5108                   i = n;
  5077   5109                 }else{
  5078   5110                   if( iArg>=(nArg-1) ){
  5079         -                  return arErrorMsg("option requires an argument: %c\n",z[i]);
         5111  +                  return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
  5080   5112                   }
  5081   5113                   zArg = azArg[++iArg];
  5082   5114                 }
  5083   5115               }
  5084   5116               if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
  5085   5117             }
  5086   5118           }else if( z[2]=='\0' ){
................................................................................
  5094   5126             const char *zArg = 0;             /* Argument for option, if any */
  5095   5127             struct ArSwitch *pMatch = 0;      /* Matching option */
  5096   5128             struct ArSwitch *pOpt;            /* Iterator */
  5097   5129             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
  5098   5130               const char *zLong = pOpt->zLong;
  5099   5131               if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
  5100   5132                 if( pMatch ){
  5101         -                return arErrorMsg("ambiguous option: %s",z);
         5133  +                return arErrorMsg(pAr, "ambiguous option: %s",z);
  5102   5134                 }else{
  5103   5135                   pMatch = pOpt;
  5104   5136                 }
  5105   5137               }
  5106   5138             }
  5107   5139   
  5108   5140             if( pMatch==0 ){
  5109         -            return arErrorMsg("unrecognized option: %s", z);
         5141  +            return arErrorMsg(pAr, "unrecognized option: %s", z);
  5110   5142             }
  5111   5143             if( pMatch->bArg ){
  5112   5144               if( iArg>=(nArg-1) ){
  5113         -              return arErrorMsg("option requires an argument: %s", z);
         5145  +              return arErrorMsg(pAr, "option requires an argument: %s", z);
  5114   5146               }
  5115   5147               zArg = azArg[++iArg];
  5116   5148             }
  5117   5149             if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
  5118   5150           }
  5119   5151         }
  5120   5152       }
................................................................................
  5235   5267           );
  5236   5268         }else{
  5237   5269           utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
  5238   5270         }
  5239   5271       }
  5240   5272     }
  5241   5273     shellFinalize(&rc, pSql);
         5274  +  sqlite3_free(zWhere);
  5242   5275     return rc;
  5243   5276   }
  5244   5277   
  5245   5278   
  5246   5279   /*
  5247   5280   ** Implementation of .ar "eXtract" command. 
  5248   5281   */
................................................................................
  5437   5470   }
  5438   5471   
  5439   5472   /*
  5440   5473   ** Implementation of ".ar" dot command.
  5441   5474   */
  5442   5475   static int arDotCommand(
  5443   5476     ShellState *pState,             /* Current shell tool state */
         5477  +  int fromCmdLine,                /* True if -A command-line option, not .ar cmd */
  5444   5478     char **azArg,                   /* Array of arguments passed to dot command */
  5445   5479     int nArg                        /* Number of entries in azArg[] */
  5446   5480   ){
  5447   5481     ArCommand cmd;
  5448   5482     int rc;
  5449   5483     memset(&cmd, 0, sizeof(cmd));
         5484  +  cmd.fromCmdLine = fromCmdLine;
  5450   5485     rc = arParseCommand(azArg, nArg, &cmd);
  5451   5486     if( rc==SQLITE_OK ){
  5452   5487       int eDbType = SHELL_OPEN_UNSPEC;
  5453   5488       cmd.p = pState;
  5454   5489       cmd.db = pState->db;
  5455   5490       if( cmd.zFile ){
  5456   5491         eDbType = deduceDatabaseType(cmd.zFile, 1);
................................................................................
  5489   5524         }
  5490   5525         sqlite3_fileio_init(cmd.db, 0, 0);
  5491   5526         sqlite3_sqlar_init(cmd.db, 0, 0);
  5492   5527         sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
  5493   5528                                 shellPutsFunc, 0, 0);
  5494   5529   
  5495   5530       }
  5496         -    if( cmd.zSrcTable==0 && cmd.bZip==0 ){
         5531  +    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
  5497   5532         if( cmd.eCmd!=AR_CMD_CREATE
  5498   5533          && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
  5499   5534         ){
  5500   5535           utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
  5501   5536           rc = SQLITE_ERROR;
  5502   5537           goto end_ar_command;
  5503   5538         }
................................................................................
  5525   5560           assert( cmd.eCmd==AR_CMD_UPDATE );
  5526   5561           rc = arCreateOrUpdateCommand(&cmd, 1);
  5527   5562           break;
  5528   5563       }
  5529   5564     }
  5530   5565   end_ar_command:
  5531   5566     if( cmd.db!=pState->db ){
  5532         -    sqlite3_close(cmd.db);
         5567  +    close_db(cmd.db);
  5533   5568     }
  5534   5569     sqlite3_free(cmd.zSrcTable);
  5535   5570   
  5536   5571     return rc;
  5537   5572   }
  5538   5573   /* End of the ".archive" or ".ar" command logic
  5539   5574   **********************************************************************************/
................................................................................
  5605   5640       }
  5606   5641     }else
  5607   5642   #endif
  5608   5643   
  5609   5644   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
  5610   5645     if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
  5611   5646       open_db(p, 0);
  5612         -    rc = arDotCommand(p, azArg, nArg);
         5647  +    rc = arDotCommand(p, 0, azArg, nArg);
  5613   5648     }else
  5614   5649   #endif
  5615   5650   
  5616   5651     if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
  5617   5652      || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
  5618   5653     ){
  5619   5654       const char *zDestFile = 0;
................................................................................
  5648   5683         return 1;
  5649   5684       }
  5650   5685       if( zDb==0 ) zDb = "main";
  5651   5686       rc = sqlite3_open_v2(zDestFile, &pDest, 
  5652   5687                     SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
  5653   5688       if( rc!=SQLITE_OK ){
  5654   5689         utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
  5655         -      sqlite3_close(pDest);
         5690  +      close_db(pDest);
  5656   5691         return 1;
  5657   5692       }
  5658   5693       open_db(p, 0);
  5659   5694       pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
  5660   5695       if( pBackup==0 ){
  5661   5696         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
  5662         -      sqlite3_close(pDest);
         5697  +      close_db(pDest);
  5663   5698         return 1;
  5664   5699       }
  5665   5700       while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
  5666   5701       sqlite3_backup_finish(pBackup);
  5667   5702       if( rc==SQLITE_DONE ){
  5668   5703         rc = 0;
  5669   5704       }else{
  5670   5705         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
  5671   5706         rc = 1;
  5672   5707       }
  5673         -    sqlite3_close(pDest);
         5708  +    close_db(pDest);
  5674   5709     }else
  5675   5710   
  5676   5711     if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
  5677   5712       if( nArg==2 ){
  5678   5713         bail_on_error = booleanValue(azArg[1]);
  5679   5714       }else{
  5680   5715         raw_printf(stderr, "Usage: .bail on|off\n");
................................................................................
  6482   6517   
  6483   6518     if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
  6484   6519       char *zNewFilename;  /* Name of the database file to open */
  6485   6520       int iName = 1;       /* Index in azArg[] of the filename */
  6486   6521       int newFlag = 0;     /* True to delete file before opening */
  6487   6522       /* Close the existing database */
  6488   6523       session_close_all(p);
  6489         -    sqlite3_close(p->db);
         6524  +    close_db(p->db);
  6490   6525       p->db = 0;
  6491   6526       p->zDbFilename = 0;
  6492   6527       sqlite3_free(p->zFreeOnClose);
  6493   6528       p->zFreeOnClose = 0;
  6494   6529       p->openMode = SHELL_OPEN_UNSPEC;
  6495   6530       /* Check for command-line arguments */
  6496   6531       for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
................................................................................
  6512   6547         }
  6513   6548       }
  6514   6549       /* If a filename is specified, try to open it first */
  6515   6550       zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
  6516   6551       if( zNewFilename ){
  6517   6552         if( newFlag ) shellDeleteFile(zNewFilename);
  6518   6553         p->zDbFilename = zNewFilename;
  6519         -      open_db(p, 1);
         6554  +      open_db(p, OPEN_DB_KEEPALIVE);
  6520   6555         if( p->db==0 ){
  6521   6556           utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
  6522   6557           sqlite3_free(zNewFilename);
  6523   6558         }else{
  6524   6559           p->zFreeOnClose = zNewFilename;
  6525   6560         }
  6526   6561       }
................................................................................
  6662   6697         raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
  6663   6698         rc = 1;
  6664   6699         goto meta_command_exit;
  6665   6700       }
  6666   6701       rc = sqlite3_open(zSrcFile, &pSrc);
  6667   6702       if( rc!=SQLITE_OK ){
  6668   6703         utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
  6669         -      sqlite3_close(pSrc);
         6704  +      close_db(pSrc);
  6670   6705         return 1;
  6671   6706       }
  6672   6707       open_db(p, 0);
  6673   6708       pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
  6674   6709       if( pBackup==0 ){
  6675   6710         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
  6676         -      sqlite3_close(pSrc);
         6711  +      close_db(pSrc);
  6677   6712         return 1;
  6678   6713       }
  6679   6714       while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
  6680   6715             || rc==SQLITE_BUSY  ){
  6681   6716         if( rc==SQLITE_BUSY ){
  6682   6717           if( nTimeout++ >= 3 ) break;
  6683   6718           sqlite3_sleep(100);
................................................................................
  6689   6724       }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
  6690   6725         raw_printf(stderr, "Error: source database is busy\n");
  6691   6726         rc = 1;
  6692   6727       }else{
  6693   6728         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
  6694   6729         rc = 1;
  6695   6730       }
  6696         -    sqlite3_close(pSrc);
         6731  +    close_db(pSrc);
  6697   6732     }else
  6698   6733   
  6699   6734     if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
  6700   6735       if( nArg==2 ){
  6701   6736         p->scanstatsOn = (u8)booleanValue(azArg[1]);
  6702   6737   #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
  6703   6738         raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
................................................................................
  7373   7408       char **azResult;
  7374   7409       int nRow, nAlloc;
  7375   7410       int ii;
  7376   7411       ShellText s;
  7377   7412       initText(&s);
  7378   7413       open_db(p, 0);
  7379   7414       rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
  7380         -    if( rc ) return shellDatabaseError(p->db);
         7415  +    if( rc ){
         7416  +      sqlite3_finalize(pStmt);
         7417  +      return shellDatabaseError(p->db);
         7418  +    }
  7381   7419   
  7382   7420       if( nArg>2 && c=='i' ){
  7383   7421         /* It is an historical accident that the .indexes command shows an error
  7384   7422         ** when called with the wrong number of arguments whereas the .tables
  7385   7423         ** command does not. */
  7386   7424         raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
  7387   7425         rc = 1;
         7426  +      sqlite3_finalize(pStmt);
  7388   7427         goto meta_command_exit;
  7389   7428       }
  7390   7429       for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
  7391   7430         const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
  7392   7431         if( zDbName==0 ) continue;
  7393   7432         if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
  7394   7433         if( sqlite3_stricmp(zDbName, "main")==0 ){
................................................................................
  8281   8320     int i;
  8282   8321     int rc = 0;
  8283   8322     int warnInmemoryDb = 0;
  8284   8323     int readStdin = 1;
  8285   8324     int nCmd = 0;
  8286   8325     char **azCmd = 0;
  8287   8326     const char *zVfs = 0;           /* Value of -vfs command-line option */
         8327  +#if !SQLITE_SHELL_IS_UTF8
         8328  +  char **argvToFree = 0;
         8329  +  int argcToFree = 0;
         8330  +#endif
  8288   8331   
  8289   8332     setBinaryMode(stdin, 0);
  8290   8333     setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
  8291   8334     stdin_is_interactive = isatty(0);
  8292   8335     stdout_is_console = isatty(1);
  8293   8336   
  8294   8337   #if USE_SYSTEM_SQLITE+0!=1
................................................................................
  8304   8347     ** The SQLite memory allocator subsystem has to be enabled in order to
  8305   8348     ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
  8306   8349     ** subsequent sqlite3_config() calls will work.  So copy all results into
  8307   8350     ** memory that does not come from the SQLite memory allocator.
  8308   8351     */
  8309   8352   #if !SQLITE_SHELL_IS_UTF8
  8310   8353     sqlite3_initialize();
  8311         -  argv = malloc(sizeof(argv[0])*argc);
         8354  +  argvToFree = malloc(sizeof(argv[0])*argc*2);
         8355  +  argcToFree = argc;
         8356  +  argv = argvToFree + argc;
  8312   8357     if( argv==0 ) shell_out_of_memory();
  8313   8358     for(i=0; i<argc; i++){
  8314   8359       char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
  8315   8360       int n;
  8316   8361       if( z==0 ) shell_out_of_memory();
  8317   8362       n = (int)strlen(z);
  8318   8363       argv[i] = malloc( n+1 );
  8319   8364       if( argv[i]==0 ) shell_out_of_memory();
  8320   8365       memcpy(argv[i], z, n+1);
         8366  +    argvToFree[i] = argv[i];
  8321   8367       sqlite3_free(z);
  8322   8368     }
  8323   8369     sqlite3_shutdown();
  8324   8370   #endif
  8325   8371   
  8326   8372     assert( argc>=1 && argv && argv[0] );
  8327   8373     Argv0 = argv[0];
................................................................................
  8635   8681   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
  8636   8682       }else if( strncmp(z, "-A", 2)==0 ){
  8637   8683         if( nCmd>0 ){
  8638   8684           utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
  8639   8685                               " with \"%s\"\n", z);
  8640   8686           return 1;
  8641   8687         }
  8642         -      open_db(&data, 0);
         8688  +      open_db(&data, OPEN_DB_ZIPFILE);
  8643   8689         if( z[2] ){
  8644   8690           argv[i] = &z[2];
  8645         -        arDotCommand(&data, argv+(i-1), argc-(i-1));
         8691  +        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
  8646   8692         }else{
  8647         -        arDotCommand(&data, argv+i, argc-i);
         8693  +        arDotCommand(&data, 1, argv+i, argc-i);
  8648   8694         }
  8649   8695         readStdin = 0;
  8650   8696         break;
  8651   8697   #endif
  8652   8698       }else{
  8653   8699         utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
  8654   8700         raw_printf(stderr,"Use -help for a list of options.\n");
................................................................................
  8719   8765       }else{
  8720   8766         rc = process_input(&data, stdin);
  8721   8767       }
  8722   8768     }
  8723   8769     set_table_name(&data, 0);
  8724   8770     if( data.db ){
  8725   8771       session_close_all(&data);
  8726         -    sqlite3_close(data.db);
         8772  +    close_db(data.db);
  8727   8773     }
  8728   8774     sqlite3_free(data.zFreeOnClose);
  8729   8775     find_home_dir(1);
  8730   8776     output_reset(&data);
  8731   8777     data.doXdgOpen = 0;
  8732   8778     clearTempFile(&data);
  8733   8779   #if !SQLITE_SHELL_IS_UTF8
  8734         -  for(i=0; i<argc; i++) free(argv[i]);
  8735         -  free(argv);
         8780  +  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
         8781  +  free(argvToFree);
  8736   8782   #endif
         8783  +  /* Clear the global data structure so that valgrind will detect memory
         8784  +  ** leaks */
         8785  +  memset(&data, 0, sizeof(data));
  8737   8786     return rc;
  8738   8787   }

Changes to src/vdbeaux.c.

  3913   3913       if( x<r ) return -1;
  3914   3914       if( x>r ) return +1;
  3915   3915       return 0;
  3916   3916     }else{
  3917   3917       i64 y;
  3918   3918       double s;
  3919   3919       if( r<-9223372036854775808.0 ) return +1;
  3920         -    if( r>9223372036854775807.0 ) return -1;
         3920  +    if( r>=9223372036854775808.0 ) return -1;
  3921   3921       y = (i64)r;
  3922   3922       if( i<y ) return -1;
  3923         -    if( i>y ){
  3924         -      if( y==SMALLEST_INT64 && r>0.0 ) return -1;
  3925         -      return +1;
  3926         -    }
         3923  +    if( i>y ) return +1;
  3927   3924       s = (double)i;
  3928   3925       if( s<r ) return -1;
  3929   3926       if( s>r ) return +1;
  3930   3927       return 0;
  3931   3928     }
  3932   3929   }
  3933   3930