Changeset - r27066:6628e02dec78
[Not reviewed]
master
0 3 1
Charles Pigott - 17 months ago 2023-04-10 13:32:37
charlespigott@googlemail.com
Codechange: Use fmt for SlErrorCorruptFmt
4 files changed with 50 insertions and 30 deletions:
0 comments (0 inline, 0 general)
src/core/pool_func.hpp
Show inline comments
 
@@ -14,7 +14,7 @@
 
#include "mem_func.hpp"
 
#include "pool_type.hpp"
 

	
 
extern void NORETURN SlErrorCorruptFmt(const char *format, ...) WARN_FORMAT(1, 2);
 
#include "../saveload/saveload_error.hpp" // SlErrorCorruptFmt
 

	
 
/**
 
 * Helper for defining the method's signature.
 
@@ -157,13 +157,13 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_
 
DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index)
 
{
 
	if (index >= Tmax_size) {
 
		SlErrorCorruptFmt("%s index " PRINTF_SIZE " out of range (" PRINTF_SIZE ")", this->name, index, Tmax_size);
 
		SlErrorCorruptFmt("{} index {} out of range ({})", this->name, index, Tmax_size);
 
	}
 

	
 
	if (index >= this->size) this->ResizeFor(index);
 

	
 
	if (this->data[index] != nullptr) {
 
		SlErrorCorruptFmt("%s index " PRINTF_SIZE " already in use", this->name, index);
 
		SlErrorCorruptFmt("{} index {} already in use", this->name, index);
 
	}
 

	
 
	return this->AllocateItem(size, index);
src/saveload/saveload.cpp
Show inline comments
 
@@ -315,7 +315,7 @@ static void SlNullPointers()
 
	_sl_version = SAVEGAME_VERSION;
 

	
 
	for (const ChunkHandler &ch : ChunkHandlers()) {
 
		Debug(sl, 3, "Nulling pointers for {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id);
 
		Debug(sl, 3, "Nulling pointers for {}", ch.GetName());
 
		ch.FixPointers();
 
	}
 

	
 
@@ -367,27 +367,6 @@ void NORETURN SlErrorCorrupt(const char 
 
	SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
 
}
 

	
 
void NORETURN SlErrorCorruptFmt(const char *format, ...) WARN_FORMAT(1, 2);
 

	
 
/**
 
 * Issue an SlErrorCorrupt with a format string.
 
 * @param format format string
 
 * @param ... arguments to format string
 
 * @note This function does never return as it throws an exception to
 
 *       break out of all the saveload code.
 
 */
 
void NORETURN SlErrorCorruptFmt(const char *format, ...)
 
{
 
	va_list ap;
 
	char msg[256];
 

	
 
	va_start(ap, format);
 
	vseprintf(msg, lastof(msg), format, ap);
 
	va_end(ap);
 

	
 
	SlErrorCorrupt(msg);
 
}
 

	
 

	
 
typedef void (*AsyncSaveFinishProc)();                      ///< Callback for when the savegame loading is finished.
 
static std::atomic<AsyncSaveFinishProc> _async_save_finish; ///< Callback to call when the savegame loading is finished.
 
@@ -2242,7 +2221,7 @@ static void SlSaveChunk(const ChunkHandl
 
	if (ch.type == CH_READONLY) return;
 

	
 
	SlWriteUint32(ch.id);
 
	Debug(sl, 2, "Saving chunk {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id);
 
	Debug(sl, 2, "Saving chunk {}", ch.GetName());
 

	
 
	_sl.block_mode = ch.type;
 
	_sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
 
@@ -2331,7 +2310,7 @@ static void SlFixPointers()
 
	_sl.action = SLA_PTRS;
 

	
 
	for (const ChunkHandler &ch : ChunkHandlers()) {
 
		Debug(sl, 3, "Fixing pointers for {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id);
 
		Debug(sl, 3, "Fixing pointers for {}", ch.GetName());
 
		ch.FixPointers();
 
	}
 

	
src/saveload/saveload.h
Show inline comments
 
@@ -10,9 +10,9 @@
 
#ifndef SAVELOAD_H
 
#define SAVELOAD_H
 

	
 
#include "saveload_error.hpp"
 
#include "../fileio_type.h"
 
#include "../fios.h"
 
#include "../strings_type.h"
 
#include "../core/span_type.hpp"
 
#include <optional>
 
#include <string>
 
@@ -449,6 +449,15 @@ struct ChunkHandler {
 
	 * @param len Number of bytes to skip.
 
	 */
 
	virtual void LoadCheck(size_t len = 0) const;
 

	
 
	std::string GetName() const
 
	{
 
		return std::string()
 
			+ static_cast<char>(this->id >> 24)
 
			+ static_cast<char>(this->id >> 16)
 
			+ static_cast<char>(this->id >> 8)
 
			+ static_cast<char>(this->id);
 
	}
 
};
 

	
 
/** A reference to ChunkHandler. */
 
@@ -1164,8 +1173,6 @@ void SlCopy(void *object, size_t length,
 
std::vector<SaveLoad> SlTableHeader(const SaveLoadTable &slt);
 
std::vector<SaveLoad> SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct);
 
void SlObject(void *object, const SaveLoadTable &slt);
 
void NORETURN SlError(StringID string, const char *extra_msg = nullptr);
 
void NORETURN SlErrorCorrupt(const char *msg);
 

	
 
bool SaveloadCrashWithMissingNewGRFs();
 

	
src/saveload/saveload_error.hpp
Show inline comments
 
new file 100644
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file saveload.h Functions/types related to errors from savegames. */
 

	
 
#ifndef SAVELOAD_ERROR_HPP
 
#define SAVELOAD_ERROR_HPP
 

	
 
#include "../3rdparty/fmt/format.h"
 
#include "../strings_type.h"
 

	
 
void NORETURN SlError(StringID string, const char *extra_msg = nullptr);
 
void NORETURN SlErrorCorrupt(const char *msg);
 

	
 
/**
 
 * Issue an SlErrorCorrupt with a format string.
 
 * @param format_string The formatting string to tell what to do with the remaining arguments.
 
 * @param fmt_args The arguments to be passed to fmt.
 
 * @tparam T The type of formatting parameter.
 
 * @tparam Args The types of the fmt arguments.
 
 * @note This function does never return as it throws an exception to
 
 *       break out of all the saveload code.
 
 */
 
template <typename T, typename ... Args>
 
static inline void NORETURN SlErrorCorruptFmt(const T &format, Args&&... fmt_args)
 
{
 
	SlErrorCorrupt(fmt::format(format, fmt_args...).c_str());
 
}
 

	
 
#endif /* SAVELOAD_ERROR_HPP */
0 comments (0 inline, 0 general)