From 992031e8248c3b95cda23315bf390303c7c7955c Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Fri, 6 Jul 2018 14:54:37 +0200 Subject: [PATCH] Fix CID 1164702 (Untrusted value as argument) Signed-off-by: Stefan Weil --- src/ccutil/genericvector.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ccutil/genericvector.h b/src/ccutil/genericvector.h index 2c0c7a34..0a6238b8 100644 --- a/src/ccutil/genericvector.h +++ b/src/ccutil/genericvector.h @@ -945,9 +945,11 @@ bool GenericVector::Serialize(tesseract::TFile* fp) const { // If swap is true, assumes a big/little-endian swap is needed. template bool GenericVector::DeSerialize(bool swap, FILE* fp) { - int32_t reserved; + uint32_t reserved; if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false; if (swap) Reverse32(&reserved); + // Arbitrarily limit the number of elements to protect against bad data. + if (reserved > UINT16_MAX) return false; reserve(reserved); size_used_ = reserved; if (fread(data_, sizeof(T), size_used_, fp) != unsigned_size()) return false; @@ -959,15 +961,17 @@ bool GenericVector::DeSerialize(bool swap, FILE* fp) { } template bool GenericVector::DeSerialize(tesseract::TFile* fp) { - int32_t reserved; + uint32_t reserved; if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false; + // Arbitrarily limit the number of elements to protect against bad data. + if (reserved > UINT16_MAX) return false; reserve(reserved); size_used_ = reserved; return fp->FReadEndian(data_, sizeof(T), size_used_) == size_used_; } template bool GenericVector::SkipDeSerialize(tesseract::TFile* fp) { - int32_t reserved; + uint32_t reserved; if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false; return fp->FRead(nullptr, sizeof(T), reserved) == reserved; }