Do not try to create a string from a potentially null ptr, ref #561
This commit is contained in:
@@ -41,20 +41,41 @@
|
|||||||
|
|
||||||
namespace lms::image::STB
|
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)
|
RawImage::RawImage(std::span<const std::byte> encodedData)
|
||||||
{
|
{
|
||||||
int n{};
|
int n{};
|
||||||
_data = UniquePtrFree{ ::stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData.data()), encodedData.size(), &_width, &_height, &n, 3), std::free };
|
_data = UniquePtrFree{ ::stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData.data()), encodedData.size(), &_width, &_height, &n, 3), std::free };
|
||||||
if (!_data)
|
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)
|
RawImage::RawImage(const std::filesystem::path& p)
|
||||||
{
|
{
|
||||||
int n{};
|
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)
|
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)
|
void RawImage::resize(ImageSize width)
|
||||||
@@ -94,7 +115,7 @@ namespace lms::image::STB
|
|||||||
#error "Unhandled STB image resize version"!
|
#error "Unhandled STB image resize version"!
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
throw Exception{ "Failed to resize image:" + std::string{ ::stbi_failure_reason() } };
|
throw StbiException{ "Failed to resize image" };
|
||||||
}
|
}
|
||||||
|
|
||||||
_data = std::move(resizedData);
|
_data = std::move(resizedData);
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ namespace lms::scanner
|
|||||||
}
|
}
|
||||||
catch (const image::Exception& e)
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user