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

[BUG] Fix crashing on NaN value #382

Merged
merged 1 commit into from
Apr 12, 2021

Conversation

coreyd303
Copy link
Contributor

@coreyd303 coreyd303 commented Apr 9, 2021

Summary

Describe the goal of this PR. Mention any related Issue numbers.

This PR fixes a crash seen when the result of CGFloat(bounds.height - config.paddingInsets.top - config.paddingInsets.bottom) / CGFloat(requiredSpaceForEachLine) is 0

Applying round to 0 results in NaN which crashes the runtime when converting to Int

https://www.dropbox.com/s/u9id3amj27370bw/Screen%20Shot%202021-04-09%20at%203.04.05%20PM.png?dl=0

Steps to reproduce:

  • Skeletonize a text field or label
  • Set elements height to 0
  • call .showAnimatedGradientSkeleton() [or I am assuming any of the .show methods]

In our app we dynamically resize a text view depending if there is text within, it might be notable that the text view is embedded in a stackView, but I assume the crash would happen either way if the view size is 0.

Requirements (place an x in each of the [ ])

let calculatedNumberOfLines = Int(round(CGFloat(bounds.height - config.paddingInsets.top - config.paddingInsets.bottom) / CGFloat(requiredSpaceForEachLine)))

let neededLines = round(CGFloat(bounds.height - config.paddingInsets.top - config.paddingInsets.bottom) / CGFloat(requiredSpaceForEachLine))
guard !neededLines.isNaN else { return 0 }
Copy link
Owner

@Juanpe Juanpe Apr 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taking into account the crash description we should check if the value is finite as well. In fact, I think we could use the property isNormal. As we can see in the official doc, this property return false if the value is zero, infinite or NaN

I suggest you modify the guard condition:

Suggested change
guard !neededLines.isNaN else { return 0 }
guard neededLines.isNormal else { return 0 }

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Juanpe, I love this, wasn't aware of isNormal, it's a power move for sure and improves the human readability of the code, cheers.

@Juanpe
Copy link
Owner

Juanpe commented Apr 12, 2021

Thanks @coreyd303 for your first contribution!! 🎉 I've added a comment, please check it and let me know what do you think 🤙

It is possible for the rounded result to be NaN with both the needed lines and the padding values are 0.0, checking for NaN will prevent a crash
[part] change isNan to isNormal

isNormal provides a more robust check for unexpected values
@Juanpe Juanpe merged commit 36668f4 into Juanpe:main Apr 12, 2021
@Juanpe
Copy link
Owner

Juanpe commented Apr 12, 2021

Great! This PR will be included in the next version 🤙🏼 thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants