GPGME Cannot allocate memory on gpgme_op_decrypt_start

Marco bna.marco at gmail.com
Fri Jun 25 09:39:55 CEST 2021


Hi all.



I’m having this problem with code that would like to decrypt a large gpg
file (252 MB) which is password protected!

I set the environment which works whit smaller files but in this case I face



117473366 --> Cannot allocate memory



Is there any option that I can set to use something similar
auto-expand-secmem? Is there any other way to solve the problem?



Thank you very much for your attention!



*Code sample below:*

*-------------------------------------------*


bool res(false);

  gpgme_ctx_t ctx;
  gpgme_error_t err(GPG_ERR_NO_ERROR);
  gpgme_data_t in;
  gpgme_data_t out;
  gpgme_engine_info_t engineInfo;

  size_t inFileSize(fs::file_size(input));
  std::ostringstream oss;
  oss << std::dec << inFileSize;
  std::string fileSizeStr(oss.str());

  VCIUpdaterWrapper wrap;
  wrap.setObj(const_cast<VCIUpdater*>(this));
  wrap.setFileSize(fs::file_size(input));

  memset(&ctx, 0, sizeof(ctx));
  memset(&in, 0, sizeof(in));
  memset(&out, 0, sizeof(out));
  memset(&engineInfo, 0, sizeof(engineInfo));

  setlocale(LC_ALL, "");
  gpgme_check_version(NULL);

  gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));

  err = gpgme_new(&ctx);
  if (err) {
    GENERIC_LOG_MESSAGE(LogLevel::error,
                        (boost::format("Failed to create GPG context with
error: %1% --> %2%") % err % gpgme_strerror(err)).str())
    return res;
  }

  engineInfo = gpgme_ctx_get_engine_info(ctx);
  err = gpgme_ctx_set_engine_info(ctx, engineInfo->protocol,
engineInfo->file_name,

VCIUpdateConfigurations::getInstance().getVciUpdateBaseFolder().c_str());
  if (err) {
    GENERIC_LOG_MESSAGE(LogLevel::error,
                        (boost::format("Failed to set engine info with
error: %1% --> %2%") % err % gpgme_strerror(err)).str())
    return res;
  }

  gpgme_set_armor(ctx, 0);
  gpgme_set_pinentry_mode(ctx, GPGME_PINENTRY_MODE_LOOPBACK);
  gpgme_set_passphrase_cb(ctx, &passwfFuncCb, NULL);

  err = gpgme_data_new_from_file(&in, input.string().c_str(), 1);
  if (err) {
    GENERIC_LOG_MESSAGE(
        LogLevel::error,
        (boost::format("Failed to set input file %1% for GPG with error:
%2% --> %3%") % input.string() % err % gpgme_strerror(err)).str())
    return res;
  }
//  else {
//    err = gpgme_data_set_flag(in, "size-hint", fileSizeStr.c_str());
//
//    if (err) {
//      GENERIC_LOG_MESSAGE(
//          LogLevel::error,
//          (boost::format("Failed to set size hint for input file %1% for
GPG with error: %2% --> %3%") % input.string() % err
//              % gpgme_strerror(err)).str())
//      return res;
//    }
//  }

  FILE *outFile(::fopen(output.string().c_str(), "w+"));

  if (outFile) {
    err = gpgme_data_new_from_stream(&out, outFile);
    if (err) {
      GENERIC_LOG_MESSAGE(
          LogLevel::error,
          (boost::format("Failed to set output file %1% for GPG with error:
%2% --> %3%") % output.string() % err % gpgme_strerror(err)).str())
      return res;
    }
//    else {
//      err = gpgme_data_set_flag(out, "size-hint", fileSizeStr.c_str());
//
//      if (err) {
//        GENERIC_LOG_MESSAGE(
//            LogLevel::error,
//            (boost::format("Failed to set size hint for output file %1%
for GPG with error: %2% --> %3%") % output.string() % err
//                % gpgme_strerror(err)).str())
//        return res;
//      }
//    }
  } else {
    GENERIC_LOG_MESSAGE(LogLevel::error, "Failed to open out file " +
output.string());
    return res;
  }

  gpgme_set_progress_cb(ctx, &VCIUpdaterWrapper::progress, NULL);

  gpgme_decrypt_result_t dec_result;
  err = gpgme_op_decrypt_start(ctx, in, out);
  if (err) {
    GENERIC_LOG_MESSAGE(LogLevel::error,
                        (boost::format("Failed to execute GPG decryption:
%1% --> %2%") % err % gpgme_strerror(err)).str())

    dec_result = gpgme_op_decrypt_result(ctx);

    return res;
  } else {
    gpgme_wait(ctx, &err, 1);

    if (err) {
      GENERIC_LOG_MESSAGE(LogLevel::error,
                          (boost::format("Failed to execute GPG decryption:
%1% --> %2%") % err % gpgme_strerror(err)).str())

      dec_result = gpgme_op_decrypt_result(ctx);

      return res;
    }

    dec_result = gpgme_op_decrypt_result(ctx);
    if (dec_result->unsupported_algorithm) {
      GENERIC_LOG_MESSAGE(LogLevel::error,
dec_result->unsupported_algorithm);
      return res;
    }
  }

  gpgme_data_release(in);
  gpgme_data_release(out);

  ::fclose(outFile);

  gpgme_release(ctx);

  mSignalUpdateProgress(mWholeUpdatePackages, mDoneUpdates + 1, 100,
UpdatePhase::gpg_decripth);

  res = true;
  return res;


*---------------------------------------*



*Marco Bna’*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gnupg.org/pipermail/gnupg-users/attachments/20210625/6a453e69/attachment-0001.html>


More information about the Gnupg-users mailing list