Skip to content

Commit 33b1471

Browse files
authored
Merge pull request #38 from saxbophone/josh/37-mulitiplication-bug
Cast operands of intermediates in multiplication up to bigger type
2 parents d717430 + edf3d28 commit 33b1471

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

arby/include/arby/arby.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ namespace com::saxbophone::arby {
378378
// multiply each digit from lhs with each digit from rhs
379379
for (std::size_t l = 0; l < lhs._digits.size(); l++) {
380380
for (std::size_t r = 0; r < rhs._digits.size(); r++) {
381-
OverflowType multiplication = lhs._digits[l] * rhs._digits[r];
381+
// cast lhs to OverflowType to make sure both operands get promoted to avoid wrap-around overflow
382+
OverflowType multiplication = (OverflowType)lhs._digits[l] * rhs._digits[r];
382383
// create a new Uint with this intermediate result and add trailing places as needed
383384
Uint intermediate = multiplication;
384385
// we need to remap the indices as the digits are stored big-endian

0 commit comments

Comments
 (0)