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

improvements #100

Open
wants to merge 3 commits into
base: master_candidate
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arith_uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ unsigned int base_uint<BITS>::bits() const
for (int pos = WIDTH - 1; pos >= 0; pos--) {
if (pn[pos]) {
for (int nbits = 31; nbits > 0; nbits--) {
if (pn[pos] & 1 << nbits)
if (pn[pos] & 1U << nbits) // 1U instead of 1 to avoid Shifting a signed 32-bit value by 31 bits
return 32 * pos + nbits + 1;
}
return 32 * pos + 1;
Expand Down
6 changes: 3 additions & 3 deletions src/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class base_uint
base_uint ret;
for (int i = 0; i < WIDTH; i++)
ret.pn[i] = ~pn[i];
ret++;
++ret; // no need to use post-increment when pre-increment is available for this class
return ret;
}

Expand Down Expand Up @@ -177,7 +177,7 @@ class base_uint
{
// prefix operator
int i = 0;
while (++pn[i] == 0 && i < WIDTH - 1)
while (i < WIDTH - 1 && ++pn[i] == 0) // don't use i as an index before checking we are within limits
i++;
return *this;
}
Expand All @@ -194,7 +194,7 @@ class base_uint
{
// prefix operator
int i = 0;
while (--pn[i] == (uint32_t)-1 && i < WIDTH - 1)
while (i < WIDTH - 1 && --pn[i] == (uint32_t)-1) // don't use i as an index before checking we are within limits
i++;
return *this;
}
Expand Down
14 changes: 7 additions & 7 deletions src/core_memusage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static inline size_t RecursiveDynamicUsage(const CTxOut& out) {

static inline size_t RecursiveDynamicUsage(const CScriptWitness& scriptWit) {
size_t mem = memusage::DynamicUsage(scriptWit.stack);
for (std::vector<std::vector<unsigned char> >::const_iterator it = scriptWit.stack.begin(); it != scriptWit.stack.end(); it++) {
for (std::vector<std::vector<unsigned char> >::const_iterator it = scriptWit.stack.begin(); it != scriptWit.stack.end(); ++it) { // no need to use post-increment when pre-increment is available
mem += memusage::DynamicUsage(*it);
}
return mem;
Expand All @@ -41,37 +41,37 @@ static inline size_t RecursiveDynamicUsage(const CTxInWitness& txinwit) {

static inline size_t RecursiveDynamicUsage(const CTxWitness& txwit) {
size_t mem = memusage::DynamicUsage(txwit.vtxinwit);
for (std::vector<CTxInWitness>::const_iterator it = txwit.vtxinwit.begin(); it != txwit.vtxinwit.end(); it++) {
for (std::vector<CTxInWitness>::const_iterator it = txwit.vtxinwit.begin(); it != txwit.vtxinwit.end(); ++it) { // no need to use post-increment when pre-increment is available
mem += RecursiveDynamicUsage(*it);
}
return mem;
}

static inline size_t RecursiveDynamicUsage(const CTransaction& tx) {
size_t mem = memusage::DynamicUsage(tx.vin) + memusage::DynamicUsage(tx.vout) + RecursiveDynamicUsage(tx.wit);
for (std::vector<CTxIn>::const_iterator it = tx.vin.begin(); it != tx.vin.end(); it++) {
for (std::vector<CTxIn>::const_iterator it = tx.vin.begin(); it != tx.vin.end(); ++it) { // no need to use post-increment when pre-increment is available
mem += RecursiveDynamicUsage(*it);
}
for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); it++) {
for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); ++it) { // no need to use post-increment when pre-increment is available
mem += RecursiveDynamicUsage(*it);
}
return mem;
}

static inline size_t RecursiveDynamicUsage(const CMutableTransaction& tx) {
size_t mem = memusage::DynamicUsage(tx.vin) + memusage::DynamicUsage(tx.vout) + RecursiveDynamicUsage(tx.wit);
for (std::vector<CTxIn>::const_iterator it = tx.vin.begin(); it != tx.vin.end(); it++) {
for (std::vector<CTxIn>::const_iterator it = tx.vin.begin(); it != tx.vin.end(); ++it) { // no need to use post-increment when pre-increment is available
mem += RecursiveDynamicUsage(*it);
}
for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); it++) {
for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); ++it) { // no need to use post-increment when pre-increment is available
mem += RecursiveDynamicUsage(*it);
}
return mem;
}

static inline size_t RecursiveDynamicUsage(const CBlock& block) {
size_t mem = memusage::DynamicUsage(block.vtx);
for (std::vector<CTransaction>::const_iterator it = block.vtx.begin(); it != block.vtx.end(); it++) {
for (std::vector<CTransaction>::const_iterator it = block.vtx.begin(); it != block.vtx.end(); ++it) { // no need to use post-increment when pre-increment is available
mem += RecursiveDynamicUsage(*it);
}
return mem;
Expand Down
1 change: 1 addition & 0 deletions src/crypto/lyra2/Lyra2.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *sa
//Allocates pointers to each row of the matrix
uint64_t **memMatrix = malloc(sizeof(uint64_t*) * nRows);
if (memMatrix == NULL) {
free(wholeMatrix); //stop a cheeky mem leak
return -1;
}
//Places the pointers in the correct positions
Expand Down
2 changes: 1 addition & 1 deletion src/cryptopp/validat1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ bool TestNIST_DRBG()
"\xF2\x86\xE4\xED\x74\xF2\x5D\x8B\x6C\x4D\xB8\xDE\xD8\x4A\xD6\x5E\xD6\x6D\xAE\xB1"
"\x1B\xA2\x94\x52\x54\xAD\x3C\x3D\x25\xBD\x12\x46\x3C\xA0\x45\x9D";

fail = !!memcmp(result, expected, 2048/8);
fail = !!memcmp(result, expected, sizeof(expected)); // Stop buffer from accessing out of bounds
pass = !fail && pass;

cout << (fail ? "FAILED " : "passed ") << "Hash_DRBG SHA512/256/888 (C0UNT=0, E=32, N=16, A=32, P=32)" << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class CBlock : public CBlockHeader
CBlock(const CBlockHeader &header)
{
SetNull();
*((CBlockHeader*)this) = header;
*(static_cast<CBlockHeader*>(this)) = header; // don't use C-style cast
}

ADD_SERIALIZE_METHODS;
Expand Down
2 changes: 2 additions & 0 deletions src/qt/addtokenpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ void AddTokenPage::on_confirmButton_clicked()
}
}
}
delete addToHex; // mem leak out of nowhere
}

