diff --git a/dep/libchdr/src/libchdr_chd.c b/dep/libchdr/src/libchdr_chd.c index 6ee62ce3a..bd06c53c8 100644 --- a/dep/libchdr/src/libchdr_chd.c +++ b/dep/libchdr/src/libchdr_chd.c @@ -89,6 +89,11 @@ #define CHD_V1_SECTOR_SIZE 512 /* size of a "sector" in the V1 header */ +#define CHD_MAX_HUNK_SIZE (128 * 1024 * 1024) /* hunk size probably shouldn't be more than 128MB */ + +/* we're currently only using this for CD/DVDs, if we end up with more than 10GB data, it's probably invalid */ +#define CHD_MAX_FILE_SIZE (10ULL * 1024 * 1024 * 1024) + #define COOKIE_VALUE 0xbaadf00d #define MAX_ZLIB_ALLOCS 64 @@ -2587,12 +2592,8 @@ static chd_error header_validate(const chd_header *header) return CHDERR_INVALID_PARAMETER; } - /* some basic size checks to prevent huge mallocs: hunk size probably shouldn't be more than 128MB */ - if (header->hunkbytes >= (128 * 1024 * 1024)) - return CHDERR_INVALID_PARAMETER; - - /* - we're currently only using this for CD/DVDs, if we end up with more than 10GB data, it's probably invalid */ - if (((uint64_t)header->hunkbytes * (uint64_t)header->totalhunks) >= (10ULL * 1024 * 1024 * 1024)) + /* some basic size checks to prevent huge mallocs */ + if (header->hunkbytes >= CHD_MAX_HUNK_SIZE || ((uint64_t)header->hunkbytes * (uint64_t)header->totalhunks) >= CHD_MAX_FILE_SIZE) return CHDERR_INVALID_PARAMETER; return CHDERR_NONE; diff --git a/src/util/cd_image_chd.cpp b/src/util/cd_image_chd.cpp index 42e575de6..94e3cbfce 100644 --- a/src/util/cd_image_chd.cpp +++ b/src/util/cd_image_chd.cpp @@ -453,8 +453,9 @@ CDImage::PrecacheResult CDImageCHD::Precache(ProgressCallback* progress) progress->SetProgressRange(100); auto callback = [](size_t pos, size_t total, void* param) { - const u32 percent = static_cast((pos * 100) / total); - static_cast(param)->SetProgressValue(std::min(percent, 100)); + constexpr size_t one_mb = 1048576; + static_cast(param)->SetProgressRange(static_cast((total + (one_mb - 1)) / one_mb)); + static_cast(param)->SetProgressValue(static_cast((pos + (one_mb - 1)) / one_mb)); }; if (chd_precache_progress(m_chd, callback, progress) != CHDERR_NONE)