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

Check for overflow in IncrementAccountCounter(). #5849

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,11 @@ std::pair<UnifiedFullViewingKey, libzcash::AccountId> CWallet::GenerateNewUnifie
while (true) {
auto accountId = hdChain.GetAccountCounter();
auto generated = GenerateUnifiedSpendingKeyForAccount(accountId);
hdChain.IncrementAccountCounter();
auto account = hdChain.IncrementAccountCounter();
if (!account.has_value()) {
throw std::runtime_error(
"CWallet::GenerateNewUnifiedSpendingKey(): Already generated the maxiumum number of accounts (2^31 - 2) for this wallet's mnemonic phrase. Congratulations, you need to create a new wallet!");
}

if (generated.has_value()) {
// Update the persisted chain information
Expand Down
14 changes: 11 additions & 3 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "key.h"
#include "keystore.h"
#include "zcash/Address.hpp"
#include "zcash/address/zip32.h"

#include <list>
#include <stdint.h>
Expand Down Expand Up @@ -103,9 +104,16 @@ class CHDChain
return accountCounter;
}

void IncrementAccountCounter() {
// TODO: We should check for overflow somewhere and handle it.
accountCounter += 1;
/** Increments the account counter by 1 and returns it. Returns std::nullopt
* if the increment operation would cause an overflow. */
std::optional<uint32_t> IncrementAccountCounter() {
auto newAccountCounter = accountCounter + 1;
if (newAccountCounter >= libzcash::ZCASH_LEGACY_ACCOUNT) {
return std::nullopt;
} else {
accountCounter = newAccountCounter;
return newAccountCounter;
}
}

uint32_t GetLegacyTKeyCounter(bool external) {
Expand Down