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

Sanitize some wallet serialization #12658

Merged
merged 2 commits into from Mar 13, 2018
Merged

Conversation

sipa
Copy link
Member

@sipa sipa commented Mar 10, 2018

This is a small subset of changes taken from #10785, fixing a few of the craziest constness violations in the serialization code.

CWalletTx currently serializes some of its fields by embedding them in a key-value mapValue, which is modified (and then fixed up) even from the Serialize method (for which mapValue is const). CAccountingEntry goes even further in that it stores such a map by appending it into strComment after a null char, which is again later fixed up again.

Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of mapValue / strComment.

@sipa sipa requested a review from ryanofsky March 10, 2018 01:38
@sipa sipa changed the title Sanitize some wallet serialize Sanitize some wallet serialization Mar 10, 2018
Copy link
Contributor

@eklitzke eklitzke left a comment

Choose a reason for hiding this comment

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

I'm not an expert in this part of the code, but this change looks good, and I'm happy to see so many red lines in the diff.

std::vector<CMerkleTx> vUnused; //!< Used to be vtxPrev
s >> vUnused >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> fSpent;

strFromAccount = std::move(mapValue["fromaccount"]);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Instead of move here and erase a few lines below, you can do the search one and localize the logic:

auto it = mapValue.find("fromaccount");
assert(it != mapValue.end());  // optional, if you feel like it
strFromAccount = std::move(it->second);
mapValue.erase(it);

One day in the glorious future when we're all using C++17 you can use .extract().

Copy link
Member Author

Choose a reason for hiding this comment

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

As this code isn't particularly performance critical, I prefer the current shorter code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense.

WriteOrderPos(nOrderPos, mapValueCopy);

std::string strCommentCopy = strComment;
if (!(mapValueCopy.empty() && _ssExtra.empty())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider making this:

if (!mapValueCopy.empty() || !_ssExtra.empty()) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@promag
Copy link
Member

promag commented Mar 13, 2018

utACK 42343c7 LGTM.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK 42343c7. Nice to make #10785 a little simpler.

//! Note: strAccount is serialized as part of the key, not here.
READWRITE(nCreditDebit);
READWRITE(nTime);
READWRITE(LIMITED_STRING(strOtherAccount, 65536));
Copy link
Contributor

Choose a reason for hiding this comment

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

Not being changed by this PR, but seems bad to me that length is not checked on serialization (only on deserialization).

Copy link
Member

Choose a reason for hiding this comment

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

Although you could add a sanity check, serialization is probably too late to check it. Ideally anything inputting account/label names should check this to be able to give an useful error and reject setting it before putting it in a data structure.
But yes, out of scope here.

@laanwj
Copy link
Member

laanwj commented Mar 13, 2018

utACK 42343c7

@laanwj laanwj merged commit 42343c7 into bitcoin:master Mar 13, 2018
laanwj added a commit that referenced this pull request Mar 13, 2018
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from #10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 9, 2020
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from bitcoin#10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 9, 2020
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from bitcoin#10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2020
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from bitcoin#10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2020
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from bitcoin#10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2020
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from bitcoin#10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2020
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from bitcoin#10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 12, 2020
42343c7 Split up and sanitize CAccountingEntry serialization (Pieter Wuille)
029ecac Split up and sanitize CWalletTx serialization (Pieter Wuille)

Pull request description:

  This is a small subset of changes taken from bitcoin#10785, fixing a few of the craziest constness violations in the serialization code.

  `CWalletTx` currently serializes some of its fields by embedding them in a key-value `mapValue`, which is modified (and then fixed up) even from the `Serialize` method (for which `mapValue` is const). `CAccountingEntry` goes even further in that it stores such a map by appending it into `strComment` after a null char, which is again later fixed up again.

  Fix this by splitting the serialization and deserialization code, and making the serialization act on a copy of `mapValue` / `strComment`.

Tree-SHA512: 487e04996dea6aba5b9b8bdaf2c4e680808f111a15afc557b8d078e14b01e4f40f8ef27588869be62f9a87052117c17e0a0c26c59150f83472a9076936af035e
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants