[poppler] glib/poppler-cached-file-loader.cc glib/poppler-cached-file-loader.h glib/poppler-document.cc poppler/CachedFile.cc poppler/CachedFile.h poppler/CurlCachedFile.cc poppler/CurlCachedFile.h poppler/CurlPDFDocBuilder.cc poppler/FDPDFDocBuilder.cc poppler/FILECacheLoader.cc poppler/FILECacheLoader.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Mar 30 16:18:23 UTC 2022


 glib/poppler-cached-file-loader.cc |    7 +++----
 glib/poppler-cached-file-loader.h  |    4 ++--
 glib/poppler-document.cc           |    4 ++--
 poppler/CachedFile.cc              |   10 ++++------
 poppler/CachedFile.h               |    9 ++++-----
 poppler/CurlCachedFile.cc          |   12 +++++-------
 poppler/CurlCachedFile.h           |    8 ++++----
 poppler/CurlPDFDocBuilder.cc       |    2 +-
 poppler/FDPDFDocBuilder.cc         |    2 +-
 poppler/FILECacheLoader.cc         |    4 ++--
 poppler/FILECacheLoader.h          |    4 ++--
 11 files changed, 30 insertions(+), 36 deletions(-)

New commits:
commit 1ee63109e19975be931c607e04faff72e0f43888
Author: Albert Astals Cid <aacid at kde.org>
Date:   Wed Mar 30 16:27:24 2022 +0200

    Remove the url from the CachedFileLoader::init function
    
    It was unused everywhere except in CurlCachedFileLoader, but there we
    can just pass it in the constructor.
    
    The use in one of the two glib cases was a memory leak

