Skip to content

Conversation

@furszy
Copy link

@furszy furszy commented Mar 21, 2021

Back ported some pretty concise PRs from upstream over the data serialization area.

gmaxwell and others added 7 commits March 20, 2021 22:30
We don't normally use ReadVarInt from untrusted inputs, but we might
 see this in the case of corruption.

This is exposed in test_bitcoin_fuzzy.
Missing back port from btc@8c1dbc5e9ddbafb77e60e8c4e6eb275a3a76ac12
Currently, the READWRITE macro cannot be passed any non-const temporaries, as
the SerReadWrite function only accepts lvalue references.

Deserializing into a temporary is very common, however. See for example
things like 's >> VARINT(n)'. The VARINT macro produces a temporary wrapper
that holds a reference to n.

Fix this by accepting non-const rvalue references instead of lvalue references.
We don't propagate the rvalue-ness down, as there are no useful optimizations
that only apply to temporaries.

Then use this new functionality to get rid of many (but not all) uses of the
'REF' macro (which casts away constness).
Using VARINT with signed types is dangerous because negative values will appear
to serialize correctly, but then deserialize as positive values mod 128.

This commit changes the VARINT macro to trigger an error by default if called
with an signed value, and updates broken uses of VARINT to pass a special flag
that lets them keep working with no change in behavior.
Switch to unsigned encoding, which is backwards compatible and avoids MSVC
error reported bitcoin#12732
This adds a READWRITEAS(type, obj) macro which serializes obj as if it
were casted to (const type&) when const, and to (type&) when non-const.

This makes it usable in serialization code that uses a single
implementation for both serialization and deserializing, which doesn't
know the constness of the object involved.
@furszy furszy self-assigned this Mar 21, 2021
@furszy furszy force-pushed the 2021_serialization_updates branch from ebdcef7 to 172fe15 Compare March 21, 2021 16:43
@random-zebra random-zebra added this to the 6.0.0 milestone Mar 25, 2021
Copy link

@random-zebra random-zebra left a comment

Choose a reason for hiding this comment

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

ACK 172fe15

Copy link
Collaborator

@Fuzzbawls Fuzzbawls left a comment

Choose a reason for hiding this comment

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

ACK 172fe15

@furszy furszy merged commit a87f859 into PIVX-Project:master Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants