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 stack overflow in CoreDID PartialEq impl #946

Merged
merged 1 commit into from Jul 18, 2022

Conversation

PhilippGackstatter
Copy link
Contributor

@PhilippGackstatter PhilippGackstatter commented Jul 16, 2022

Description of change

Fixes an infinite recursion when comparing CoreDIDs. The following example currently causes a stack overflow:

let example1 = CoreDID::parse("did:example:0x0").unwrap();
let example2 = CoreDID::parse("did:example:0x1").unwrap();
println!("{}", example1 == &example2);

The Rust compiler ends up effectively calling:

<CoreDID as PartialEq<&CoreDID>>::eq(&example1, &&example2)

which recurses infinitely. The implementation is:

impl PartialEq<&CoreDID> for CoreDID {
  fn eq(&self, other: &&CoreDID) -> bool {
    self == other
  }
}

Two options for fixing the situation: 1) Using *other or 2) removing the impl. We don't use this implementation anywhere, and since this impl has been around for 9 months without anyone running into the problem, there's a good chance no one is using it. Note that this is an additional implementation next to the derived PartialEq impl on CoreDID. So this PR removes this impl. However, if we don't want this to be a breaking change or think it's a convenient impl, we can also go with option 1.

Edit: CI is failing due to a known ICE: rust-lang/rust#99261

Links to any relevant issues

n/a

Type of change

Add an x to the boxes that are relevant to your changes.

  • Bug fix (a non-breaking change which fixes an issue)
  • Enhancement (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Fix

How the change has been tested

Ran the above example before and after the change.

Change checklist

Add an x to the boxes that are relevant to your changes.

  • I have followed the contribution guidelines for this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@PhilippGackstatter PhilippGackstatter added Breaking change A change to the API that requires a major release. Part of "Changed" section in changelog Rust Related to the core Rust code. Becomes part of the Rust changelog. labels Jul 16, 2022
@PhilippGackstatter PhilippGackstatter added this to the v0.7 Features milestone Jul 16, 2022
Copy link
Contributor

@cycraig cycraig left a comment

Choose a reason for hiding this comment

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

Not sure why that PartialEq existed (my fault for adding it, just not sure why, apologies). It could have been from when unsafe was used to transmute transparent DID types but that's just a guess (probably not).

Thanks for fixing it! Fully agree to remove the impl, no loss of functionality since CoreDID still derives PartialEq.

@PhilippGackstatter PhilippGackstatter merged commit cfe789d into dev Jul 18, 2022
@PhilippGackstatter PhilippGackstatter deleted the fix/coredid-partialeq-stack-overflow branch July 18, 2022 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking change A change to the API that requires a major release. Part of "Changed" section in changelog Rust Related to the core Rust code. Becomes part of the Rust changelog.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants