diff --git a/avc/avcconfiguration.cpp b/avc/avcconfiguration.cpp index 4e0bcb9..a40eadf 100644 --- a/avc/avcconfiguration.cpp +++ b/avc/avcconfiguration.cpp @@ -41,18 +41,17 @@ void AvcConfiguration::parse(BinaryReader &reader, std::uint64_t maxSize, Diagno std::uint8_t entryCount = reader.readByte() & 0x0f; spsInfos.reserve(entryCount); for (; entryCount; --entryCount) { - if (maxSize < 2) { + if (maxSize < SpsInfo::minSize) { throw TruncatedDataException(); } - spsInfos.emplace_back(); try { - spsInfos.back().parse( + spsInfos.emplace_back().parse( reader, maxSize > numeric_limits::max() ? numeric_limits::max() : static_cast(maxSize)); } catch (const TruncatedDataException &) { - if (spsInfos.back().size > maxSize - 2) { - throw; + if (spsInfos.back().size > (maxSize - SpsInfo::minSize)) { + throw; // sps info looks bigger than bytes to read } - spsInfos.pop_back(); + spsInfos.pop_back(); // sps info exceeds denoted size } catch (const Failure &) { spsInfos.pop_back(); // TODO: log parsing error @@ -64,18 +63,17 @@ void AvcConfiguration::parse(BinaryReader &reader, std::uint64_t maxSize, Diagno entryCount = reader.readByte(); ppsInfos.reserve(entryCount); for (; entryCount; --entryCount) { - if (maxSize < 2) { + if (maxSize < PpsInfo::minSize) { throw TruncatedDataException(); } - ppsInfos.emplace_back(); try { - ppsInfos.back().parse( + ppsInfos.emplace_back().parse( reader, maxSize > numeric_limits::max() ? numeric_limits::max() : static_cast(maxSize)); } catch (const TruncatedDataException &) { - if (ppsInfos.back().size > maxSize - 2) { - throw; + if (ppsInfos.back().size > (maxSize - PpsInfo::minSize)) { + throw; // pps info looks bigger than bytes to read } - ppsInfos.pop_back(); + ppsInfos.pop_back(); // pps info exceeds denoted size } catch (const Failure &) { ppsInfos.pop_back(); // TODO: log parsing error diff --git a/avc/avcinfo.cpp b/avc/avcinfo.cpp index 93a4fa7..fb17ffa 100644 --- a/avc/avcinfo.cpp +++ b/avc/avcinfo.cpp @@ -24,11 +24,7 @@ namespace TagParser { void SpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize) { // read (and check) size - if (maxSize < 2) { - throw TruncatedDataException(); - } - maxSize -= 2; - if ((size = reader.readUInt16BE()) > maxSize) { + if ((maxSize < minSize) || (size = reader.readUInt16BE()) > (maxSize - minSize)) { throw TruncatedDataException(); } @@ -225,11 +221,7 @@ void SpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize) void PpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize) { // read (and check) size - if (maxSize < 2) { - throw TruncatedDataException(); - } - maxSize -= 2; - if ((size = reader.readUInt16BE()) > maxSize) { + if ((maxSize < minSize) || (size = reader.readUInt16BE()) > (maxSize - minSize)) { throw TruncatedDataException(); } diff --git a/avc/avcinfo.h b/avc/avcinfo.h index a107d73..a1fd385 100644 --- a/avc/avcinfo.h +++ b/avc/avcinfo.h @@ -93,6 +93,7 @@ struct TAG_PARSER_EXPORT SpsInfo { HrdParameters vclHrdParameters; std::uint8_t pictureStructPresent; std::uint16_t size; + static constexpr std::uint16_t minSize = 2; void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize); }; @@ -124,6 +125,7 @@ struct TAG_PARSER_EXPORT PpsInfo { ugolomb spsId; std::uint8_t picOrderPresent; std::uint16_t size; + static constexpr std::uint16_t minSize = 2; void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize); };