fix(lib): add saturating subtraction to prevent integer underflow

This commit is contained in:
Amaan Qureshi 2024-12-25 03:06:38 -05:00
parent 7ba0f297e5
commit f3d50f273b
3 changed files with 74 additions and 4 deletions

View file

@ -31,7 +31,7 @@ static inline Length length_add(Length len1, Length len2) {
static inline Length length_sub(Length len1, Length len2) {
Length result;
result.bytes = len1.bytes - len2.bytes;
result.bytes = (len1.bytes >= len2.bytes) ? len1.bytes - len2.bytes : 0;
result.extent = point_sub(len1.extent, len2.extent);
return result;
}

View file

@ -22,7 +22,7 @@ static inline TSPoint point_sub(TSPoint a, TSPoint b) {
if (a.row > b.row)
return point__new(a.row - b.row, a.column);
else
return point__new(0, a.column - b.column);
return point__new(0, (a.column >= b.column) ? a.column - b.column : 0);
}
static inline bool point_lte(TSPoint a, TSPoint b) {