Auto-pointers introduced to reduce possible memory-leak scenarios

This commit is contained in:
Simon Rozman 2016-05-24 21:43:32 +02:00
parent 986deb3fef
commit bf28f7f55e
2 changed files with 15 additions and 31 deletions

View File

@ -27,5 +27,6 @@
#include <wx/ffile.h> #include <wx/ffile.h>
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/init.h> #include <wx/init.h>
#include <wx/scopedptr.h>
#include <wxex/common.h> #include <wxex/common.h>

View File

@ -19,6 +19,10 @@
#include "stdafx.h" #include "stdafx.h"
//////////////////////////////////////////////////////////////////////////
// wxEVT_UPDATER_CHECK_COMPLETE
//////////////////////////////////////////////////////////////////////////
wxDEFINE_EVENT(wxEVT_UPDATER_CHECK_COMPLETE, wxThreadEvent); wxDEFINE_EVENT(wxEVT_UPDATER_CHECK_COMPLETE, wxThreadEvent);
@ -88,7 +92,7 @@ wxUpdCheckThread::wxResult wxUpdCheckThread::DoCheckForUpdate()
// Download update catalogue. // Download update catalogue.
if (TestDestroy()) return wxUpdAborted; if (TestDestroy()) return wxUpdAborted;
wxXmlDocument *doc = GetCatalogue(); wxScopedPtr<wxXmlDocument> doc(GetCatalogue());
if (doc) { if (doc) {
// Parse the update catalogue to check for an update package availability. // Parse the update catalogue to check for an update package availability.
if (TestDestroy()) return wxUpdAborted; if (TestDestroy()) return wxUpdAborted;
@ -149,27 +153,21 @@ wxXmlDocument* wxUpdCheckThread::GetCatalogue()
wxLogWarning(_("Error resolving %s server name."), server); wxLogWarning(_("Error resolving %s server name."), server);
continue; continue;
} }
wxInputStream *httpStream = http.GetInputStream(wxS(UPDATER_HTTP_PATH)); wxScopedPtr<wxInputStream> httpStream(http.GetInputStream(wxS(UPDATER_HTTP_PATH)));
if (http.GetResponse() == 304) { if (http.GetResponse() == 304) {
wxLogStatus(_("Repository did not change since the last time.")); wxLogStatus(_("Repository did not change since the last time."));
wxDELETE(httpStream);
return NULL; return NULL;
} else if (!httpStream) { } else if (!httpStream) {
wxLogWarning(_("Error response received from server %s (port %u) requesting %s."), server, UPDATER_HTTP_PORT, UPDATER_HTTP_PATH); wxLogWarning(_("Error response received from server %s (port %u) requesting %s."), server, UPDATER_HTTP_PORT, UPDATER_HTTP_PATH);
continue; continue;
} else if (TestDestroy()) { } else if (TestDestroy())
wxDELETE(httpStream);
return NULL; return NULL;
} wxScopedPtr<wxXmlDocument> doc(new wxXmlDocument());
wxXmlDocument *doc = new wxXmlDocument();
if (!doc->Load(*httpStream, "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES)) { if (!doc->Load(*httpStream, "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES)) {
wxLogWarning(_("Error loading repository catalogue.")); wxLogWarning(_("Error loading repository catalogue."));
wxDELETE(httpStream);
http.Close(); http.Close();
delete doc;
continue; continue;
} }
wxDELETE(httpStream);
m_config.Write(wxT("CatalogueLastModified"), http.GetHeader(wxT("Last-Modified"))); m_config.Write(wxT("CatalogueLastModified"), http.GetHeader(wxT("Last-Modified")));
http.Close(); http.Close();
@ -205,29 +203,19 @@ wxXmlDocument* wxUpdCheckThread::GetCatalogue()
if (sig.IsEmpty()) { if (sig.IsEmpty()) {
wxLogWarning(_("Signature not found in the repository catalogue.")); wxLogWarning(_("Signature not found in the repository catalogue."));
delete doc;
continue; continue;
} }
// Hash the content. // Hash the content.
if (TestDestroy()) { if (TestDestroy()) return NULL;
delete doc;
return NULL;
}
wxCryptoHashSHA1 ch(*m_cs); wxCryptoHashSHA1 ch(*m_cs);
if (!wxXmlHashNode(ch, document)) { if (!wxXmlHashNode(ch, document))
delete doc;
continue; continue;
}
// We have the hash, we have the signature, we have the public key. Now verify. // We have the hash, we have the signature, we have the public key. Now verify.
if (TestDestroy()) { if (TestDestroy()) return NULL;
delete doc;
return NULL;
}
if (!wxCryptoVerifySignature(ch, sig, *m_ck)) { if (!wxCryptoVerifySignature(ch, sig, *m_ck)) {
wxLogWarning(_("Repository catalogue signature does not match its content, or signature verification failed.")); wxLogWarning(_("Repository catalogue signature does not match its content, or signature verification failed."));
delete doc;
continue; continue;
} }
@ -235,12 +223,11 @@ wxXmlDocument* wxUpdCheckThread::GetCatalogue()
const wxString &nameRoot = doc->GetRoot()->GetName(); const wxString &nameRoot = doc->GetRoot()->GetName();
if (nameRoot != wxT("Packages")) { if (nameRoot != wxT("Packages")) {
wxLogWarning(_("Invalid root element in repository catalogue (actual: %s, expected: %s)."), nameRoot.c_str(), wxT("Packages")); wxLogWarning(_("Invalid root element in repository catalogue (actual: %s, expected: %s)."), nameRoot.c_str(), wxT("Packages"));
delete doc;
continue; continue;
} }
// The downloaded repository database passed all checks. // The downloaded repository database passed all checks.
return doc; return doc.release();
} }
// No repository database downloaded successfully. // No repository database downloaded successfully.
@ -445,7 +432,7 @@ bool wxUpdCheckThread::DownloadUpdatePackage()
} }
wxLogStatus(_("Downloading update package from %s..."), m_urls[i].c_str()); wxLogStatus(_("Downloading update package from %s..."), m_urls[i].c_str());
wxInputStream *stream = url.GetInputStream(); wxScopedPtr<wxInputStream> stream(url.GetInputStream());
if (!stream) { if (!stream) {
wxLogWarning(_("Error response received.")); wxLogWarning(_("Error response received."));
continue; continue;
@ -457,10 +444,7 @@ bool wxUpdCheckThread::DownloadUpdatePackage()
char *data = (char*)buf.GetData(); char *data = (char*)buf.GetData();
size_t nBlock = buf.GetBufSize(); size_t nBlock = buf.GetBufSize();
do { do {
if (TestDestroy()) { if (TestDestroy()) return false;
wxDELETE(stream);
return false;
}
stream->Read(data, nBlock); stream->Read(data, nBlock);
size_t nRead = stream->LastRead(); size_t nRead = stream->LastRead();
@ -472,7 +456,6 @@ bool wxUpdCheckThread::DownloadUpdatePackage()
ch.Hash(data, nRead); ch.Hash(data, nRead);
} while (stream->IsOk()); } while (stream->IsOk());
ch.GetValue(buf); ch.GetValue(buf);
wxDELETE(stream);
if (buf.GetDataLen() == m_hash.GetDataLen() && if (buf.GetDataLen() == m_hash.GetDataLen() &&
memcmp(buf.GetData(), m_hash.GetData(), buf.GetDataLen()) == 0) memcmp(buf.GetData(), m_hash.GetData(), buf.GetDataLen()) == 0)