Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 717ff07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions src/MainWindow.cpp
Expand Up @@ -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)
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/sqlitetablemodel.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 717ff07

Please sign in to comment.