From c96d5f12bc8e2e715621b2730546d8a2519c9383 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 16 May 2021 08:06:18 -0400 Subject: [PATCH] Comments; range-for; fix unchecked dereferences of pointer-to-pointer --- src/AudacityApp.cpp | 5 +++-- src/AutoRecoveryDialog.cpp | 17 +++++++---------- src/AutoRecoveryDialog.h | 2 +- src/ProjectManager.cpp | 6 ++---- src/commands/OpenSaveCommands.cpp | 1 + 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index 95dccffb0..56a9c9d07 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -1487,7 +1487,8 @@ bool AudacityApp::InitPart2() // Auto-recovery // bool didRecoverAnything = false; - if (!ShowAutoRecoveryDialogIfNeeded(&project, &didRecoverAnything)) + // This call may reassign project (passed by reference) + if (!ShowAutoRecoveryDialogIfNeeded(project, &didRecoverAnything)) { QuitAudacity(true); } @@ -1495,7 +1496,7 @@ bool AudacityApp::InitPart2() // // Remainder of command line parsing, but only if we didn't recover // - if (!didRecoverAnything) + if (project && !didRecoverAnything) { if (parser->Found(wxT("t"))) { diff --git a/src/AutoRecoveryDialog.cpp b/src/AutoRecoveryDialog.cpp index 7c71da5b4..b09a591ef 100644 --- a/src/AutoRecoveryDialog.cpp +++ b/src/AutoRecoveryDialog.cpp @@ -36,7 +36,7 @@ enum { class AutoRecoveryDialog final : public wxDialogWrapper { public: - AutoRecoveryDialog(AudacityProject *proj); + explicit AutoRecoveryDialog(AudacityProject *proj); bool HasRecoverables() const; FilePaths GetRecoverables(); @@ -417,7 +417,7 @@ void AutoRecoveryDialog::OnListKeyDown(wxKeyEvent &evt) //////////////////////////////////////////////////////////////////////////// static bool RecoverAllProjects(const FilePaths &files, - AudacityProject **pproj) + AudacityProject *&pproj) { // Open a project window for each auto save file wxString filename; @@ -425,12 +425,9 @@ static bool RecoverAllProjects(const FilePaths &files, for (auto &file: files) { AudacityProject *proj = nullptr; - if (*pproj) - { - // Reuse existing project window - proj = *pproj; - *pproj = NULL; - } + // Reuse any existing project window, which will be the empty project + // created at application startup + std::swap(proj, pproj); // Open project. if (ProjectManager::OpenProject(proj, file, false) == nullptr) @@ -449,7 +446,7 @@ static void DiscardAllProjects(const FilePaths &files) ProjectFileManager::DiscardAutosave(file); } -bool ShowAutoRecoveryDialogIfNeeded(AudacityProject **pproj, bool *didRecoverAnything) +bool ShowAutoRecoveryDialogIfNeeded(AudacityProject *&pproj, bool *didRecoverAnything) { if (didRecoverAnything) { @@ -472,7 +469,7 @@ bool ShowAutoRecoveryDialogIfNeeded(AudacityProject **pproj, bool *didRecoverAny // This must be done before "dlg" is declared. wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI); - AutoRecoveryDialog dialog(*pproj); + AutoRecoveryDialog dialog(pproj); if (dialog.HasRecoverables()) { diff --git a/src/AutoRecoveryDialog.h b/src/AutoRecoveryDialog.h index 06f20b5ff..89dd9002c 100644 --- a/src/AutoRecoveryDialog.h +++ b/src/AutoRecoveryDialog.h @@ -26,7 +26,7 @@ class AudacityProject; // The didRecoverAnything param is strictly for a return value. // Any value passed in is ignored. // -bool ShowAutoRecoveryDialogIfNeeded(AudacityProject** pproj, +bool ShowAutoRecoveryDialogIfNeeded(AudacityProject*& pproj, bool *didRecoverAnything); #endif diff --git a/src/ProjectManager.cpp b/src/ProjectManager.cpp index f231b8643..e8c46937a 100644 --- a/src/ProjectManager.cpp +++ b/src/ProjectManager.cpp @@ -868,9 +868,7 @@ void ProjectManager::OpenFiles(AudacityProject *proj) Importer::SetLastOpenType({}); } ); - for (size_t ff = 0; ff < selectedFiles.size(); ff++) { - const wxString &fileName = selectedFiles[ff]; - + for (const auto &fileName : selectedFiles) { // Make sure it isn't already open. if (ProjectFileManager::IsAlreadyOpen(fileName)) continue; // Skip ones that are already open. @@ -912,7 +910,7 @@ AudacityProject *ProjectManager::OpenProject( // Ensure that it happens here: don't wait for the application level // exception handler, because the exception may be intercepted ProjectHistory::Get(*pProject).RollbackState(); - // Any exception now continues propagating + // Any exception now continues propagating } ); ProjectFileManager::Get( *pProject ).OpenFile( fileNameArg, addtohistory ); diff --git a/src/commands/OpenSaveCommands.cpp b/src/commands/OpenSaveCommands.cpp index 9219ea6a5..ab7d8cdd0 100644 --- a/src/commands/OpenSaveCommands.cpp +++ b/src/commands/OpenSaveCommands.cpp @@ -59,6 +59,7 @@ bool OpenProjectCommand::Apply(const CommandContext & context){ auto oldFileName = projectFileIO.GetFileName(); if(mFileName.empty()) { + // This path queries the user for files to open auto project = &context.project; ProjectManager::OpenFiles(project); }