Skip to content

PyObject.GetHashCode overflow bug #676

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

Closed
GSPP opened this issue May 26, 2018 · 4 comments
Closed

PyObject.GetHashCode overflow bug #676

GSPP opened this issue May 26, 2018 · 4 comments

Comments

@GSPP
Copy link

GSPP commented May 26, 2018

PyObject.GetHashCode() can throw an OverflowException because IntPtr.ToInt32() does throw. See

return Runtime.PyObject_Hash(obj).ToInt32();
. I suggest fixing this by xoring the high and low parts of that IntPtr together. Like this:

var ulongVal = (ulong)ptr;
return (int)((uint)ulongVal ^ (uint)(ulongVal >> 32));

This happened to me when inserting into a dictionary using tensorflow objects as keys:

new Dictionary<dynamic, dynamic>() { { x, 1.0 }, { y, 1.0 }, }

Environment

  • Pythonnet version: 2.3
  • Python version: 3.5
  • Operating System: Windows 7
@filmor
Copy link
Member

filmor commented May 26, 2018

Yes, please, can you prepare a PR for this?

@GSPP
Copy link
Author

GSPP commented May 26, 2018

I don't have the project set up. I can submit a blind untested PR if that helps.

@filmor
Copy link
Member

filmor commented May 26, 2018

Sure, go ahead. It's anyways tricky to write tests for this as you need both a 64bit system and reliably force allocation above 2^32 for this patch to make a difference.

@GSPP
Copy link
Author

GSPP commented May 27, 2018

The PR is there. Some build stuff failed. I have no idea what it means. I compiled and ran tests locally.

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

No branches or pull requests

2 participants