diff options
author | zork@google.com <zork@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 22:19:20 +0000 |
---|---|---|
committer | zork@google.com <zork@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 22:19:20 +0000 |
commit | ebacb45701985a30d77647c580a8c6d36e080a0e (patch) | |
tree | bf41a9fe7a0d448f48bd2551bcd0f98d78b7b31a | |
parent | 3f30e0291a13d314b299abc41e3f77651a78b4a8 (diff) | |
download | chromium_src-ebacb45701985a30d77647c580a8c6d36e080a0e.zip chromium_src-ebacb45701985a30d77647c580a8c6d36e080a0e.tar.gz chromium_src-ebacb45701985a30d77647c580a8c6d36e080a0e.tar.bz2 |
Handle errors in DoesTableExist() gracefully instead of crashing.
BUG=30403
TEST=None
Review URL: http://codereview.chromium.org/500040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34760 0039d316-1c4b-4281-b951-d872f2087c98
-rwxr-xr-x | chrome/browser/sync/syncable/directory_backing_store.cc | 8 | ||||
-rw-r--r-- | chrome/browser/sync/util/query_helpers.cc | 15 | ||||
-rw-r--r-- | chrome/browser/sync/util/query_helpers.h | 8 |
3 files changed, 25 insertions, 6 deletions
diff --git a/chrome/browser/sync/syncable/directory_backing_store.cc b/chrome/browser/sync/syncable/directory_backing_store.cc index 7093043..ac85e56 100755 --- a/chrome/browser/sync/syncable/directory_backing_store.cc +++ b/chrome/browser/sync/syncable/directory_backing_store.cc @@ -306,7 +306,8 @@ DirOpenResult DirectoryBackingStore::InitializeTables() { } int version_on_disk = 0; - if (DoesTableExist(load_dbhandle_, "share_version")) { + bool exists = false; + if (DoesTableExist(load_dbhandle_, "share_version", &exists) && exists) { ScopedStatement version_query( PrepareQuery(load_dbhandle_, "SELECT data from share_version")); int query_result = sqlite3_step(version_query.get()); @@ -331,7 +332,10 @@ DirOpenResult DirectoryBackingStore::InitializeTables() { { ScopedStatement statement(PrepareQuery(load_dbhandle_, "SELECT db_create_version, db_create_time FROM share_info")); - CHECK(SQLITE_ROW == sqlite3_step(statement.get())); + if (SQLITE_ROW != sqlite3_step(statement.get())) { + ExecOrDie(load_dbhandle_, "ROLLBACK TRANSACTION"); + return FAILED_DISK_FULL; + } string db_create_version; int db_create_time; GetColumn(statement.get(), 0, &db_create_version); diff --git a/chrome/browser/sync/util/query_helpers.cc b/chrome/browser/sync/util/query_helpers.cc index 46e8ee0..21d7e8e 100644 --- a/chrome/browser/sync/util/query_helpers.cc +++ b/chrome/browser/sync/util/query_helpers.cc @@ -26,6 +26,7 @@ sqlite3_stmt* PrepareQuery(sqlite3* dbhandle, const char* query) { CountBytes(query), &statement, &query_tail)) { LOG(ERROR) << query << "\n" << sqlite3_errmsg(dbhandle); + return NULL; } return statement; } @@ -201,17 +202,25 @@ void GetColumn(sqlite3_stmt* statement, int index, std::vector<uint8>* value) { } } -bool DoesTableExist(sqlite3* dbhandle, const string& table_name) { +bool DoesTableExist(sqlite3* dbhandle, const string& table_name, + bool* exists) { + CHECK(exists); ScopedStatement count_query (PrepareQuery(dbhandle, "SELECT count(*) from sqlite_master where name = ?", table_name)); + if (!count_query.get()) + return false; + int query_result = sqlite3_step(count_query.get()); - CHECK(SQLITE_ROW == query_result); + if (SQLITE_ROW != query_result) + return false; + int count = sqlite3_column_int(count_query.get(), 0); - return 1 == count; + *exists = (1 == count); + return true; } void ScopedStatement::reset(sqlite3_stmt* statement) { diff --git a/chrome/browser/sync/util/query_helpers.h b/chrome/browser/sync/util/query_helpers.h index c44e635..3aedfe2 100644 --- a/chrome/browser/sync/util/query_helpers.h +++ b/chrome/browser/sync/util/query_helpers.h @@ -43,7 +43,13 @@ void GetColumn(sqlite3_stmt*, int index, double* value); void GetColumn(sqlite3_stmt*, int index, bool* value); void GetColumn(sqlite3_stmt*, int index, std::vector<uint8>* value); -bool DoesTableExist(sqlite3* dbhandle, const std::string& tablename); +// Checks if tablename exists in the database referenced by dbhandle. +// If the function succeeds, *exists is set to true if the table exists, and +// false if it doesn't. +// +// Returns true on success, or false on error. +bool DoesTableExist(sqlite3* dbhandle, const std::string& tablename, + bool* exists); // Prepares a query with a WHERE clause that filters the values by the items // passed inside of the Vector. |