Skip to content

Commit

Permalink
idat: protect against trying to output negative size
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Dec 19, 2023
1 parent e3a17ca commit 32b316e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
6 changes: 5 additions & 1 deletion libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2854,7 +2854,11 @@ std::string Box_idat::dump(Indent& indent) const
std::ostringstream sstr;
sstr << Box::dump(indent);

sstr << indent << "number of data bytes: " << get_box_size() - get_header_size() << "\n";
if (get_box_size() >= get_header_size()) {
sstr << indent << "number of data bytes: " << get_box_size() - get_header_size() << "\n";
} else {
sstr << indent << "number of data bytes is invalid\n";
}

return sstr.str();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ endmacro()
# --- tests that require access to internal symbols

if (WITH_REDUCED_VISIBILITY)
message(WARNING "Conversion and JPEG 2000 box unit tests can only be compiled with full symbol visibility (WITH_REDUCED_VISIBILITY=OFF)")
message(WARNING "Conversion and box unit tests can only be compiled with full symbol visibility (WITH_REDUCED_VISIBILITY=OFF)")
else()
add_libheif_test(conversion)
add_libheif_test(idat)
add_libheif_test(jpeg2000)
endif()

Expand Down
51 changes: 51 additions & 0 deletions tests/idat.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
libheif Item Data Box (idat) unit tests
MIT License
Copyright (c) 2023 Brad Hards <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "libheif/box.h"
#include <cstdint>
#include <iostream>

TEST_CASE("idat bad") {
std::vector<uint8_t> testData{0x00, 0x00, 0x00, 0x00, 'i',
'd', 'a', 't', 0x65};
auto reader = std::make_shared<StreamReader_memory>(testData.data(),
testData.size(), false);

BitstreamRange range(reader, testData.size());
for (;;) {
std::shared_ptr<Box> box;
Error error = Box::read(range, &box);
if (error != Error::Ok || range.error()) {
break;
}

box->get_type();
box->get_type_string();
Indent indent;
box->dump(indent);
}
}

0 comments on commit 32b316e

Please sign in to comment.