|
19 | 19 | #include <sys/types.h> |
20 | 20 | #include <unistd.h> |
21 | 21 |
|
| 22 | +#include <c10/util/safe_numerics.h> |
22 | 23 | #include <executorch/runtime/core/error.h> |
23 | 24 | #include <executorch/runtime/core/result.h> |
24 | 25 | #include <executorch/runtime/platform/log.h> |
@@ -157,10 +158,12 @@ Result<FreeableBuffer> FileDescriptorDataLoader::load( |
157 | 158 | fd_ >= 0, |
158 | 159 | InvalidState, |
159 | 160 | "Uninitialized"); |
| 161 | + size_t total_size; |
| 162 | + bool overflow = c10::add_overflows(offset, size, &total_size); |
160 | 163 | ET_CHECK_OR_RETURN_ERROR( |
161 | | - offset + size <= file_size_, |
| 164 | + !overflow && total_size <= file_size_, |
162 | 165 | InvalidArgument, |
163 | | - "File %s: offset %zu + size %zu > file_size_ %zu", |
| 166 | + "File %s: offset %zu + size %zu > file_size_ %zu, or overflow detected", |
164 | 167 | file_descriptor_uri_, |
165 | 168 | offset, |
166 | 169 | size, |
@@ -218,10 +221,12 @@ ET_NODISCARD Error FileDescriptorDataLoader::load_into( |
218 | 221 | fd_ >= 0, |
219 | 222 | InvalidState, |
220 | 223 | "Uninitialized"); |
| 224 | + size_t total_size; |
| 225 | + bool overflow = c10::add_overflows(offset, size, &total_size); |
221 | 226 | ET_CHECK_OR_RETURN_ERROR( |
222 | | - offset + size <= file_size_, |
| 227 | + !overflow && total_size <= file_size_, |
223 | 228 | InvalidArgument, |
224 | | - "File %s: offset %zu + size %zu > file_size_ %zu", |
| 229 | + "File %s: offset %zu + size %zu > file_size_ %zu, or overflow detected", |
225 | 230 | file_descriptor_uri_, |
226 | 231 | offset, |
227 | 232 | size, |
|
0 commit comments