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

fix two bugs about computeHash and roundInputBySignificantFigure #480

Merged
merged 2 commits into from
Oct 30, 2024

Conversation

cloudQuant
Copy link
Contributor

when the shaVersion is not right, it will generate memory leak, we need free the context in the default

static std::string computeHash(const ShaVersion shaVersion, const std::string& unhashed, bool returnHex = false) {
    EVP_MD_CTX* context = EVP_MD_CTX_new();
    switch (shaVersion) {
      case ShaVersion::SHA256:
        EVP_DigestInit_ex(context, EVP_sha256(), nullptr);
        break;
      case ShaVersion::SHA512:
        EVP_DigestInit_ex(context, EVP_sha512(), nullptr);
        break;
      default:
        EVP_MD_CTX_free(context);  // Release the context, adding an extra line here to avoid potential memory leaks that may occur in computeHash
        throw std::invalid_argument("invalid shaVersion");
    }
    EVP_DigestUpdate(context, unhashed.c_str(), unhashed.length());
    unsigned char hash[EVP_MAX_MD_SIZE];
    unsigned int lengthOfHash = 0;
    EVP_DigestFinal_ex(context, hash, &lengthOfHash);
    EVP_MD_CTX_free(context);
    std::stringstream ss;
    if (returnHex) {
      for (unsigned int i = 0; i < lengthOfHash; ++i) {
        ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
      }
    } else {
      for (unsigned int i = 0; i < lengthOfHash; ++i) {
        ss << (char)hash[i];
      }
    }
    return ss.str();
  }

the original function : roundInputBySignificantFigure(0.00123456, 3, 1), output is "000.124"
maybe we expect the output is "0.00124", so we need to modify three line in else
new function will output "0.00124"

static std::string roundInputBySignificantFigure(double input, int numSignificantFigure, int roundDirection) {
    const auto& splitted = UtilString::split(UtilString::printDoubleScientific(input), 'e');
    double a = std::stod(splitted.at(0)) * std::pow(10, numSignificantFigure - 1);
    double b;
    if (roundDirection > 0) {
      b = std::ceil(a);
    } else if (roundDirection < 0) {
      b = std::floor(a);
    } else {
      b = std::round(a);
    }
    std::string c = std::to_string(static_cast<int>(b));
    int exponent = std::stoi(splitted.at(1)) - (numSignificantFigure - 1);
    std::string output;
    if (exponent >= 0) {
      output = c + std::string(exponent, '0');
    } else if (-exponent <= c.size() - 1) {
      output = c.substr(0, c.size() + exponent);
      output += ".";
      output += c.substr(c.size() + exponent);
    } else {
//      output = std::string(-exponent - c.size() + 1, '0');
//      output += ".";
//      output += c;  // use these three code, roundInputBySignificantFigure(0.00123456, 3, 1), output is  "000.124"
      output = "0.";
      output += std::string(-exponent - c.size(), '0');
      output += c;   // use these three code, roundInputBySignificantFigure(0.00123456, 3, 1), output is  "0.00124"
    }
    return output;
  }

@AnthonyFJGarner AnthonyFJGarner merged commit d1dbbbd into crypto-chassis:develop Oct 30, 2024
1 check passed
Copy link

🎉 This PR is included in version 6.11.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants