Skip to content

function currentPageIndex possible crash #2

@Minitour

Description

@Minitour

The function can throw an error if the width is equal to 0 - which may be possible in some cases.

So I replaced this:

private func currentPageIndex() -> Int {
        let width = self.frame.width
        var currentPageIndex = Int((contentSectionScrollView.contentOffset.x + (0.5 * width)) / width)
        if (currentPageIndex < 0) {
            currentPageIndex = 0
        } else if (currentPageIndex >= self.numberOfPages) {
            currentPageIndex = self.numberOfPages - 1
        }
        return currentPageIndex
    }

With this:

private func currentPageIndex() -> Int {
        let width = self.frame.width


        var currentPageIndex = 0
        if width != 0 {
           currentPageIndex =   Int(( contentSectionScrollView.contentOffset.x + (0.5 * width)) / width)
        } 


        if (currentPageIndex < 0) {
            currentPageIndex = 0
        } else if (currentPageIndex >= self.numberOfPages) {
            currentPageIndex = self.numberOfPages - 1
        }
        return currentPageIndex
    }

A simple validation to avoid division by zero.

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