[Filesystem] fix for preventing integer overflow during subtraction. 86/225786/2
authorLukasz Bardeli <l.bardeli@samsung.com>
Mon, 24 Feb 2020 10:08:21 +0000 (11:08 +0100)
committerLukasz Bardeli <l.bardeli@samsung.com>
Mon, 24 Feb 2020 12:15:09 +0000 (13:15 +0100)
Filesystem TCT passrate 100%
Deprecated TCT passrate 100%

Change-Id: Ia2bf2bd10d79cf5bfba1bc4597ab7b1b5c634b02
Signed-off-by: Lukasz Bardeli <l.bardeli@samsung.com>
src/filesystem/filesystem_instance.cc

index 0e71a14..9855c50 100644 (file)
@@ -336,8 +336,12 @@ static std::vector<std::uint8_t> 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};
   }