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

Add missing nil guard in valid_hostname? #160

Merged
merged 1 commit into from
Nov 1, 2022

Conversation

dentarg
Copy link
Collaborator

@dentarg dentarg commented Oct 29, 2022

Regression from #158

Comment on lines +112 to 115
return false if hostname.nil?

# No need to check the TLD, the public suffix list does that
labels = hostname.split(DOT)[0...-1].map(&:to_s)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just some alternatives. I think current is just as good as them, but options are always fun!

To avoid work later on, not sure if hostname can ever be "" though:

Suggested change
return false if hostname.nil?
# No need to check the TLD, the public suffix list does that
labels = hostname.split(DOT)[0...-1].map(&:to_s)
return false if hostname.to_s.empty?
# No need to check the TLD, the public suffix list does that
labels = hostname.split(DOT)[0...-1].map(&:to_s)

or even

Suggested change
return false if hostname.nil?
# No need to check the TLD, the public suffix list does that
labels = hostname.split(DOT)[0...-1].map(&:to_s)
# No need to check the TLD, the public suffix list does that
labels = hostname.to_s.split(DOT)[0...-1].map(&:to_s)

I used to like the, latter, "benign value approach", but not sure I do these days :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought of doing to_s (and guard against the empty string) but opted not to because the code already handles empty string. Sure, avoiding unnecessary work could be good. I will leave it to someone else to profile if they want. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

We use to use guard clauses in situations like these. I'm fine with both the committed solution and the first suggestion here, so let's just go with what's already committed :D

Copy link
Contributor

@Pontus4 Pontus4 left a comment

Choose a reason for hiding this comment

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

👍

@dentarg
Copy link
Collaborator Author

dentarg commented Oct 31, 2022

A release with this fix would be great :)

@Pontus4 Pontus4 merged commit 390c2c7 into twingly:master Nov 1, 2022
@Pontus4
Copy link
Contributor

Pontus4 commented Nov 1, 2022

A release with this fix would be great :)

v7.0.1 🎉

@dentarg dentarg deleted the nil-guard branch November 1, 2022 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants