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 byteswap Fingerprints when encoding #42335

Merged
merged 1 commit into from Jun 3, 2017

Conversation

jcowgill
Copy link
Contributor

Byteswapping Fingerprints when encoding is unnessesary and breaks if the Fingerprint is later decoded on a machine with different endianness to the one it was encoded on.

Fixes #42239

This PR fixes a regression caused by #42082. @michaelwoerister

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @eddyb (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

let _0 = u64::from_le(d.read_u64()?);
let _1 = u64::from_le(d.read_u64()?);
let _0 = d.read_u64()?;
let _1 = d.read_u64()?;
Ok(Fingerprint(_0, _1))
Copy link
Member

Choose a reason for hiding this comment

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

Can you move it to use #[derive]? Looks like it's what deriving would produce, now.

@eddyb
Copy link
Member

eddyb commented May 31, 2017

r? @michaelwoerister

@michaelwoerister
Copy link
Member

Thanks for the PR! I'll review it as soon as I find the time.

@nagisa
Copy link
Member

nagisa commented May 31, 2017

Byteswapping Fingerprints when encoding is unnessesary and breaks if the Fingerprint is later decoded on a machine with different endianness to the one it was encoded on.

This explanation doesn’t exactly make any sense to me. It seems to me like going from machine specific to low endian in encoded form to machine specific after decoding is in fact the correct way to do this.

Maybe instead it is a problem with the comment just below that says:

The bytes returned bytes the Blake2B hasher are always little-endian.

or something like that?

@aidanhs aidanhs added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 1, 2017
@est31
Copy link
Member

est31 commented Jun 1, 2017

This explanation doesn’t exactly make any sense to me. It seems to me like going from machine specific to low endian in encoded form to machine specific after decoding is in fact the correct way to do this.

Not if Decoder::read_u64 and Encoder::emit_u64 already do the host to network conversion themselves. And apparently they do precisely that (that function is used by a macro in the implementation of read_u64).

So if you are doing a superfluous conversion host <-> little endian, it won't mean any problems if you serialize and deserialize on the same host, but if you serialize on a host with one byte order, and deserialize on a host with another, you will get the order mixed up.

Maybe instead it is a problem with the comment just below that says:

Nope, thats a different thing. The comment is correct, we actually encode to little endian. Admittedly, the code smells bad, it shouldn't have to use unsafe, but rather something safe with (hopefully) equivalent performance. That's a different issue though.

@nagisa nagisa added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 1, 2017
@eddyb eddyb added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 1, 2017
@michaelwoerister
Copy link
Member

Thanks again, @jcowgill!

Some comments:

  • In general it is definitely better to not do any endianess conversion in these encode/decode methods. The Encoder implementation should take care of that (and the OpaqueEncoder does by storing numbers in the endian-independent leb128 format). I overlooked that when introducing this change.
  • @eddyb is right, we could just add RustcEncodable, RustcDecodable to Fingerprint at this point.
  • It's a bit strange that byte-swapping should break anything here, but here is what's going on (using 291u16 as an example):
    • If HOST is big-endian we encode 291 == 0x01 0x23 to 8961 = 0x23 0x01 and that to leb128(8961).
      • If decoding on big-endian again we convert leb128(8961) to 8961 = 0x23 0x01 and then to 291 = 0x01 0x23 by swapping the bytes again.
      • If decoding on little-endian, we convert leb128(8961) to 8961 = 0x01 0x23 (!) and then we are not swapping bytes, ending up with the wrong value.

Little- to big-endian host is analogous.

So this gets an r+ from me once @eddyb's suggestion is implemented.

Byteswapping Fingerprints when encoding is unnessesary and breaks if
the Fingerprint is later decoded on a machine with different endianness
to the one it was encoded on. Fix by removing the Encodable and
Decodable implementations and use the ones derived from RustcEncodable
and RustcDecodable.

Fixes rust-lang#42239
@jcowgill
Copy link
Contributor Author

jcowgill commented Jun 1, 2017

Hi, I've now changed the fix to use RustcEncodable and RustcDecodable. I just had to make sure things were still working :)

@eddyb
Copy link
Member

eddyb commented Jun 1, 2017

@bors r=michaelwoerister

@bors
Copy link
Contributor

bors commented Jun 1, 2017

📌 Commit edefcb2 has been approved by michaelwoerister

@michaelwoerister
Copy link
Member

🎉

@bors
Copy link
Contributor

bors commented Jun 1, 2017

⌛ Testing commit edefcb2 with merge e776af7...

@bors
Copy link
Contributor

bors commented Jun 1, 2017

💔 Test failed - status-travis

@Mark-Simulacrum
Copy link
Member

@bors retry

[00:09:29] curl: (56) SSLRead() return error -36

@Mark-Simulacrum
Copy link
Member

@bors rollup

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jun 1, 2017
…oerister

Don't byteswap Fingerprints when encoding

Byteswapping Fingerprints when encoding is unnessesary and breaks if the Fingerprint is later decoded on a machine with different endianness to the one it was encoded on.

Fixes rust-lang#42239

This PR fixes a regression caused by rust-lang#42082. @michaelwoerister
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jun 1, 2017
…oerister

Don't byteswap Fingerprints when encoding

Byteswapping Fingerprints when encoding is unnessesary and breaks if the Fingerprint is later decoded on a machine with different endianness to the one it was encoded on.

Fixes rust-lang#42239

This PR fixes a regression caused by rust-lang#42082. @michaelwoerister
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jun 2, 2017
…oerister

Don't byteswap Fingerprints when encoding

Byteswapping Fingerprints when encoding is unnessesary and breaks if the Fingerprint is later decoded on a machine with different endianness to the one it was encoded on.

Fixes rust-lang#42239

This PR fixes a regression caused by rust-lang#42082. @michaelwoerister
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jun 2, 2017
…oerister

Don't byteswap Fingerprints when encoding

Byteswapping Fingerprints when encoding is unnessesary and breaks if the Fingerprint is later decoded on a machine with different endianness to the one it was encoded on.

Fixes rust-lang#42239

This PR fixes a regression caused by rust-lang#42082. @michaelwoerister
bors added a commit that referenced this pull request Jun 2, 2017
Rollup of 10 pull requests

- Successful merges: #41981, #42225, #42310, #42319, #42335, #42343, #42355, #42360, #42370, #42372
- Failed merges:
bors added a commit that referenced this pull request Jun 2, 2017
Rollup of 10 pull requests

- Successful merges: #41981, #42225, #42310, #42319, #42335, #42343, #42355, #42360, #42370, #42372
- Failed merges:
@bors bors merged commit edefcb2 into rust-lang:master Jun 3, 2017
@jcowgill jcowgill deleted the fingerprint-be branch January 30, 2018 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mips (big endian) gives "undefined reference to `std::rt::lang_start'"
9 participants