Skip to content

Commit

Permalink
Don't crash on empty appcasts.
Browse files Browse the repository at this point in the history
This bug was introduced by PR #14 in ec85b3c.

Fixes #24.
  • Loading branch information
vslavik committed Jun 6, 2014
1 parent fef8fa6 commit 007f0c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/appcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ bool is_windows_item(const Appcast &item)
Appcast class
*--------------------------------------------------------------------------*/

void Appcast::Load(const std::string& xml)
Appcast Appcast::Load(const std::string& xml)
{
XML_Parser p = XML_ParserCreateNS(NULL, NS_SEP);
if ( !p )
Expand All @@ -211,12 +211,16 @@ void Appcast::Load(const std::string& xml)

XML_ParserFree(p);

if (ctxt.items.empty())
return Appcast(); // invalid

// Search for first <item> which has "sparkle:os=windows" attribute.
// If none, use the first item.
std::vector<Appcast>::iterator it = std::find_if(ctxt.items.begin(), ctxt.items.end(), is_windows_item);
if (it == ctxt.items.end())
it = ctxt.items.begin();
*this = *it;
if (it != ctxt.items.end())
return *it;
else
return ctxt.items.front();
}

} // namespace winsparkle
13 changes: 9 additions & 4 deletions src/appcast.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,25 @@ struct Appcast
/// Description of the update
std::string Description;

// Operating system
std::string Os;
// Operating system
std::string Os;

/**
Initializes the struct with data from XML appcast feed.
If the feed contains multiple entries, only the latest one is read,
the rest is ignored.
the rest is ignored. Entries that are not appliable (e.g. for different
OS) are likewise skipped.
Throws on error.
Returns NULL if no error ocurred, but there was no update in the appcast.
@param xml Appcast feed data.
*/
void Load(const std::string& xml);
static Appcast Load(const std::string& xml);

/// Returns true if the struct constains valid data.
bool IsValid() const { return !Version.empty(); }
};

} // namespace winsparkle
Expand Down
5 changes: 2 additions & 3 deletions src/updatechecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,15 @@ void UpdateChecker::Run()
StringDownloadSink appcast_xml;
DownloadFile(url, &appcast_xml, GetAppcastDownloadFlags());

Appcast appcast;
appcast.Load(appcast_xml.data);
Appcast appcast = Appcast::Load(appcast_xml.data);

Settings::WriteConfigValue("LastCheckTime", time(NULL));

const std::string currentVersion =
WideToAnsi(Settings::GetAppBuildVersion());

// Check if our version is out of date.
if ( CompareVersions(currentVersion, appcast.Version) >= 0 )
if ( !appcast.IsValid() || CompareVersions(currentVersion, appcast.Version) >= 0 )
{
// The same or newer version is already installed.
UI::NotifyNoUpdates();
Expand Down

0 comments on commit 007f0c5

Please sign in to comment.