-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Don't panic when loading preferences with wrong type #4039
Conversation
A corrupted file may return a value not of the return type expected. The change forces the fallback value so a panic is averted
System would panic if the file was corrupt, this fixes the panics and returns the default value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I left a few comments on improving the variable naming. Most of the comments do apply in more places than where I added them (a lot of similar code in various functions).
One question for the other reviewers: Should we really keep the casting between float and int? At least for slices, this does seem like a very slow operation.
I forgot to add, would you mind giving the PR a more description name? |
The float/int casting is required because Go loads JSON ints as floats... |
Sure, but if all number lists are stored as slices of floats then surely all of our |
Ah yes maybe some of it could be avoided. As long as it doesn't introduce type issues between int/float in load/save happy to remove what we can. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, I think we need to resolve the casting discussion with @Jacalz
It is possible we missed a test case in the new number list code?... |
I did a quick test on develop and int lists do work fine on FYI: The code we have to casting over slices if incorrect for float in this PR. If we decide on keeping the casting, we never want to cast float to int as any possible decimal precision is rounded down. |
Agree with rounding, but is fallback better than return 1 rather than 1.0 I can change it. Let me know.... |
I think the rounding is a red-herring. Ints can be deserialised as floats, but they will have ".0" after the decimal place because they were ints in the first place... so if int cast is here to cope with JSON storage then there is no rounding to worry about? |
I have tested on linux, android and tests run as expected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. This is looking good. Sorry for the confusion with the casting. What you have now makes sense.
I've left two comments about removing some newlines that I think you might have accidentally added in. Apart from that, I think we need a test for the []int to []float slice cast because that wasn't there before. This should be good to go after that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested on linux and android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent to have more safety thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Description:
Fix panics in case of type change or file corruption
Checklist: