From 5e6f9b3460f2db06f60f273dbd20bb06986514d8 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Fri, 25 Sep 2020 12:31:52 -0700 Subject: [PATCH] Fix some return of ptr-to-temporary errors Flagged by new clang Apple version 12. --- libgnucash/engine/qof-backend.hpp | 2 +- libgnucash/engine/qofsession.cpp | 12 +++++++----- libgnucash/engine/qofsession.hpp | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libgnucash/engine/qof-backend.hpp b/libgnucash/engine/qof-backend.hpp index f2e8985088..5fd51e42dd 100644 --- a/libgnucash/engine/qof-backend.hpp +++ b/libgnucash/engine/qof-backend.hpp @@ -273,7 +273,7 @@ public: QofBePercentageFunc get_percentage() { return m_percentage; } /** Retrieve the backend's storage URI. */ - std::string get_uri() { return m_fullpath; } + const std::string& get_uri() { return m_fullpath; } /** * Class methods for dynamically loading the several backends and for freeing * them at shutdown. diff --git a/libgnucash/engine/qofsession.cpp b/libgnucash/engine/qofsession.cpp index 7e3ad94208..86edb1fc67 100644 --- a/libgnucash/engine/qofsession.cpp +++ b/libgnucash/engine/qofsession.cpp @@ -69,6 +69,7 @@ static QofLogModule log_module = QOF_MOD_SESSION; using ProviderVec = std::vector; static ProviderVec s_providers; +static const std::string empty_string{}; /* * These getters are used in tests to reach static vars from outside * They should be removed when no longer needed @@ -383,7 +384,7 @@ QofSessionImpl::get_error () noexcept return m_last_err; } -std::string +const std::string& QofSessionImpl::get_error_message () const noexcept { return m_error_message; @@ -414,11 +415,11 @@ QofSession::get_backend () const noexcept return m_backend; } -std::string +const std::string& QofSessionImpl::get_file_path () const noexcept { auto backend = qof_book_get_backend (m_book); - if (!backend) return nullptr; + if (!backend) return empty_string; return backend->get_uri(); } @@ -583,8 +584,9 @@ qof_session_get_book (const QofSession *session) const char * qof_session_get_file_path (const QofSession *session) { - if (!session) return NULL; - return session->get_file_path ().c_str (); + if (!session) return nullptr; + auto& path{session->get_file_path()}; + return path.empty() ? nullptr : path.c_str (); } void diff --git a/libgnucash/engine/qofsession.hpp b/libgnucash/engine/qofsession.hpp index 9029320c39..ea845b54e4 100644 --- a/libgnucash/engine/qofsession.hpp +++ b/libgnucash/engine/qofsession.hpp @@ -68,10 +68,10 @@ struct QofSessionImpl * for an error in the backend. */ QofBackendError get_error () noexcept; - std::string get_error_message () const noexcept; + const std::string& get_error_message () const noexcept; QofBook * get_book () const noexcept; QofBackend * get_backend () const noexcept; - std::string get_file_path () const noexcept; + const std::string& get_file_path () const noexcept; bool is_saving () const noexcept; /**