void AddTokenPage::on_addressChanged()
Expand Down Expand Up @@ -177,6 +178,7 @@ void AddTokenPage::on_addressChanged()
m_validTokenAddress = ret;
}
ui->confirmButton->setEnabled(m_validTokenAddress);
delete addToHex; // mem leak out of nowhere
}

void AddTokenPage::on_numBlocksChanged(int newHeight)
Expand Down
1 change: 1 addition & 0 deletions src/qt/hexaddressconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void HexAddressConverter::addressChanged(const QString& address) {
if(!isAddressValid) ui->resultLabel->setText("");

ui->addressEdit->setValid(isAddressValid);
delete addToHex; // mem leak
}

void HexAddressConverter::copyButtonClicked() {
Expand Down
2 changes: 1 addition & 1 deletion src/uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ unsigned int base_uint<BITS>::bits() const
for (int pos = WIDTH - 1; pos >= 0; pos--) {
if (pn[pos]) {
for (int bits = 31; bits > 0; bits--) {
if (pn[pos] & 1 << bits)
if (pn[pos] & 1U << bits) // 1U instead of 1 to avoid Shifting a signed 32-bit value by 31 bits
return 32 * pos + bits + 1;
}
return 32 * pos + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class base_uint
base_uint ret;
for (int i = 0; i < WIDTH; i++)
ret.pn[i] = ~pn[i];
ret++;
++ret; // no need to use post-increment when pre-increment is available for this class
return ret;
}

Expand Down