Skip to content

Commit 0009c83

Browse files
committed
intn.c: fix: normalize len before performing intn_cmp
Integers length is compared before comparing digits. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent d50da15 commit 0009c83

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/libAtomVM/intn.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,18 +434,19 @@ size_t intn_divmnu(const intn_digit_t m[], size_t m_len, const intn_digit_t n[],
434434
return padded_q_len / UINT16_IN_A_DIGIT;
435435
}
436436

437-
// This function assumes no leading zeros (lenght is used in comparison)
438-
// Caller must ensure this precondition
439437
int intn_cmp(const intn_digit_t a[], size_t a_len, const intn_digit_t b[], size_t b_len)
440438
{
441-
if (a_len > b_len) {
439+
size_t normal_a_len = intn_count_digits(a, a_len);
440+
size_t normal_b_len = intn_count_digits(b, b_len);
441+
442+
if (normal_a_len > normal_b_len) {
442443
return 1;
443444
}
444-
if (a_len < b_len) {
445+
if (normal_a_len < normal_b_len) {
445446
return -1;
446447
}
447448

448-
for (size_t i = a_len; i > 0; i--) {
449+
for (size_t i = normal_a_len; i > 0; i--) {
449450
if (a[i - 1] > b[i - 1]) {
450451
return 1;
451452
}

0 commit comments

Comments
 (0)