From 28542b14c4df40c7a54bfcf65d7c725bd99fb3e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Thu, 4 Jul 2024 18:03:17 +0200 Subject: [PATCH] cpp: Deprecate Error::asString and update users * lang/cpp/src/error.h (Error::asString): Mark as deprecated. * lang/cpp/src/context.cpp (operator<<), lang/cpp/src/editinteractor.cpp (edit_interactor_callback_impl), lang/cpp/tests/run-getkey.cpp (main), lang/cpp/tests/run-keylist.cpp (main), lang/cpp/tests/run-wkdlookup.cpp (main): Use Error::asStdString instead of Error::asString. -- GnuPG-bug-id: 7188 --- NEWS | 1 + lang/cpp/src/context.cpp | 2 +- lang/cpp/src/editinteractor.cpp | 4 ++-- lang/cpp/src/error.h | 5 ++++- lang/cpp/tests/run-getkey.cpp | 2 +- lang/cpp/tests/run-keylist.cpp | 2 +- lang/cpp/tests/run-wkdlookup.cpp | 14 +++++++------- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/NEWS b/NEWS index 0faf410e..d0a77df1 100644 --- a/NEWS +++ b/NEWS @@ -43,6 +43,7 @@ Noteworthy changes in version 1.24.0 (unrelease) cpp: Key::numRevocationKeys NEW. cpp: Key::revocationKeys NEW. cpp: Error::asStdString NEW. + cpp: Error::asString DEPRECATED. qt: DecryptVerifyJob::setInputFile NEW. qt: DecryptVerifyJob::inputFile NEW. qt: DecryptVerifyJob::setOutputFile NEW. diff --git a/lang/cpp/src/context.cpp b/lang/cpp/src/context.cpp index 4882c80c..8150d08b 100644 --- a/lang/cpp/src/context.cpp +++ b/lang/cpp/src/context.cpp @@ -195,7 +195,7 @@ Error Error::fromCode(unsigned int err, unsigned int src) std::ostream &operator<<(std::ostream &os, const Error &err) { - return os << "GpgME::Error(" << err.encodedError() << " (" << err.asString() << "))"; + return os << "GpgME::Error(" << err.encodedError() << " (" << err.asStdString() << "))"; } Context::KeyListModeSaver::KeyListModeSaver(Context *ctx) diff --git a/lang/cpp/src/editinteractor.cpp b/lang/cpp/src/editinteractor.cpp index 72ed92f7..84ddf398 100644 --- a/lang/cpp/src/editinteractor.cpp +++ b/lang/cpp/src/editinteractor.cpp @@ -138,7 +138,7 @@ public: if (writeAll(fd, result, len) != len) { err = Error::fromSystemError(); if (ei->debug) { - std::fprintf(ei->debug, "EditInteractor: Could not write to fd %d (%s)\n", fd, err.asString()); + std::fprintf(ei->debug, "EditInteractor: Could not write to fd %d (%s)\n", fd, err.asStdString().c_str()); } goto error; } @@ -147,7 +147,7 @@ public: if (writeAll(fd, "\n", 1) != 1) { err = Error::fromSystemError(); if (ei->debug) { - std::fprintf(ei->debug, "EditInteractor: Could not write to fd %d (%s)\n", fd, err.asString()); + std::fprintf(ei->debug, "EditInteractor: Could not write to fd %d (%s)\n", fd, err.asStdString().c_str()); } goto error; } diff --git a/lang/cpp/src/error.h b/lang/cpp/src/error.h index bc970e36..000b562b 100644 --- a/lang/cpp/src/error.h +++ b/lang/cpp/src/error.h @@ -47,7 +47,10 @@ public: explicit Error(unsigned int e) : mErr(e), mMessage() {} const char *source() const; - const char *asString() const; + /* This function is deprecated. Use asStdString() instead. asString() may + * return wrongly encoded (i.e. not UTF-8) results on Windows for the main + * thread if the function was first called from a secondary thread. */ + GPGMEPP_DEPRECATED const char *asString() const; std::string asStdString() const; int code() const; diff --git a/lang/cpp/tests/run-getkey.cpp b/lang/cpp/tests/run-getkey.cpp index c47da0b5..97d863e1 100644 --- a/lang/cpp/tests/run-getkey.cpp +++ b/lang/cpp/tests/run-getkey.cpp @@ -150,7 +150,7 @@ main (int argc, char **argv) const GpgME::Key key = ctx->key (*argv, err, only_secret); std::stringstream ss; - ss << "Key " << key << " Err: " << err.asString() << "\n"; + ss << "Key " << key << " Err: " << err.asStdString() << "\n"; std::cout << ss.str(); diff --git a/lang/cpp/tests/run-keylist.cpp b/lang/cpp/tests/run-keylist.cpp index b46a815a..6581567c 100644 --- a/lang/cpp/tests/run-keylist.cpp +++ b/lang/cpp/tests/run-keylist.cpp @@ -153,7 +153,7 @@ main (int argc, char **argv) } Error err = ctx->startKeyListing (*argv, only_secret); if (err) { - std::cout << "Error: " << err.asString() << "\n"; + std::cout << "Error: " << err.asStdString() << "\n"; return -1; } GpgME::Key key; diff --git a/lang/cpp/tests/run-wkdlookup.cpp b/lang/cpp/tests/run-wkdlookup.cpp index 4a3966b0..1e18c9a8 100644 --- a/lang/cpp/tests/run-wkdlookup.cpp +++ b/lang/cpp/tests/run-wkdlookup.cpp @@ -75,31 +75,31 @@ main (int argc, char **argv) Error err; auto ctx = std::unique_ptr{Context::createForEngine(AssuanEngine, &err)}; if (!ctx) { - std::cerr << "Failed to get context (Error: " << err.asString() << ")\n"; + std::cerr << "Failed to get context (Error: " << err.asStdString() << ")\n"; return -1; } const std::string dirmngrSocket = GpgME::dirInfo("dirmngr-socket"); if ((err = ctx->setEngineFileName(dirmngrSocket.c_str()))) { - std::cerr << "Failed to set engine file name (Error: " << err.asString() << ")\n"; + std::cerr << "Failed to set engine file name (Error: " << err.asStdString() << ")\n"; return -1; } if ((err = ctx->setEngineHomeDirectory(""))) { - std::cerr << "Failed to set engine home directory (Error: " << err.asString() << ")\n"; + std::cerr << "Failed to set engine home directory (Error: " << err.asStdString() << ")\n"; return -1; } // try to connect to dirmngr err = ctx->assuanTransact("GETINFO version"); if (err && err.code() != GPG_ERR_ASS_CONNECT_FAILED) { - std::cerr << "Failed to start assuan transaction (Error: " << err.asString() << ")\n"; + std::cerr << "Failed to start assuan transaction (Error: " << err.asStdString() << ")\n"; return -1; } if (err.code() == GPG_ERR_ASS_CONNECT_FAILED) { std::cerr << "Starting dirmngr ...\n"; auto spawnCtx = std::unique_ptr{Context::createForEngine(SpawnEngine, &err)}; if (!spawnCtx) { - std::cerr << "Failed to get context for spawn engine (Error: " << err.asString() << ")\n"; + std::cerr << "Failed to get context for spawn engine (Error: " << err.asStdString() << ")\n"; return -1; } @@ -120,7 +120,7 @@ main (int argc, char **argv) ignoreIO, ignoreIO, ignoreIO, Context::SpawnDetached); if (err) { - std::cerr << "Failed to start dirmngr (Error: " << err.asString() << ")\n"; + std::cerr << "Failed to start dirmngr (Error: " << err.asStdString() << ")\n"; return -1; } @@ -137,7 +137,7 @@ main (int argc, char **argv) const auto cmd = std::string{"WKD_GET "} + email; err = ctx->assuanTransact(cmd.c_str()); if (err && err.code() != GPG_ERR_NO_NAME && err.code() != GPG_ERR_NO_DATA) { - std::cerr << "Error: WKD_GET returned " << err.asString() << "\n"; + std::cerr << "Error: WKD_GET returned " << err.asStdString() << "\n"; return -1; }