Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code]: There are redundant loop variables in Compare.move #154

Closed
0xOutOfGas opened this issue Sep 26, 2022 · 0 comments · Fixed by #190
Closed

[Code]: There are redundant loop variables in Compare.move #154

0xOutOfGas opened this issue Sep 26, 2022 · 0 comments · Fixed by #190
Labels
enhancement New feature or request
Milestone

Comments

@0xOutOfGas
Copy link

0xOutOfGas commented Sep 26, 2022

Framework version: v11
Description: In function Compare::cmp_bytes(), the loop variables i1and i2 are redundant.
Code Location: sources/Compare.move, line 57

public fun cmp_bytes(v1: &vector<u8>, v2: &vector<u8>): u8 {
    let l1 = Vector::length(v1);
    let l2 = Vector::length(v2);
    let len_cmp = cmp_u64(l1, l2);
    let i1 = 0;
    let i2 = 0;
    while (i1 < l1 && i2 < l2) {
        let elem_cmp = cmp_u8(*Vector::borrow(v1, i1), *Vector::borrow(v2, i2));
        if (elem_cmp != 0) {
            return elem_cmp
        };
        // else, compare next element
        i1 = i1 + 1;
        i2 = i2 + 1;
    };
    // all compared elements equal; use length comparison to break the tie
    len_cmp
}

Recommendation: Remove i2, and change *Vector::borrow(v2, i2) to *Vector::borrow(v2, i1). Also the comparison (elem_cmp != 0) can be changed to (elem_cmp != EQUAL) for clarity.

@0xOutOfGas 0xOutOfGas added the enhancement New feature or request label Sep 26, 2022
@jolestar jolestar added this to the v12 milestone Oct 11, 2022
@pause125 pause125 mentioned this issue Oct 29, 2022
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants