Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[libzypp] Fix file descriptor leak. Fixes JB#49494
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
From d9f3cd3bf9822997819dd54c7212290528944026 Mon Sep 17 00:00:00 2001 | ||
From: =?UTF-8?q?Juho=20H=C3=A4m=C3=A4l=C3=A4inen?= | ||
<juho.hamalainen@jolla.com> | ||
Date: Thu, 11 Jun 2020 10:30:27 +0300 | ||
Subject: [PATCH] MediaCurl: Fix leaking fd. | ||
MIME-Version: 1.0 | ||
Content-Type: text/plain; charset=UTF-8 | ||
Content-Transfer-Encoding: 8bit | ||
|
||
Signed-off-by: Juho Hämäläinen <juho.hamalainen@jolla.com> | ||
--- | ||
zypp/media/MediaCurl.cc | 13 +++++++++++-- | ||
1 file changed, 11 insertions(+), 2 deletions(-) | ||
|
||
diff --git a/zypp/media/MediaCurl.cc b/zypp/media/MediaCurl.cc | ||
index 1d1682773..049a7003b 100644 | ||
--- a/zypp/media/MediaCurl.cc | ||
+++ b/zypp/media/MediaCurl.cc | ||
@@ -47,6 +47,8 @@ | ||
#undef CURLVERSION_AT_LEAST | ||
#define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O) | ||
|
||
+#define fclose_and_clear(x) do { if (x) ::fclose(x); x = 0; } while (0) | ||
+ | ||
using namespace std; | ||
using namespace zypp::base; | ||
|
||
@@ -1268,7 +1270,7 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const | ||
|
||
ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file ); | ||
if ( ret != 0 ) { | ||
- ::fclose(file); | ||
+ fclose_and_clear(file); | ||
std::string err( _curlError); | ||
curl_easy_setopt( _curl, CURLOPT_RANGE, NULL ); | ||
curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L); | ||
@@ -1292,6 +1294,7 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const | ||
{ | ||
curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L); | ||
if ( ret != 0 ) { | ||
+ fclose_and_clear(file); | ||
ZYPP_THROW(MediaCurlSetOptException(url, _curlError)); | ||
} | ||
|
||
@@ -1302,6 +1305,7 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const | ||
*/ | ||
curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L); | ||
if ( ret != 0 ) { | ||
+ fclose_and_clear(file); | ||
ZYPP_THROW(MediaCurlSetOptException(url, _curlError)); | ||
} | ||
|
||
@@ -1311,13 +1315,14 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const | ||
// for FTP we set different options | ||
curl_easy_setopt( _curl, CURLOPT_RANGE, NULL); | ||
if ( ret != 0 ) { | ||
+ fclose_and_clear(file); | ||
ZYPP_THROW(MediaCurlSetOptException(url, _curlError)); | ||
} | ||
} | ||
|
||
// if the code is not zero, close the file | ||
if ( ok != 0 ) | ||
- ::fclose(file); | ||
+ fclose_and_clear(file); | ||
|
||
// as we are not having user interaction, the user can't cancel | ||
// the file existence checking, a callback or timeout return code | ||
@@ -1326,13 +1331,17 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const | ||
evaluateCurlCode( filename, ok, true /* timeout */); | ||
} | ||
catch ( const MediaFileNotFoundException &e ) { | ||
+ fclose_and_clear(file); | ||
// if the file did not exist then we can return false | ||
return false; | ||
} | ||
catch ( const MediaException &e ) { | ||
+ fclose_and_clear(file); | ||
// some error, we are not sure about file existence, rethrw | ||
ZYPP_RETHROW(e); | ||
} | ||
+ | ||
+ fclose_and_clear(file); | ||
// exists | ||
return ( ok == CURLE_OK ); | ||
} | ||
-- | ||
2.25.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters