Skip to content

Commit

Permalink
Protect against session initialization failures
Browse files Browse the repository at this point in the history
Fill config with zeros before passing it to libspotify.

If Spotify session can't be created inform the user and exit the application.
  • Loading branch information
Aki Koskinen committed Jul 9, 2012
1 parent 994d054 commit a260c02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ int main(int argc, char **argv)
KGlobal::activeComponent().setAboutData(aboutData);

MainWindow *window = new MainWindow();
if (window->session() == NULL) {
delete window;
return EXIT_FAILURE;
}

window->show();

return app.exec();
Expand Down
7 changes: 6 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ MainWindow::MainWindow(QWidget *parent)
//BEGIN: Spotify session init
{
const QByteArray settingsPath = QString("%1/.config/spotify").arg(QDir::homePath()).toUtf8();
memset(&m_config, 0, sizeof(m_config));
m_config.api_version = SPOTIFY_API_VERSION;
m_config.cache_location = settingsPath.constData();
m_config.settings_location = settingsPath.constData();
Expand All @@ -598,7 +599,11 @@ MainWindow::MainWindow(QWidget *parent)
m_config.callbacks = &SpotifySession::spotifyCallbacks;

#if SPOTIFY_API_VERSION > 4
sp_session_create(&m_config, &m_session);
sp_error error = sp_session_create(&m_config, &m_session);
if (error != SP_ERROR_OK) {
KMessageBox::error(this, i18n("Couldn't create Spotify session.\n\nThe error message is \"%1\".\n\nPlease try again. If the problem persists contact the developers.", sp_error_message(error)), i18n("A critical error happened"));
return;
}
#else
sp_session_init(&m_config, &m_session);
#endif
Expand Down

0 comments on commit a260c02

Please sign in to comment.