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.

FossilOrigin-Name: e3b2e0a078b82ac6cd3c3312e8ac0983c1375e1052f1e324476d2f8d1b227c30
This commit is contained in:
drh 2018-05-18 17:11:50 +00:00
parent bc30e2023b
commit 9e80403bfc
3 changed files with 36 additions and 19 deletions

View File

@ -1,5 +1,5 @@
C Improvements\sto\sinteger/float\scomparisons\son\sarchitectures\sthat\slack\sa\n"long\sdouble"\stype. C In\sthe\sCLI,\sdetect\sand\sreport\serrors\son\ssqlite3_close().\s\sClear\sglobal\nvariables\sprior\sto\sexit\sto\sso\sthat\svalgrind\scan\sbetter\sdetect\sresource\nleaks.
D 2018-05-18T14:24:23.139 D 2018-05-18T17:11:50.887
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F Makefile.in bfc40f350586923e0419d2ea4b559c37ec10ee4b6e210e08c14401f8e340f0da F Makefile.in bfc40f350586923e0419d2ea4b559c37ec10ee4b6e210e08c14401f8e340f0da
@ -494,7 +494,7 @@ F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
F src/resolve.c 6415381a0e9d22c0e7cba33ca4a53f81474190862f5d4838190f5eb5b0b47bc9 F src/resolve.c 6415381a0e9d22c0e7cba33ca4a53f81474190862f5d4838190f5eb5b0b47bc9
F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
F src/select.c a35d462ee7a3c0856ad7a9d9c8921fbf3d91d911a8f39ad9d61302eb43b24a71 F src/select.c a35d462ee7a3c0856ad7a9d9c8921fbf3d91d911a8f39ad9d61302eb43b24a71
F src/shell.c.in e0f006980883e238555c5ab51bd2dad9fa477c31928419f6d35774288b94853b F src/shell.c.in 51c100206f4b7f86cd9affd80b764825e0edc36ca0190c442e4ca7994611bfe2
F src/sqlite.h.in 34be2d0d18bf4726538793bdc9854cd87f689fda4b3789515134cdbd68188cf4 F src/sqlite.h.in 34be2d0d18bf4726538793bdc9854cd87f689fda4b3789515134cdbd68188cf4
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 9887b27e69c01e79c2cbe74ef73bf01af5b5703d6a7f0a4371e386d7249cb1c7 F src/sqlite3ext.h 9887b27e69c01e79c2cbe74ef73bf01af5b5703d6a7f0a4371e386d7249cb1c7
@ -1728,8 +1728,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 33dc8fad7f2b467f259eb78eb7342a760f01d54d95da7fe4cace10e558788a58 3d6625111319c0356c57aaf7b7460fd882c3f00ca668dc43c0abe3403c7c2ba3 P 5139ea62a8a6c6dc6558c337de39bcbadd26f6515742263387be03c862c78cf0
R 95f9dad3dedc0c1d42618371aa8f363f R b8765ab2a09ad6fd3afc8344422cf1a3
T +closed 3d6625111319c0356c57aaf7b7460fd882c3f00ca668dc43c0abe3403c7c2ba3
U drh U drh
Z 5a624356c837c7a872fedd9425b35399 Z d089a713f2ba352bce4662bcdd3c7d90

View File

@ -1 +1 @@
5139ea62a8a6c6dc6558c337de39bcbadd26f6515742263387be03c862c78cf0 e3b2e0a078b82ac6cd3c3312e8ac0983c1375e1052f1e324476d2f8d1b227c30

View File

