Tests for Pnm.hpp, Pnm operator>> input robustness

This commit is contained in:
Vladyslav Baranovskyi
2024-10-10 15:36:22 +03:00
parent 64c60b3726
commit cc20a1fb3b
2 changed files with 219 additions and 2 deletions

View File

@@ -87,8 +87,25 @@ namespace OpenVulkano
if (val > 6) throw std::runtime_error("Malformed PNM header!");
pnmHeader.ascii = val < 4;
pnmHeader.color = static_cast<ColorMode>(val - 3);
inStream >> pnmHeader.width >> pnmHeader.height;
inStream >> pnmHeader.maxValue;
if (!(inStream >> pnmHeader.width >> pnmHeader.height))
{
throw std::runtime_error("Malformed PNM header: Unable to read width and height!");
}
if (pnmHeader.width == 0 || pnmHeader.height == 0)
{
throw std::runtime_error("Malformed PNM header: Width and height must be positive!");
}
if (!(inStream >> pnmHeader.maxValue))
{
throw std::runtime_error("Malformed PNM header: Unable to read maxValue!");
}
if (pnmHeader.maxValue == 0 || pnmHeader.maxValue > 65535)
{
throw std::runtime_error("Malformed PNM header: Invalid maxValue (must be between 1 and 65535)!");
}
inStream.get();
return inStream;
}