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

Don't panic when loading preferences with wrong type #4039

Merged
merged 9 commits into from
Jul 25, 2023

Conversation

nh3000-org
Copy link
Contributor

@nh3000-org nh3000-org commented Jul 8, 2023

Description:

Fix panics in case of type change or file corruption

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

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.
Copy link
Member

@Jacalz Jacalz left a 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.

internal/preferences.go Outdated Show resolved Hide resolved
internal/preferences.go Outdated Show resolved Hide resolved
internal/preferences.go Outdated Show resolved Hide resolved
internal/preferences.go Show resolved Hide resolved
@Jacalz
Copy link
Member

Jacalz commented Jul 8, 2023

I forgot to add, would you mind giving the PR a more description name?

@andydotxyz
Copy link
Member

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.

The float/int casting is required because Go loads JSON ints as floats...

@Jacalz
Copy link
Member

Jacalz commented Jul 9, 2023

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 IntList() methods that we have would not have worked before this PR?

@andydotxyz
Copy link
Member

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.

@coveralls
Copy link

coveralls commented Jul 9, 2023

Coverage Status

coverage: 66.2% (+0.05%) from 66.149% when pulling ee5c3bc on nh3000-org:develop into e050d79 on fyne-io:develop.

@nh3000-org nh3000-org changed the title Develop Eliminate panics Jul 9, 2023
@Jacalz Jacalz changed the title Eliminate panics Don't panic when loading preferenceswith wrong type Jul 9, 2023
@Jacalz Jacalz changed the title Don't panic when loading preferenceswith wrong type Don't panic when loading preferences with wrong type Jul 9, 2023
Copy link
Member

@andydotxyz andydotxyz left a 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

@andydotxyz
Copy link
Member

Sure, but if all number lists are stored as slices of floats then surely all of our IntList() methods that we have would not have worked before this PR?

It is possible we missed a test case in the new number list code?...

@Jacalz
Copy link
Member

Jacalz commented Jul 11, 2023

I did a quick test on develop and int lists do work fine on develop without any casts from float in the slices we return. Do we really want to do the costly translation then? I'd be with it just returning the fallback if I try to load a FloatList() when I have set an IntList() on that key. It seems better to just make the developer understand that they used the wrong ket rather than allocating a new slice and copying over all the values.

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.

@nh3000-org
Copy link
Contributor Author

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....

@andydotxyz
Copy link
Member

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?

@nh3000-org
Copy link
Contributor Author

I have tested on linux, android and tests run as expected.

Copy link
Member

@Jacalz Jacalz left a 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 :)

internal/preferences_test.go Outdated Show resolved Hide resolved
internal/preferences.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@nh3000-org nh3000-org left a 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.

@nh3000-org nh3000-org requested a review from Jacalz July 24, 2023 14:31
Copy link
Member

@andydotxyz andydotxyz left a 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 :)

Copy link
Member

@Jacalz Jacalz left a comment

Choose a reason for hiding this comment

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

Thanks

@Jacalz Jacalz merged commit 0b35b8e into fyne-io:develop Jul 25, 2023
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.

4 participants