-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
Current default implementation of __getstate__ and __setstate__ could be made safer #1004
Comments
Have you tried replacing the tuple by the dict and see how much it breaks? Performance difference should be benign, since we save ourselves zipping. |
I wanted to get some thoughts on the feasibility of making the change at all first. Let me put together a patch and see how much it breaks, and we can take it from there. Thanks :) |
I don't think our implementation of pickling is a public API. So if you can make it compatible, that should be OK. |
I think this should now have the right coverage, and all tests seem to pass so \o/ Let me know what you think :) Thanks! |
This is a known "sharp edge" of pickle, but attrs could make this a bit safer by slightly modifying the default implementation of
__getstate__
and__setstate__
. The problem is that due to returning a tuple in the default implementation of__getstate__
, removing a member, and then unpickling from a previous version can be very unsafe. This is not a hypothetical situation - it commonly happens when an object is pickled, and then stored in a data-store of some sort, and unpickled some time later by a changed version of the code.Here's a simple reproducer that demonstrates it (python 3.10, attrs 22.1.0):
While there's certainly an argument to be made that pickle should not be used for such things (and I'd agree) - the default implementation could, I believe, be made safer. Raise if things don't match, or even ignore unknown attributes in setstate, by potentially returning a dictionary instead of a tuple here would help.
The text was updated successfully, but these errors were encountered: