From 72d3f8f8e248a08e71ec5184aaaacd04cb877159 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 26 Aug 2020 12:25:21 +0200 Subject: [PATCH] Fix segmentation fault with curl requests. Use a heap allocated buffer (with lifetime of Aria2 class) instead of a stack allocated one. Original fix made by @ZaWertun. Kudos to him. Fix #kiwix/kiwix-desktop#123, kiwix/kiwix-desktop#513 and kiwix/kiwix-desktop#423 --- src/aria2.cpp | 10 +++++----- src/aria2.h | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/aria2.cpp b/src/aria2.cpp index 5aea07f45..9e0f5eacc 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -26,6 +26,7 @@ Aria2::Aria2(): mp_aria(nullptr), m_port(42042), m_secret("kiwixariarpc"), + m_curlErrorBuffer(new char[CURL_ERROR_SIZE]), mp_curl(nullptr) { m_downloadDir = getDataDirectory(); @@ -83,24 +84,23 @@ Aria2::Aria2(): } mp_aria = Subprocess::run(callCmd); mp_curl = curl_easy_init(); - char errbuf[CURL_ERROR_SIZE]; curl_easy_setopt(mp_curl, CURLOPT_URL, "http://localhost/rpc"); curl_easy_setopt(mp_curl, CURLOPT_PORT, m_port); curl_easy_setopt(mp_curl, CURLOPT_POST, 1L); - curl_easy_setopt(mp_curl, CURLOPT_ERRORBUFFER, errbuf); + curl_easy_setopt(mp_curl, CURLOPT_ERRORBUFFER, m_curlErrorBuffer.get()); int watchdog = 50; while(--watchdog) { sleep(10); - errbuf[0] = 0; + m_curlErrorBuffer[0] = 0; auto res = curl_easy_perform(mp_curl); if (res == CURLE_OK) { break; } else if (watchdog == 1) { std::cerr <<" curl_easy_perform() failed." << std::endl; fprintf(stderr, "\nlibcurl: (%d) ", res); - if (errbuf[0] != 0) { - std::cerr << errbuf << std::endl; + if (m_curlErrorBuffer[0] != 0) { + std::cerr << m_curlErrorBuffer.get() << std::endl; } else { std::cerr << curl_easy_strerror(res) << std::endl; } diff --git a/src/aria2.h b/src/aria2.h index 3401310eb..b07018f00 100644 --- a/src/aria2.h +++ b/src/aria2.h @@ -24,6 +24,7 @@ class Aria2 int m_port; std::string m_secret; std::string m_downloadDir; + std::unique_ptr m_curlErrorBuffer; CURL* mp_curl; std::mutex m_lock;