@ -3656,6 +3656,17 @@ static void open_db(ShellState *p, int openFlags){
} }
} }
/*
** Attempt to close the databaes connection. Report errors.
*/
void close_db(sqlite3 *db){
int rc = sqlite3_close(db);
if( rc ){
utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
rc, sqlite3_errmsg(db));
}
}
#if HAVE_READLINE || HAVE_EDITLINE #if HAVE_READLINE || HAVE_EDITLINE
/* /*
** Readline completion callbacks ** Readline completion callbacks
@ -4236,7 +4247,7 @@ static void tryToClone(ShellState *p, const char *zNewDb){
sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
} }
sqlite3_close(newDb); close_db(newDb);
} }
/* /*
@ -5553,7 +5564,7 @@ static int arDotCommand(
} }
end_ar_command: end_ar_command:
if( cmd.db!=pState->db ){ if( cmd.db!=pState->db ){
sqlite3_close(cmd.db); close_db(cmd.db);
} }
sqlite3_free(cmd.zSrcTable); sqlite3_free(cmd.zSrcTable);
@ -5676,14 +5687,14 @@ static int do_meta_command(char *zLine, ShellState *p){
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
if( rc!=SQLITE_OK ){ if( rc!=SQLITE_OK ){
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
sqlite3_close(pDest); close_db(pDest);
return 1; return 1;
} }
open_db(p, 0); open_db(p, 0);
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
if( pBackup==0 ){ if( pBackup==0 ){
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
sqlite3_close(pDest); close_db(pDest);
return 1; return 1;
} }
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
@ -5694,7 +5705,7 @@ static int do_meta_command(char *zLine, ShellState *p){
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
rc = 1; rc = 1;
} }
sqlite3_close(pDest); close_db(pDest);
}else }else
if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
@ -6510,7 +6521,7 @@ static int do_meta_command(char *zLine, ShellState *p){
int newFlag = 0; /* True to delete file before opening */ int newFlag = 0; /* True to delete file before opening */
/* Close the existing database */ /* Close the existing database */
session_close_all(p); session_close_all(p);
sqlite3_close(p->db); close_db(p->db);
p->db = 0; p->db = 0;
p->zDbFilename = 0; p->zDbFilename = 0;
sqlite3_free(p->zFreeOnClose); sqlite3_free(p->zFreeOnClose);
@ -6690,14 +6701,14 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = sqlite3_open(zSrcFile, &pSrc); rc = sqlite3_open(zSrcFile, &pSrc);
if( rc!=SQLITE_OK ){ if( rc!=SQLITE_OK ){
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
sqlite3_close(pSrc); close_db(pSrc);
return 1; return 1;
} }
open_db(p, 0); open_db(p, 0);
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
if( pBackup==0 ){ if( pBackup==0 ){
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
sqlite3_close(pSrc); close_db(pSrc);
return 1; return 1;
} }
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
@ -6717,7 +6728,7 @@ static int do_meta_command(char *zLine, ShellState *p){
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
rc = 1; rc = 1;
} }
sqlite3_close(pSrc); close_db(pSrc);
}else }else
if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
@ -7401,7 +7412,10 @@ static int do_meta_command(char *zLine, ShellState *p){
initText(&s); initText(&s);
open_db(p, 0); open_db(p, 0);
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
if( rc ) return shellDatabaseError(p->db); if( rc ){
sqlite3_finalize(pStmt);
return shellDatabaseError(p->db);
}
if( nArg>2 && c=='i' ){ if( nArg>2 && c=='i' ){
/* It is an historical accident that the .indexes command shows an error /* It is an historical accident that the .indexes command shows an error
@ -7409,6 +7423,7 @@ static int do_meta_command(char *zLine, ShellState *p){
** command does not. */ ** command does not. */
raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
rc = 1; rc = 1;
sqlite3_finalize(pStmt);
goto meta_command_exit; goto meta_command_exit;
} }
for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
@ -8754,7 +8769,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
set_table_name(&data, 0); set_table_name(&data, 0);
if( data.db ){ if( data.db ){
session_close_all(&data); session_close_all(&data);
sqlite3_close(data.db); close_db(data.db);
} }
sqlite3_free(data.zFreeOnClose); sqlite3_free(data.zFreeOnClose);
find_home_dir(1); find_home_dir(1);
@ -8765,5 +8780,8 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
for(i=0; i<argcToFree; i++) free(argvToFree[i]); for(i=0; i<argcToFree; i++) free(argvToFree[i]);
free(argvToFree); free(argvToFree);
#endif #endif
/* Clear the global data structure so that valgrind will detect memory
** leaks */
memset(&data, 0, sizeof(data));
return rc; return rc;
} }