diff --git a/glib/poppler-cached-file-loader.cc b/glib/poppler-cached-file-loader.cc
index 570bd20b..9e747a04 100644
--- a/glib/poppler-cached-file-loader.cc
+++ b/glib/poppler-cached-file-loader.cc
@@ -1,6 +1,7 @@
 /* poppler-cached-file-loader.h: glib interface to poppler
  *
  * Copyright (C) 2012 Carlos Garcia Campos <carlosgc at gnome.org>
+ * Copyright (C) 2022 Albert Astals Cid <aacid at kde.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -25,7 +26,6 @@ PopplerCachedFileLoader::PopplerCachedFileLoader(GInputStream *inputStreamA, GCa
     inputStream = (GInputStream *)g_object_ref(inputStreamA);
     cancellable = cancellableA ? (GCancellable *)g_object_ref(cancellableA) : nullptr;
     length = lengthA;
-    url = nullptr;
     cachedFile = nullptr;
 }
 
@@ -37,13 +37,12 @@ PopplerCachedFileLoader::~PopplerCachedFileLoader()
     }
 }
 
-size_t PopplerCachedFileLoader::init(GooString *urlA, CachedFile *cachedFileA)
+size_t PopplerCachedFileLoader::init(CachedFile *cachedFileA)
 {
     size_t size;
     gssize bytesRead;
     char buf[CachedFileChunkSize];
 
-    url = urlA;
     cachedFile = cachedFileA;
 
     if (length != (goffset)-1) {
@@ -55,7 +54,7 @@ size_t PopplerCachedFileLoader::init(GooString *urlA, CachedFile *cachedFileA)
 
         info = g_file_input_stream_query_info(G_FILE_INPUT_STREAM(inputStream), G_FILE_ATTRIBUTE_STANDARD_SIZE, cancellable, nullptr);
         if (!info) {
-            error(errInternal, -1, "Failed to get size of '{0:t}'.", urlA);
+            error(errInternal, -1, "Failed to get size.");
             return (size_t)-1;
         }
 
diff --git a/glib/poppler-cached-file-loader.h b/glib/poppler-cached-file-loader.h
index 935ea483..464fb5e4 100644
--- a/glib/poppler-cached-file-loader.h
+++ b/glib/poppler-cached-file-loader.h
@@ -1,6 +1,7 @@
 /* poppler-cached-file-loader.h: glib interface to poppler
  *
  * Copyright (C) 2012 Carlos Garcia Campos <carlosgc at gnome.org>
+ * Copyright (C) 2022 Albert Astals Cid <aacid at kde.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,14 +30,13 @@ class PopplerCachedFileLoader : public CachedFileLoader
 public:
     PopplerCachedFileLoader(GInputStream *inputStreamA, GCancellable *cancellableA, goffset lengthA = -1);
     ~PopplerCachedFileLoader() override;
-    size_t init(GooString *url, CachedFile *cachedFile) override;
+    size_t init(CachedFile *cachedFile) override;
     int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) override;
 
 private:
     GInputStream *inputStream;
     GCancellable *cancellable;
     goffset length;
-    GooString *url;
     CachedFile *cachedFile;
 };
 
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 5de08108..7796f5a3 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -387,7 +387,7 @@ PopplerDocument *poppler_document_new_from_stream(GInputStream *stream, goffset
         }
         str = new PopplerInputStream(stream, cancellable, 0, false, length, Object(objNull));
     } else {
-        CachedFile *cachedFile = new CachedFile(new PopplerCachedFileLoader(stream, cancellable, length), new GooString());
+        CachedFile *cachedFile = new CachedFile(new PopplerCachedFileLoader(stream, cancellable, length));
         str = new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull));
     }
 
@@ -509,7 +509,7 @@ PopplerDocument *poppler_document_new_from_fd(int fd, const char *password, GErr
             }
         }
 
-        CachedFile *cachedFile = new CachedFile(new FILECacheLoader(file), nullptr);
+        CachedFile *cachedFile = new CachedFile(new FILECacheLoader(file));
         stream = new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull));
     } else {
         stream = new OwningFileStream(GooFile::open(fd), Object(objNull));
diff --git a/poppler/CachedFile.cc b/poppler/CachedFile.cc
index c4a5acb5..149a6126 100644
--- a/poppler/CachedFile.cc
+++ b/poppler/CachedFile.cc
@@ -6,7 +6,7 @@
 //
 // Copyright 2009 Stefan Thomas <thomas at eload24.com>
 // Copyright 2010, 2011 Hib Eris <hib at hiberis.nl>
-// Copyright 2010, 2018-2020 Albert Astals Cid <aacid at kde.org>
+// Copyright 2010, 2018-2020, 2022 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2013 Julien Nabet <serval2412 at yahoo.fr>
 //
 //========================================================================
@@ -18,29 +18,27 @@
 // CachedFile
 //------------------------------------------------------------------------
 
-CachedFile::CachedFile(CachedFileLoader *cacheLoader, GooString *uriA)
+CachedFile::CachedFile(CachedFileLoader *cacheLoader)
 {
-    uri = uriA;
     loader = cacheLoader;
 
     streamPos = 0;
     chunks = new std::vector<Chunk>();
     length = 0;
 
-    length = loader->init(uri, this);
+    length = loader->init(this);
     refCnt = 1;
 
     if (length != ((size_t)-1)) {
         chunks->resize(length / CachedFileChunkSize + 1);
     } else {
-        error(errInternal, -1, "Failed to initialize file cache for '{0:t}'.", uri);
+        error(errInternal, -1, "Failed to initialize file cache.");
         chunks->resize(0);
     }
 }
 
 CachedFile::~CachedFile()
 {
-    delete uri;
     delete loader;
     delete chunks;
 }
diff --git a/poppler/CachedFile.h b/poppler/CachedFile.h
index 588c0e4e..854692ea 100644
--- a/poppler/CachedFile.h
+++ b/poppler/CachedFile.h
@@ -8,7 +8,7 @@
 //
 // Copyright 2009 Stefan Thomas <thomas at eload24.com>
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
-// Copyright 2010, 2018-2020 Albert Astals Cid <aacid at kde.org>
+// Copyright 2010, 2018-2020, 2022 Albert Astals Cid <aacid at kde.org>
 //
 //========================================================================
 
@@ -45,7 +45,7 @@ class POPPLER_PRIVATE_EXPORT CachedFile
     friend class CachedFileWriter;
 
 public:
-    CachedFile(CachedFileLoader *cacheLoader, GooString *uri);
+    explicit CachedFile(CachedFileLoader *cacheLoader);
 
     CachedFile(const CachedFile &) = delete;
     CachedFile &operator=(const CachedFile &) = delete;
@@ -79,7 +79,6 @@ private:
     int cache(size_t offset, size_t length);
 
     CachedFileLoader *loader;
-    GooString *uri;
 
     size_t length;
     size_t streamPos;
@@ -136,8 +135,8 @@ public:
 
     // Initializes the file load.
     // Returns the length of the file.
-    // The caller is responsible for deleting uri and cachedFile.
-    virtual size_t init(GooString *uri, CachedFile *cachedFile) = 0;
+    // The caller is responsible for deleting cachedFile.
+    virtual size_t init(CachedFile *cachedFile) = 0;
 
     // Loads specified byte ranges and passes it to the writer to store them.
     // Returns 0 on success, Anything but 0 on failure.
diff --git a/poppler/CurlCachedFile.cc b/poppler/CurlCachedFile.cc
index 1ff17220..453c66a3 100644
--- a/poppler/CurlCachedFile.cc
+++ b/poppler/CurlCachedFile.cc
@@ -18,9 +18,8 @@
 
 //------------------------------------------------------------------------
 
-CurlCachedFileLoader::CurlCachedFileLoader()
+CurlCachedFileLoader::CurlCachedFileLoader(const std::string &urlA) : url(urlA)
 {
-    url = nullptr;
     cachedFile = nullptr;
     curl = nullptr;
 }
@@ -35,17 +34,16 @@ static size_t noop_cb(char *ptr, size_t size, size_t nmemb, void *ptr2)
     return size * nmemb;
 }
 
-size_t CurlCachedFileLoader::init(GooString *urlA, CachedFile *cachedFileA)
+size_t CurlCachedFileLoader::init(CachedFile *cachedFileA)
 {
     double contentLength = -1;
     long code = 0;
     size_t size;
 
-    url = urlA;
     cachedFile = cachedFileA;
     curl = curl_easy_init();
 
-    curl_easy_setopt(curl, CURLOPT_URL, url->c_str());
+    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
     curl_easy_setopt(curl, CURLOPT_HEADER, 1);
     curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &noop_cb);
@@ -55,7 +53,7 @@ size_t CurlCachedFileLoader::init(GooString *urlA, CachedFile *cachedFileA)
         curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength);
         size = contentLength;
     } else {
-        error(errInternal, -1, "Failed to get size of '{0:t}'.", url);
+        error(errInternal, -1, "Failed to get size of '{0:s}'.", url.c_str());
         size = -1;
     }
     curl_easy_reset(curl);
@@ -79,7 +77,7 @@ int CurlCachedFileLoader::load(const std::vector<ByteRange> &ranges, CachedFileW
         toByte = fromByte + bRange.length - 1;
         const std::unique_ptr<GooString> range = GooString::format("{0:ulld}-{1:ulld}", fromByte, toByte);
 
-        curl_easy_setopt(curl, CURLOPT_URL, url->c_str());
+        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, load_cb);
         curl_easy_setopt(curl, CURLOPT_WRITEDATA, writer);
         curl_easy_setopt(curl, CURLOPT_RANGE, range->c_str());
diff --git a/poppler/CurlCachedFile.h b/poppler/CurlCachedFile.h
index b3bf77b2..752b7005 100644
--- a/poppler/CurlCachedFile.h
+++ b/poppler/CurlCachedFile.h
@@ -5,7 +5,7 @@
 // This file is licensed under the GPLv2 or later
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
-// Copyright 2010 Albert Astals Cid <aacid at kde.org>
+// Copyright 2010, 2022 Albert Astals Cid <aacid at kde.org>
 //
 //========================================================================
 
@@ -23,13 +23,13 @@ class CurlCachedFileLoader : public CachedFileLoader
 {
 
 public:
-    CurlCachedFileLoader();
+    explicit CurlCachedFileLoader(const std::string &urlA);
     ~CurlCachedFileLoader() override;
-    size_t init(GooString *url, CachedFile *cachedFile) override;
+    size_t init(CachedFile *cachedFile) override;
     int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) override;
 
 private:
-    GooString *url;
+    const std::string url;
     CachedFile *cachedFile;
     CURL *curl;
 };
diff --git a/poppler/CurlPDFDocBuilder.cc b/poppler/CurlPDFDocBuilder.cc
index 491711ca..57ae7a50 100644
--- a/poppler/CurlPDFDocBuilder.cc
+++ b/poppler/CurlPDFDocBuilder.cc
@@ -24,7 +24,7 @@
 
 std::unique_ptr<PDFDoc> CurlPDFDocBuilder::buildPDFDoc(const GooString &uri, const std::optional<GooString> &ownerPassword, const std::optional<GooString> &userPassword, void *guiDataA)
 {
-    CachedFile *cachedFile = new CachedFile(new CurlCachedFileLoader(), uri.copy());
+    CachedFile *cachedFile = new CachedFile(new CurlCachedFileLoader(uri.toStr()));
 
     if (cachedFile->getLength() == ((unsigned int)-1)) {
         cachedFile->decRefCnt();
diff --git a/poppler/FDPDFDocBuilder.cc b/poppler/FDPDFDocBuilder.cc
index 9a464059..dcfbbacc 100644
--- a/poppler/FDPDFDocBuilder.cc
+++ b/poppler/FDPDFDocBuilder.cc
@@ -51,7 +51,7 @@ std::unique_ptr<PDFDoc> FileDescriptorPDFDocBuilder::buildPDFDoc(const GooString
         return {};
     }
 
-    CachedFile *cachedFile = new CachedFile(new FILECacheLoader(file), nullptr);
+    CachedFile *cachedFile = new CachedFile(new FILECacheLoader(file));
     return std::make_unique<PDFDoc>(new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull)), ownerPassword, userPassword);
 }
 
diff --git a/poppler/FILECacheLoader.cc b/poppler/FILECacheLoader.cc
index 74169ed7..4deb2ff6 100644
--- a/poppler/FILECacheLoader.cc
+++ b/poppler/FILECacheLoader.cc
@@ -5,7 +5,7 @@
 // This file is licensed under the GPLv2 or later
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
-// Copyright 2010 Albert Astals Cid <aacid at kde.org>
+// Copyright 2010, 2022 Albert Astals Cid <aacid at kde.org>
 // Copyright 2010 Jonathan Liu <net147 at gmail.com>
 // Copyright 2021 Peter Williams <peter at newton.cx>
 // Copyright 2021 Christian Persch <chpe at src.gnome.org>
@@ -28,7 +28,7 @@ FILECacheLoader::~FILECacheLoader()
     }
 }
 
-size_t FILECacheLoader::init(GooString *dummy, CachedFile *cachedFile)
+size_t FILECacheLoader::init(CachedFile *cachedFile)
 {
     size_t read, size = 0;
     char buf[CachedFileChunkSize];
diff --git a/poppler/FILECacheLoader.h b/poppler/FILECacheLoader.h
index 09167eba..6662ebcf 100644
--- a/poppler/FILECacheLoader.h
+++ b/poppler/FILECacheLoader.h
@@ -5,7 +5,7 @@
 // This file is licensed under the GPLv2 or later
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
-// Copyright 2010 Albert Astals Cid <aacid at kde.org>
+// Copyright 2010, 2022 Albert Astals Cid <aacid at kde.org>
 // Copyright 2021 Christian Persch <chpe at src.gnome.org>
 //
 //========================================================================
@@ -27,7 +27,7 @@ public:
 
     explicit FILECacheLoader(FILE *fileA) : file(fileA) { }
 
-    size_t init(GooString *dummy, CachedFile *cachedFile) override;
+    size_t init(CachedFile *cachedFile) override;
     int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) override;
 };
 


More information about the poppler mailing list