From c82407fc1e8696d3520b03825f2b66864e61b9e2 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sun, 27 Oct 2024 18:27:11 +0100 Subject: [PATCH] libstore: always allocate TransferItem::req there's no reason not to do this. also improve error handling a bit. Change-Id: I35853a919fa58a9a34ad47ffab6de77ba6f7fb86 --- src/libstore/filetransfer.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libstore/filetransfer.cc b/src/libstore/filetransfer.cc index 6f78dad94..97411bdf3 100644 --- a/src/libstore/filetransfer.cc +++ b/src/libstore/filetransfer.cc @@ -55,7 +55,7 @@ struct curlFileTransfer : public FileTransfer std::pair(std::exception_ptr, FileTransferResult)> callback; std::function dataCallback; - CURL * req = 0; + CURL * req; // must never be nullptr bool active = false; // whether the handle has been added to the multi object std::string statusMsg; @@ -113,7 +113,11 @@ struct curlFileTransfer : public FileTransfer return std::pair{std::move(r), std::move(downloadData)}; }) , dataCallback(std::move(dataCallback)) + , req(curl_easy_init()) { + if (req == nullptr) { + throw FileTransferError(Misc, {}, "could not allocate curl handle"); + } requestHeaders = curl_slist_append(requestHeaders, "Accept-Encoding: zstd, br, gzip, deflate, bzip2, xz"); for (auto it = headers.begin(); it != headers.end(); ++it){ requestHeaders = curl_slist_append(requestHeaders, fmt("%s: %s", it->first, it->second).c_str()); @@ -122,11 +126,9 @@ struct curlFileTransfer : public FileTransfer ~TransferItem() { - if (req) { - if (active) - curl_multi_remove_handle(fileTransfer.curlm, req); - curl_easy_cleanup(req); - } + if (active) + curl_multi_remove_handle(fileTransfer.curlm, req); + curl_easy_cleanup(req); if (requestHeaders) curl_slist_free_all(requestHeaders); try { if (!done) @@ -268,8 +270,6 @@ struct curlFileTransfer : public FileTransfer void init() { - if (!req) req = curl_easy_init(); - curl_easy_reset(req); if (verbosity >= lvlVomit) {