Permalink
Browse files

Avoid some possible crashes

This adds some extra sanity checks in the code to avoid possible
crashes. The most likely way to trigger them is loading an outdated
project file which references tables that no longer exist.
  • Loading branch information...
MKleusberg committed Sep 27, 2018
1 parent 54cf4cf commit 717ff075b52b64603338e0629710bac3e55c41d6
Showing with 14 additions and 11 deletions.
  1. +13 −10 src/MainWindow.cpp
  2. +1 −1 src/sqlitetablemodel.cpp
@@ -637,16 +637,19 @@ void MainWindow::populateTable()
// Load display formats and set them along with the table name
QVector<QString> v;
bool only_defaults = true;
const sqlb::FieldInfoList& tablefields = db.getObjectByName(tablename)->fieldInformation();
for(size_t i=0; i<tablefields.size(); ++i)
if(db.getObjectByName(tablename))
{
QString format = storedData.displayFormats[i+1];
if(format.size())
const sqlb::FieldInfoList& tablefields = db.getObjectByName(tablename)->fieldInformation();
for(size_t i=0; i<tablefields.size(); ++i)
{
v.push_back(format);
only_defaults = false;
} else {
v.push_back(sqlb::escapeIdentifier(tablefields.at(i).name));
QString format = storedData.displayFormats[i+1];
if(format.size())
{
v.push_back(format);
only_defaults = false;
} else {
v.push_back(sqlb::escapeIdentifier(tablefields.at(i).name));
}
}
}
if(only_defaults)
@@ -664,7 +667,7 @@ void MainWindow::populateTable()
}

// Show/hide menu options depending on whether this is a table or a view
if(db.getObjectByName(currentlyBrowsedTableName())->type() == sqlb::Object::Table)
if(db.getObjectByName(currentlyBrowsedTableName()) && db.getObjectByName(currentlyBrowsedTableName())->type() == sqlb::Object::Table)
{
// Table
sqlb::TablePtr table = db.getObjectByName<sqlb::Table>(currentlyBrowsedTableName());
@@ -3133,7 +3136,7 @@ void MainWindow::unlockViewEditing(bool unlock, QString pk)
sqlb::ObjectIdentifier currentTable = currentlyBrowsedTableName();

// If this isn't a view just unlock editing and return
if(db.getObjectByName(currentTable)->type() != sqlb::Object::View)
if(db.getObjectByName(currentTable) && db.getObjectByName(currentTable)->type() != sqlb::Object::View)
{
m_browseTableModel->setPseudoPk(QString());
enableEditing(true);
@@ -130,7 +130,7 @@ void SqliteTableModel::setTable(const sqlb::ObjectIdentifier& table, int sortCol

// Get the data types of all other columns as well as the column names
bool allOk = false;
if(m_db.getObjectByName(table)->type() == sqlb::Object::Types::Table)
if(m_db.getObjectByName(table) && m_db.getObjectByName(table)->type() == sqlb::Object::Types::Table)
{
sqlb::TablePtr t = m_db.getObjectByName<sqlb::Table>(table);
if(t && t->fields.size()) // parsing was OK

0 comments on commit 717ff07

Please sign in to comment.