From a2bd8a85a51908136d8e6b1f73770271d67d8f30 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Mon, 3 Jun 2024 03:26:59 +0900 Subject: [PATCH] Fix build for Android AssetManager target(TINYUSDZ_ANDROID_LOAD_FROM_ASSETS) --- src/io-util.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/io-util.cc b/src/io-util.cc index 20e3d45d..6ce44b3a 100644 --- a/src/io-util.cc +++ b/src/io-util.cc @@ -278,14 +278,23 @@ bool ReadWholeFile(std::vector *out, std::string *err, } return false; } - size_t size = AAsset_getLength(asset); - if (size == 0) { + off_t len = AAsset_getLength(asset); + if (len <= 0) { if (err) { (*err) += "Invalid file size : " + filepath + " (does the path point to a directory?)"; } return false; } + size_t size = size_t(len); + + if (size >= filesize_max) { + (*err) += "File size exceeds filesize_max : " + filepath + + " (filesize_max " + std::to_string(filesize_max) + ")"; + + return false; + } + out->resize(size); AAsset_read(asset, reinterpret_cast(&out->at(0)), size); AAsset_close(asset); @@ -400,8 +409,8 @@ bool ReadFileHeader(std::vector *out, std::string *err, } return false; } - size_t size = AAsset_getLength(asset); - if (size == 0) { + off_t len = AAsset_getLength(asset); + if (len <= 0) { if (err) { (*err) += "Invalid file size : " + filepath + " (does the path point to a directory?)"; @@ -409,6 +418,8 @@ bool ReadFileHeader(std::vector *out, std::string *err, return false; } + size_t size = size_t(len); + size = (std::min)(size_t(max_read_bytes), size); out->resize(size); AAsset_read(asset, reinterpret_cast(&out->at(0)), size);