mirror of
https://github.com/RPCS3/rpcs3.git
synced 2026-04-04 14:08:30 +00:00
Improve narrow() and size32() with src_loc detection
This commit is contained in:
parent
e055d16b2c
commit
5d934c8759
16 changed files with 69 additions and 48 deletions
|
|
@ -24,7 +24,7 @@ static std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
|
|||
const std::size_t buf_size = source.size() + 1;
|
||||
|
||||
// Safe size
|
||||
const int size = narrow<int>(buf_size, "to_wchar" HERE);
|
||||
const int size = narrow<int>(buf_size);
|
||||
|
||||
// Buffer for max possible output length
|
||||
std::unique_ptr<wchar_t[]> buffer(new wchar_t[buf_size + 8 + 32768]);
|
||||
|
|
@ -55,7 +55,7 @@ static void to_utf8(std::string& out, const wchar_t* source)
|
|||
const std::size_t length = std::wcslen(source);
|
||||
|
||||
// Safe buffer size for max possible output length (including null terminator)
|
||||
const int buf_size = narrow<int>(length * 3 + 1, "to_utf8" HERE);
|
||||
const int buf_size = narrow<int>(length * 3 + 1);
|
||||
|
||||
// Resize buffer
|
||||
out.resize(buf_size - 1);
|
||||
|
|
@ -1028,7 +1028,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
|
|||
u64 read(void* buffer, u64 count) override
|
||||
{
|
||||
// TODO (call ReadFile multiple times if count is too big)
|
||||
const int size = narrow<int>(count, "file::read" HERE);
|
||||
const int size = narrow<int>(count);
|
||||
|
||||
DWORD nread;
|
||||
ensure(ReadFile(m_handle, buffer, size, &nread, NULL)); // "file::read"
|
||||
|
|
@ -1039,7 +1039,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
|
|||
u64 write(const void* buffer, u64 count) override
|
||||
{
|
||||
// TODO (call WriteFile multiple times if count is too big)
|
||||
const int size = narrow<int>(count, "file::write" HERE);
|
||||
const int size = narrow<int>(count);
|
||||
|
||||
DWORD nwritten;
|
||||
ensure(WriteFile(m_handle, buffer, size, &nwritten, NULL)); // "file::write"
|
||||
|
|
|
|||
|
|
@ -452,7 +452,7 @@ public:
|
|||
name.append(".gz");
|
||||
|
||||
z_stream zs{};
|
||||
uLong zsz = compressBound(::narrow<u32>(obj.getBufferSize(), HERE)) + 256;
|
||||
uLong zsz = compressBound(::narrow<u32>(obj.getBufferSize())) + 256;
|
||||
auto zbuf = std::make_unique<uchar[]>(zsz);
|
||||
#ifndef _MSC_VER
|
||||
#pragma GCC diagnostic push
|
||||
|
|
|
|||
|
|
@ -287,7 +287,7 @@ namespace fmt
|
|||
thread_ctrl::emergency_exit(out);
|
||||
}
|
||||
|
||||
void raw_narrow_error(const char* msg, const fmt_type_info* sup, u64 arg)
|
||||
void raw_narrow_error(const src_loc& loc, const fmt_type_info* sup, u64 arg)
|
||||
{
|
||||
std::string out{"Narrow error"};
|
||||
|
||||
|
|
@ -298,10 +298,22 @@ namespace fmt
|
|||
out += ")";
|
||||
}
|
||||
|
||||
if (msg)
|
||||
if (loc.col != umax)
|
||||
{
|
||||
out += ": ";
|
||||
out += msg;
|
||||
fmt::append(out, "\n(in file %s:%s[:%s]", loc.file, loc.line, loc.col);
|
||||
}
|
||||
else
|
||||
{
|
||||
fmt::append(out, "\n(in file %s:%s", loc.file, loc.line);
|
||||
}
|
||||
|
||||
if (loc.func && *loc.func)
|
||||
{
|
||||
fmt::append(out, ", in function %s)", loc.func);
|
||||
}
|
||||
else
|
||||
{
|
||||
out += ')';
|
||||
}
|
||||
|
||||
thread_ctrl::emergency_exit(out);
|
||||
|
|
|
|||
|
|
@ -779,7 +779,7 @@ namespace fmt
|
|||
{
|
||||
[[noreturn]] void raw_error(const char* msg);
|
||||
[[noreturn]] void raw_verify_error(const src_loc& loc);
|
||||
[[noreturn]] void raw_narrow_error(const char* msg, const fmt_type_info* sup, u64 arg);
|
||||
[[noreturn]] void raw_narrow_error(const src_loc& loc, const fmt_type_info* sup, u64 arg);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
|
@ -870,13 +870,17 @@ struct narrow_impl<From, To, std::void_t<typename From::simple_type>>
|
|||
};
|
||||
|
||||
template <typename To = void, typename From, typename = decltype(static_cast<To>(std::declval<From>()))>
|
||||
inline To narrow(const From& value, const char* msg = nullptr)
|
||||
[[nodiscard]] constexpr To narrow(const From& value,
|
||||
u32 line = __builtin_LINE(),
|
||||
u32 col = __builtin_COLUMN(),
|
||||
const char* file = __builtin_FILE(),
|
||||
const char* func = __builtin_FUNCTION())
|
||||
{
|
||||
// Narrow check
|
||||
if (narrow_impl<From, To>::test(value))
|
||||
if (narrow_impl<From, To>::test(value)) [[unlikely]]
|
||||
{
|
||||
// Pack value as formatting argument
|
||||
fmt::raw_narrow_error(msg, fmt::get_type_info<fmt_unveil_t<From>>(), fmt_unveil<From>::get(value));
|
||||
fmt::raw_narrow_error({line, col, file, func}, fmt::get_type_info<fmt_unveil_t<From>>(), fmt_unveil<From>::get(value));
|
||||
}
|
||||
|
||||
return static_cast<To>(value);
|
||||
|
|
@ -884,15 +888,20 @@ inline To narrow(const From& value, const char* msg = nullptr)
|
|||
|
||||
// Returns u32 size() for container
|
||||
template <typename CT, typename = decltype(static_cast<u32>(std::declval<CT>().size()))>
|
||||
inline u32 size32(const CT& container, const char* msg = nullptr)
|
||||
[[nodiscard]] constexpr u32 size32(const CT& container,
|
||||
u32 line = __builtin_LINE(),
|
||||
u32 col = __builtin_COLUMN(),
|
||||
const char* file = __builtin_FILE(),
|
||||
const char* func = __builtin_FUNCTION())
|
||||
{
|
||||
return narrow<u32>(container.size(), msg);
|
||||
return narrow<u32>(container.size(), line, col, file, func);
|
||||
}
|
||||
|
||||
// Returns u32 size for an array
|
||||
template <typename T, std::size_t Size>
|
||||
constexpr u32 size32(const T (&)[Size], const char* msg = nullptr)
|
||||
[[nodiscard]] constexpr u32 size32(const T (&)[Size])
|
||||
{
|
||||
static_assert(Size < UINT32_MAX, "Array is too big for 32-bit");
|
||||
return static_cast<u32>(Size);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue