Avoid calling >= on unknown receivers in ls command#1193
Open
dejmedus wants to merge 1 commit intoruby:masterfrom
Open
Avoid calling >= on unknown receivers in ls command#1193dejmedus wants to merge 1 commit intoruby:masterfrom
>= on unknown receivers in ls command#1193dejmedus wants to merge 1 commit intoruby:masterfrom
Conversation
Previously, when calling comparison methods on receivers we don't control (`c >= Object`), there was a chance it could lack or have overridden the method. This commit flips them to prevent this `Object <= c`
tompng
reviewed
Mar 28, 2026
| ancestors = ancestors.reject { |c| c >= Object } if klass < Object | ||
| singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| c >= Class } | ||
| ancestors = ancestors.reject { |c| Object <= c } if klass < Object | ||
| singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| Class <= c } |
Member
There was a problem hiding this comment.
To solve the problem, the change looks good. However, ls foo ls Foo doesn't work when some other methods are missing:
<
constants
class_variables
to_s
ancestors
public_instance_methods
Unlike Object instances that we need to consider BasicObject, I think we can assume Classes doesn't undef/change most of these methods: constants, class_variables, to_s, ancestors, public_instance_methods.
But for <, I guess it might be removed and need to change if klass < Object to if Object > klass in the situation you are assuming.
Can you share what kind of situation :<= is removed? Will :> be removed too?
Do you know an existing gem that creates a class without :<=?
The correct fix depends on the situation what you are assuming, and needs to be commented in test code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If
Ls#dump_methodsis called with anobjwhose ancestors alter>=, the comparison (c >= Object/c >= Class) may raise an error. For example:To prevent this, we could defensively flip the comparison so that
<=is called on known receivers