Do not try to create a string from a potentially null ptr, ref #561

This commit is contained in:
emeric
2025-01-05 16:49:21 +01:00
parent 3740c6f899
commit 1f6398b5b4
2 changed files with 26 additions and 5 deletions
+25 -4
View File
@@ -41,20 +41,41 @@
namespace lms::image::STB
{
namespace
{
class StbiException : public Exception
{
public:
StbiException(std::string_view desc)
: Exception{ std::string{ desc } + ": " + getLastFailureReason() }
{
}
private:
static std::string getLastFailureReason()
{
const char* failureReason{ ::stbi_failure_reason() };
return failureReason ? failureReason : "unknown reason";
}
};
} // namespace
RawImage::RawImage(std::span<const std::byte> encodedData)
{
int n{};
_data = UniquePtrFree{ ::stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData.data()), encodedData.size(), &_width, &_height, &n, 3), std::free };
if (!_data)
throw Exception{ "Cannot load image from memory: " + std::string{ ::stbi_failure_reason() } };
throw StbiException{ "Cannot load image from memory" };
}
RawImage::RawImage(const std::filesystem::path& p)
{
int n{};
_data = UniquePtrFree{ stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free };
_data = UniquePtrFree{ stbi_load(p.c_str(), &_width, &_height, &n, 3), std::free };
if (!_data)
throw Exception{ "Cannot load image from file: " + std::string{ ::stbi_failure_reason() } };
{
throw StbiException{ "Cannot load image from file" };
}
}
void RawImage::resize(ImageSize width)
@@ -94,7 +115,7 @@ namespace lms::image::STB
#error "Unhandled STB image resize version"!
#endif
{
throw Exception{ "Failed to resize image:" + std::string{ ::stbi_failure_reason() } };
throw StbiException{ "Failed to resize image" };
}
_data = std::move(resizedData);
@@ -77,7 +77,7 @@ namespace lms::scanner
}
catch (const image::Exception& e)
{
LMS_LOG(DBUPDATER, ERROR, "Cannot read image in file '" << _file.string() << "': " << e.what());
LMS_LOG(DBUPDATER, ERROR, "Cannot read image in file '" << _file.c_str() << "': " << e.what());
}
}