From 11350f11fd0a96e91bcbfbad30d0003cf8bae127 Mon Sep 17 00:00:00 2001 From: Lukasz Bardeli Date: Mon, 24 Feb 2020 11:08:21 +0100 Subject: [PATCH] [Filesystem] fix for preventing integer overflow during subtraction. Filesystem TCT passrate 100% Deprecated TCT passrate 100% Change-Id: Ia2bf2bd10d79cf5bfba1bc4597ab7b1b5c634b02 Signed-off-by: Lukasz Bardeli --- src/filesystem/filesystem_instance.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index 0e71a148..9855c502 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -336,8 +336,12 @@ static std::vector read_file(std::string path, long offset = 0, }; auto size = file_size(file); - if (offset < 0 || size <= (size_t)offset || - (0 != offset && 0 != std::fseek(file, offset, SEEK_SET))) { + if (offset < 0 || size < (size_t)offset) { + std::string err_msg = std::string("offset is smaller than zero or greater than size."); + throw std::system_error{EINVAL, std::generic_category(), err_msg}; + } + + if (0 != offset && 0 != std::fseek(file, offset, SEEK_SET)) { std::string err_msg = std::string("Cannot perform seek. ") + GetErrorString(errno); throw std::system_error{errno, std::generic_category(), err_msg}; } -- 2.34.1