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

Fix polymorphic compare on nullables. #2786

Merged
merged 3 commits into from
May 7, 2018

Conversation

cristianoc
Copy link
Collaborator

Polymorphic compare could crash when the second argument is null, as it would try to read the tag of null.
Added test where previously it would crash.

Polymorphic compare could crash when the second argument is null.
@@ -167,6 +167,8 @@ let unsafe_js_compare x y =
let rec caml_compare (a : Obj.t) (b : Obj.t) : int =
if a == b then 0 else
(*front and formoest, we do not compare function values*)
if a == (Obj.repr Js_null.empty) then -1 else
if b == (Obj.repr Js_null.empty) then 1 else
Copy link
Member

Choose a reason for hiding this comment

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

The a = null is caught, would not the case when b = null be moved to line 192 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's where I had it before. I've moved it here because caml_int_compare and caml_string_compare give inconsistent results when comparing with null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

E.g. caml_int_compare(-1,null) == -1 but caml_int_compare(1,null) == 1.

Copy link
Member

@bobzhang bobzhang May 6, 2018

Choose a reason for hiding this comment

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

I am a little confused why it is weird? -1 < null < 1?
Edit: maybe this is what you mean caml_int_compare (0, null) and caml_int_compare(null,0) is not consistent? shall we add such test case

Copy link
Member

Choose a reason for hiding this comment

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

For the record: line 175 seems fishy too
since string and null/undefined can have the same type: string Js.null, maybe we should add undefined check as well.

if a == null  then -1 else 
if b == null then 1 else
if a == undefined then -1 else
if b == undefined then 1 else

assuming null < undefined

@bobzhang bobzhang merged commit 919b378 into rescript-lang:master May 7, 2018
@cristianoc cristianoc deleted the fix_caml_compare branch May 8, 2018 10:06
@bobzhang
Copy link
Member

@cristianoc assuming null < undefined seems a bad idea, which implies Some Js.null < None, I am going to revert the order, hopefully it will not break anything

@cristianoc
Copy link
Collaborator Author

Good point now that we prioritize undefined. Yes it should not break anything.

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.

2 participants