Fix a couple of bugs restoring backup sessions

With https://community.aseprite.org/t/layers-become-empty-when-autosaving/22141
we found a couple of bugs where only 2 backed up versions of stored
objects were added in ObjVersions::add().

Also as ObjVersions has a limited space for 3 versions, it's a good
idea to ignore invalid binary files (files without the magic number
"FINE" as header). So now we only add valid versions to ObjVersions,
previously we could lead to a situation where 3 invalid binary
versions of the same object were added to the ObjVersions and a 4th
valid version were ignored.

Related to: https://github.com/aseprite/aseprite/issues/4481
This commit is contained in:
David Capello 2024-05-23 12:26:25 -03:00
parent 577f50b713
commit 1f529bd610
2 changed files with 17 additions and 3 deletions

View File

@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2024 Igara Studio S.A.
// Copyright (C) 2001-2015 David Capello
//
// This program is distributed under the terms of
@ -43,10 +44,10 @@ namespace crash {
// Adds a version (we don't know if the version if the latest one)
void add(doc::ObjectVersion ver) {
auto minver = std::min_element(m_vers, m_vers+2);
auto* minver = std::min_element(m_vers, m_vers+size());
if (*minver < ver) {
*minver = ver;
std::sort(m_vers, m_vers+2, std::greater<doc::ObjectVersion>());
std::sort(m_vers, m_vers+size(), std::greater<doc::ObjectVersion>());
}
}

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2018-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -58,6 +58,14 @@ using namespace doc;
namespace {
// Returns true if the file was saved correctly (has the "FINE" magic
// number), so we can ignore broken versions of objects directly.
bool check_magic_number(const std::string& fn)
{
std::ifstream s(FSTREAM_PATH(fn), std::ifstream::binary);
return (read32(s) == MAGIC_NUMBER);
}
class Reader : public SubObjectsIO {
public:
Reader(const std::string& dir,
@ -83,6 +91,11 @@ public:
if (!id || !ver)
continue; // Error converting strings to ID/ver
if (!check_magic_number(base::join_path(m_dir, fn))) {
RECO_TRACE("RECO: Ignoring invalid file %s (no magic number)\n", fn.c_str());
continue;
}
ObjVersions& versions = m_objVersions[id];
versions.add(ver);