From af3e1e1a3577c4e1c66cbe69ebdc6a632038e299 Mon Sep 17 00:00:00 2001
From: Albert Astals Cid <aacid@kde.org>
Date: Fri, 1 Apr 2022 16:03:46 +0200
Subject: [PATCH] Link: Just return the std::vector instead of two getters

Simpler code to use and solves the int vs size_t mismatch by not having a
type involved at all

CVE: CVE-2025-52886
Upstream-Status: Backport [https://gitlab.freedesktop.org/poppler/poppler/-/commit/af3e1e1a3577c4e1c66cbe69ebdc6a632038e299]

Signed-off-by: Yogita Urade <yogita.urade@windriver.com>
---
 glib/poppler-page.cc   | 5 +----
 poppler/Link.h         | 4 +---
 poppler/Page.cc        | 4 ++--
 utils/HtmlOutputDev.cc | 4 ++--
 utils/pdfinfo.cc       | 3 +--
 5 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/glib/poppler-page.cc b/glib/poppler-page.cc
index f0b7245..a9515f3 100644
--- a/glib/poppler-page.cc
+++ b/glib/poppler-page.cc
@@ -1119,7 +1119,6 @@ static void poppler_page_init(PopplerPage *page) { }
 GList *poppler_page_get_link_mapping(PopplerPage *page)
 {
     GList *map_list = nullptr;
-    gint i;
     Links *links;
     double width, height;

@@ -1133,13 +1132,11 @@ GList *poppler_page_get_link_mapping(PopplerPage *page)

     poppler_page_get_size(page, &width, &height);

-    for (i = 0; i < links->getNumLinks(); i++) {
+    for (AnnotLink *link : links->getLinks()) {
         PopplerLinkMapping *mapping;
         PopplerRectangle rect;
         LinkAction *link_action;
-        AnnotLink *link;

-        link = links->getLink(i);
         link_action = link->getAction();

         /* Create the mapping */
diff --git a/poppler/Link.h b/poppler/Link.h
index 0c4677c..204207b 100644
--- a/poppler/Link.h
+++ b/poppler/Link.h
@@ -555,9 +555,7 @@ public:
     Links(const Links &) = delete;
     Links &operator=(const Links &) = delete;

-    // Iterate through list of links.
-    int getNumLinks() const { return links.size(); }
-    AnnotLink *getLink(int i) const { return links[i]; }
+    const std::vector<AnnotLink *> &getLinks() const { return links; }

 private:
     std::vector<AnnotLink *> links;
diff --git a/poppler/Page.cc b/poppler/Page.cc
index 6b33500..c256d39 100644
--- a/poppler/Page.cc
+++ b/poppler/Page.cc
@@ -778,8 +778,8 @@ void Page::makeBox(double hDPI, double vDPI, int rotate, bool useMediaBox, bool
 void Page::processLinks(OutputDev *out)
 {
     std::unique_ptr<Links> links = getLinks();
-    for (int i = 0; i < links->getNumLinks(); ++i) {
-        out->processLink(links->getLink(i));
+    for (AnnotLink *link : links->getLinks()) {
+        out->processLink(link);
     }
 }

diff --git a/utils/HtmlOutputDev.cc b/utils/HtmlOutputDev.cc
index 71fe672..2056188 100644
--- a/utils/HtmlOutputDev.cc
+++ b/utils/HtmlOutputDev.cc
@@ -1247,8 +1247,8 @@ void HtmlOutputDev::startPage(int pageNumA, GfxState *state, XRef *xref)
 void HtmlOutputDev::endPage()
 {
     std::unique_ptr<Links> linksList = docPage->getLinks();
-    for (int i = 0; i < linksList->getNumLinks(); ++i) {
-        doProcessLink(linksList->getLink(i));
+    for (AnnotLink *link : linksList->getLinks()) {
+        doProcessLink(link);
     }

     pages->conv();
diff --git a/utils/pdfinfo.cc b/utils/pdfinfo.cc
index 4b9166f..5f96b41 100644
--- a/utils/pdfinfo.cc
+++ b/utils/pdfinfo.cc
@@ -415,8 +415,7 @@ static void printUrlList(PDFDoc *doc)
         Page *page = doc->getPage(pg);
         if (page) {
             std::unique_ptr<Links> links = page->getLinks();
-            for (int i = 0; i < links->getNumLinks(); i++) {
-                AnnotLink *annot = links->getLink(i);
+            for (AnnotLink *annot : links->getLinks()) {
                 LinkAction *action = annot->getAction();
                 if (action->getKind() == actionURI) {
                     LinkURI *linkUri = dynamic_cast<LinkURI *>(action);
--
2.40.0
