Skip to content

Off-by-one error in goToPageWithRow() #372

@devrelm

Description

@devrelm

Specifically, in _alignRowIndex():

int _alignRowIndex(int rowIndex, int rowsPerPage) {
return ((rowIndex + 1) ~/ rowsPerPage) * rowsPerPage;
}

We'd expect that _alignRowIndex(9, 10) would return 0, but instead we get 10:

((rowIndex + 1) ~/ rowsPerPage) * rowsPerPage
= ((9 + 1) ~/ 10) * 10
= (10 ~/ 10) * 10
= 1 * 10
= 10

So attempting to go to the page containing the last row for that page actually puts us on the page after the one we'd expect.

My workaround — to preserve some sort of forward-compatibility once this is fixed — is to subtract 1 from rowIndex before passing it into goToPageWithRow(), but only if (rowIndex - 1) % rowsPerPage == 0. Basically: if (and only if) we're on the index that would cause the error, then we pretend we're on the index before it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions