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

Potential memory leak #859

Open
stasos24 opened this issue Mar 2, 2022 · 0 comments
Open

Potential memory leak #859

stasos24 opened this issue Mar 2, 2022 · 0 comments

Comments

@stasos24
Copy link

stasos24 commented Mar 2, 2022

File:/src/math/Base58.cpp:75

std::string ledger::core::Base58::encode(const std::vector<uint8_t> &bytes,
                                         const std::shared_ptr<api::DynamicObject> &config) {
    auto base58Dictionary = getNetworkBase58Dictionary(config);
    std::string result;
    const double iFactor = 1.36565823730976103695740418120764243208481439700722980119458355862779176747360903943915516885072037696111192757109;
    int len = bytes.size();
    int zeros = 0, length = 0, pbegin = 0, pend;
    pend = len;
    while (pbegin != pend && !bytes[pbegin]) pbegin = ++zeros;
    const int size = 1 + iFactor * (double)(pend - pbegin);
    unsigned char* b58 = new unsigned char[size];
    for (int i = 0; i < size; i++) b58[i] = 0;
    while (pbegin != pend) {
        unsigned int carry = bytes[pbegin];
        int i = 0;
        for (int it1 = size - 1; (carry || i < length) && (it1 != -1); it1--, i++) {
            carry += 256 * b58[it1];
            b58[it1] = carry % 58;
            carry /= 58;
        }
        if (carry) return result; //b58 - memory leak, result - return of uninitialized value
        length = i;
        pbegin++;
    }
    int it2 = size - length;
    while ((it2 - size) && !b58[it2]) it2++;

    int ri = 0;
    while (ri < zeros) { result += base58Dictionary[0]; ri++; }
    for (; it2 < size; ++it2) result += base58Dictionary[b58[it2]];
    delete[] b58;
    return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant