Added checks and try-catch when attempting to load icons

Fixes #437
This commit is contained in:
Guillaume Meunier
2025-08-09 10:12:58 +02:00
parent b036731b51
commit d5ef8c1a85
2 changed files with 299 additions and 304 deletions
+11 -23
View File
@@ -126,10 +126,7 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
}; };
if (!rsvg_handle_render_document(handle.get(), cr.get(), &viewport, &error)) if (!rsvg_handle_render_document(handle.get(), cr.get(), &viewport, &error))
{ throw std::runtime_error{std::string{"Could not render: "} + error->message};
g_printerr("could not render: %s", error->message);
return {};
}
// Write a PNG file // Write a PNG file
std::vector<std::byte> png; std::vector<std::byte> png;
@@ -143,11 +140,8 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
return CAIRO_STATUS_SUCCESS; return CAIRO_STATUS_SUCCESS;
}; };
if (cairo_surface_write_to_png_stream(surface.get(), cb, &png) != CAIRO_STATUS_SUCCESS) if (auto status = cairo_surface_write_to_png_stream(surface.get(), cb, &png); status != CAIRO_STATUS_SUCCESS)
{ throw std::runtime_error{std::string{"Could not write output: "} + cairo_status_to_string(status)};
g_printerr("could not write output file");
return {};
}
return png; return png;
} }
@@ -201,8 +195,6 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
{ {
const uint32_t ICO_PNG_MAGIC = 0x474e5089; const uint32_t ICO_PNG_MAGIC = 0x474e5089;
try
{
auto ico = load_file(filename); auto ico = load_file(filename);
std::span header{ico}; std::span header{ico};
@@ -212,7 +204,7 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
auto icon_count = read<uint16_t>(header); auto icon_count = read<uint16_t>(header);
if (reserved != 0 or resource_type != 1) if (reserved != 0 or resource_type != 1)
return {}; throw std::runtime_error{"Cannot read icon: " + filename.native()};
std::vector<ico_file_entry> entries; std::vector<ico_file_entry> entries;
int max_bpp = 0; int max_bpp = 0;
@@ -230,7 +222,7 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
auto offset = read<uint32_t>(header); auto offset = read<uint32_t>(header);
if (offset + size > ico.size()) if (offset + size > ico.size())
return {}; throw std::runtime_error{"Cannot read icon: " + filename.native()};
entry.data = std::span{ico}.subspan(offset, size); entry.data = std::span{ico}.subspan(offset, size);
if (entry.width == 0) if (entry.width == 0)
@@ -238,9 +230,9 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
if (entry.height == 0) if (entry.height == 0)
entry.height = 256; entry.height = 256;
if (reserved != 0) if (reserved != 0)
return {}; throw std::runtime_error{"Cannot read icon: " + filename.native()};
if (planes != 0 and planes != 1) if (planes != 0 and planes != 1)
return {}; throw std::runtime_error{"Cannot read icon: " + filename.native()};
entries.push_back(entry); entries.push_back(entry);
@@ -308,14 +300,14 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
.format = PNG_FORMAT_BGRA, .format = PNG_FORMAT_BGRA,
}; };
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size())) if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
return {}; throw std::runtime_error{"Cannot read icon: " + filename.native()};
pixels.resize(PNG_IMAGE_SIZE(png_ref)); pixels.resize(PNG_IMAGE_SIZE(png_ref));
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr)) if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
{ {
png_image_free(&png_ref); png_image_free(&png_ref);
return {}; throw std::runtime_error{"Cannot read icon: " + filename.native()};
} }
png_image_free(&png_ref); png_image_free(&png_ref);
@@ -541,12 +533,8 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
return png_data; return png_data;
} }
}
catch (std::exception & e)
{
}
return {}; throw std::runtime_error{"Wrong magic number in " + filename.native()};
} }
std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache; std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
@@ -556,7 +544,7 @@ std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
const std::vector<std::byte> & wivrn::load_icon(const std::filesystem::path & filename, int size) const std::vector<std::byte> & wivrn::load_icon(const std::filesystem::path & filename, int size)
{ {
auto it = icon_cache.find(filename); auto it = icon_cache.find(filename);
if (it != icon_cache.end()) if (it != icon_cache.end() and not it->second.empty())
return it->second; return it->second;
std::vector<std::byte> icon; std::vector<std::byte> icon;
+7
View File
@@ -756,6 +756,8 @@ void wivrn_session::operator()(from_headset::get_application_list && request)
for (const auto & [id, app]: apps) for (const auto & [id, app]: apps)
{ {
if (app.icon_path) if (app.icon_path)
{
try
{ {
auto icon = load_icon(*app.icon_path, 256); auto icon = load_icon(*app.icon_path, 256);
@@ -764,6 +766,11 @@ void wivrn_session::operator()(from_headset::get_application_list && request)
.image = std::move(icon), .image = std::move(icon),
}); });
} }
catch (std::exception & e)
{
U_LOG_W("Error loading icon %s: %s", app.icon_path->c_str(), e.what());
}
}
} }
} }