vim-patch:9.0.2113: Coverity warns for another overflow in shift_line()

Problem:  Coverity warns for another overflow in shift_line()
Solution: Test for INT_MAX after the if condition, cast integer values
          to (long long) before multiplying.

22a97fc241

Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq 2024-12-10 13:38:35 +08:00
parent 2336389d23
commit ac230370f3

View File

@ -296,19 +296,19 @@ void shift_line(bool left, bool round, int amount, int call_changed_bytes)
} else {
i += amount;
}
count = i * sw_val;
count = (int64_t)i * (int64_t)sw_val;
} else { // original vi indent
if (left) {
count = MAX(count - sw_val * amount, 0);
count = MAX(count - (int64_t)sw_val * (int64_t)amount, 0);
} else {
if ((int64_t)sw_val * (int64_t)amount > INT_MAX - count) {
count = INT_MAX;
} else {
count += (int64_t)sw_val * (int64_t)amount;
}
count += (int64_t)sw_val * (int64_t)amount;
}
}
if (count > INT_MAX) {
count = INT_MAX;
}
// Set new indent
if (State & VREPLACE_FLAG) {
change_indent(INDENT_SET, (int)count, false, call_changed_bytes);