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

🐛 zv: Allow f32::NAN to be decoded too #1148

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

Spindel
Copy link
Contributor

@Spindel Spindel commented Nov 17, 2024

The range check would fail on NAN, INFINITY and NEG_INFINITY even if they are completely valid things to decode.

This does not promise that an encoded NAN will be bit-wise identical as the decoded NAN, but if signalling NAN is used that precisely the user can have a handful of more interesting problems.

Fixes #1145.

Copy link
Contributor

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

Thanks! Is this WIP? I don't see any fixes in the PR.

@Spindel
Copy link
Contributor Author

Spindel commented Nov 17, 2024

Thanks! Is this WIP? I don't see any fixes in the PR.

Should be a Fixes, Did I mess up and assign it only as an Issue: ref rather than Fixes ref? Meh. I'll rewrite the commit msg and force push if you don't have any other review comments.

@Spindel Spindel force-pushed the zvariant-nan-decoders branch from 338ca32 to 447eac5 Compare November 17, 2024 19:26
@zeenix
Copy link
Contributor

zeenix commented Nov 17, 2024

Should be a Fixes, Did I mess up and assign it only as an Issue: ref rather than Fixes ref? Meh. I'll rewrite the commit msg and force push if you don't have any other review comments.

I'm not referring to the wording. I don't see any code changes except addition of an assertion and a test case. Please check your changes. Perhaps you forgot to commit something?

@Spindel
Copy link
Contributor Author

Spindel commented Nov 18, 2024

Should be a Fixes, Did I mess up and assign it only as an Issue: ref rather than Fixes ref? Meh. I'll rewrite the commit msg and force push if you don't have any other review comments.

I'm not referring to the wording. I don't see any code changes except addition of an assertion and a test case. Please check your changes. Perhaps you forgot to commit something?

That assert change is really all there is to it, as it treated NAN values as "too big" to transform, causing an panic when de-serializing an NAN value. Try it yourself with the assert change reverted and you'll see the test-case fails.

@zeenix
Copy link
Contributor

zeenix commented Nov 18, 2024

That assert change is really all there is to it, as it treated NAN values as "too big" to transform, causing an panic when de-serializing an NAN value

Why not error out instead? I don't think assert is a solution.

@Spindel
Copy link
Contributor Author

Spindel commented Nov 18, 2024

That assert change is really all there is to it, as it treated NAN values as "too big" to transform, causing an panic when de-serializing an NAN value

Why not error out instead? I don't think assert is a solution.

Cause the the code already used assert's and I didn't feel that a drive-by bug fix should change the behaviour that much.

@zeenix
Copy link
Contributor

zeenix commented Nov 18, 2024

Cause the the code already used assert's and I didn't feel that a drive-by bug fix should change the behaviour that much.

It's not a behaviour change IMO. asserts are for invariants that must always hold. Clearly, that is not the case here.

@Spindel
Copy link
Contributor Author

Spindel commented Nov 18, 2024

Wrapped up that change and folded the function into the deserializer.

usize_to_u8: Signature string converters seem to have some asserts on the length of them where they shouldn't be more than u8, but that seems mostly reasonable that the code will panic on when serializing.

The asserts in usize_to_u32 seems sane as noone should ever serialize more than 32bits worth of data at once.

Copy link
Contributor

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

Now we're talking. :)

zvariant/src/utils.rs Outdated Show resolved Hide resolved
zvariant/src/dbus/de.rs Show resolved Hide resolved
@zeenix
Copy link
Contributor

zeenix commented Nov 19, 2024

Thank you for your contribution! 🙏 We hope you have read our contribution guideline and followed it to the best of your abilities: https://github.com/dbus2/zbus/blob/main/CONTRIBUTING.md#submitting-pull-requests

As per those guidelines, please add an emoji prefix to the commits/PR title. You can just c&p it from here.

The range check would fail on NAN, INFINITY and NEG_INFINITY even if
they are completely valid things to decode.

This does not promise that an encoded NAN will be bit-wise identical as
the decoded NAN, but if signalling NAN is used that precisely the user
can have a handful of more interesting problems.

As the decoding of an invalid f64=>f32 shouldn't be a panic, but a
proper error, it means that f64_to_be can better be folded into the code
in the deserialiser.

Since this is floating point, we may also have fun rounding errors as an
f64 may be too _small_ to be represented in an f32 and we may thus have
precision loss as well, that seems annoying to deal with so hopefully
whomever notices that in the future will have a good time.

As I looked at the code, it seems usize => u32/u8 will have the same
"crash the program" kind of conversions in them as well.

Fixes: dbus2#1145
@Spindel Spindel force-pushed the zvariant-nan-decoders branch from fbb6e0c to aac2ac9 Compare December 2, 2024 22:12
@Spindel
Copy link
Contributor Author

Spindel commented Dec 2, 2024

Emojis added, commits squashed. If there are more changes, you're welcome to do them.

@zeenix zeenix changed the title zv: Allow f32::NAN to be decoded too 🐛 zv: Allow f32::NAN to be decoded too Dec 3, 2024
@zeenix zeenix enabled auto-merge December 3, 2024 15:50
@zeenix zeenix merged commit 4cc9526 into dbus2:main Dec 3, 2024
7 checks passed
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.

Zvariant asserts on f32::NAN values
2 participants