Skip to content

Commit

Permalink
Avoid 32 bit overflow in multiplication (fixes 3 CodeQL CI alerts)
Browse files Browse the repository at this point in the history
The CodeQL CI reports "Multiplication result converted to larger type".

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed May 9, 2023
1 parent c9895db commit 6cb82d8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ccstruct/dppoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int64_t DPPoint::CostWithVariance(const DPPoint *prev) {
int delta = this - prev;
int32_t n = prev->n_ + 1;
int32_t sig_x = prev->sig_x_ + delta;
int64_t sig_xsq = prev->sig_xsq_ + delta * delta;
int64_t sig_xsq = prev->sig_xsq_ + static_cast<int64_t>(delta) * delta;
int64_t cost = (sig_xsq - sig_x * sig_x / n) / n;
cost += prev->total_cost_;
UpdateIfBetter(cost, prev->total_steps_ + 1, prev, n, sig_x, sig_xsq);
Expand Down
4 changes: 2 additions & 2 deletions src/training/pango/boxchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ bool BoxChar::MostlyVertical(const std::vector<BoxChar *> &boxes) {
int dx = boxes[i]->box_->x - boxes[i - 1]->box_->x;
int dy = boxes[i]->box_->y - boxes[i - 1]->box_->y;
if (abs(dx) > abs(dy) * kMinNewlineRatio || abs(dy) > abs(dx) * kMinNewlineRatio) {
total_dx += dx * dx;
total_dy += dy * dy;
total_dx += static_cast<int64_t>(dx) * dx;
total_dy += static_cast<int64_t>(dy) * dy;
}
}
}
Expand Down

0 comments on commit 6cb82d8

Please sign in to comment.