Skip to content

Commit e15ca5f

Browse files
committed
resource: error out if reserve_additional_memory is too large
there is chance that configured reserve_additional_memory exceeds total_memory. as both of them can be configured at runtime either programtically or using command line option or config file. so - if the former is greater than the latter, we could have integer underflow, in that case `needed_memory` could be a very large number, and `needed_memory > available_memory` is likely to be evaluated to true, and the thrown error still sends the right message. but this hurts the readability. - if reserve_additional_memory is a very large number, like size_t(-1), we will also have integer underflow, but `needed_memory` would be `c.total_memory + 1`, which still can be greater than `available_memory`, and no exceptions are thrown. this is not expected. we should have warn user that the reserve_additional_memory is too large. in this change, before calculating the `needed_memory`, `c.total_memory` is compared with `c.reserve_additional_memory`, exception is thrown if the former is less than the latter. Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
1 parent 5a6ecdf commit e15ca5f

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

src/core/resource.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ size_t calculate_memory(const configuration& c, size_t available_memory, float p
223223
if (!c.total_memory.has_value()) {
224224
return available_memory;
225225
}
226+
if (*c.total_memory < c.reserve_additional_memory) {
227+
throw std::runtime_error(format("insufficient total memory: reserve {} total {}", c.reserve_additional_memory, *c.total_memory));
228+
}
226229
size_t needed_memory = *c.total_memory - c.reserve_additional_memory;
227230
if (needed_memory > available_memory) {
228231
throw std::runtime_error(format("insufficient physical memory: needed {} available {}", needed_memory, available_memory));

0 commit comments

Comments
 (0)