1154f6
--- poppler/glib/poppler-document.cc
1154f6
+++ poppler/glib/poppler-document.cc
1154f6
@@ -869,12 +869,12 @@ GTree *poppler_document_create_dests_tre
1154f6
     // Iterate from name-dict
1154f6
     const int nDests = catalog->numDests();
1154f6
     for (i = 0; i < nDests; ++i) {
1154f6
-        // The names of name-dict cannot contain \0,
1154f6
-        // so we can use strlen().
1154f6
-        auto name = catalog->getDestsName(i);
1154f6
-        key = poppler_named_dest_from_bytestring(reinterpret_cast<const guint8 *>(name), strlen(name));
1154f6
         std::unique_ptr<LinkDest> link_dest = catalog->getDestsDest(i);
1154f6
         if (link_dest) {
1154f6
+            // The names of name-dict cannot contain \0,
1154f6
+            // so we can use strlen().
1154f6
+            auto name = catalog->getDestsName(i);
1154f6
+            key = poppler_named_dest_from_bytestring(reinterpret_cast<const guint8 *>(name), strlen(name));
1154f6
             dest = _poppler_dest_new_goto(document, link_dest.get());
1154f6
             g_tree_insert(tree, key, dest);
1154f6
         }
1154f6
@@ -883,10 +883,10 @@ GTree *poppler_document_create_dests_tre
1154f6
     // Iterate form name-tree
1154f6
     const int nDestsNameTree = catalog->numDestNameTree();
1154f6
     for (i = 0; i < nDestsNameTree; ++i) {
1154f6
-        auto name = catalog->getDestNameTreeName(i);
1154f6
-        key = poppler_named_dest_from_bytestring(reinterpret_cast<const guint8 *>(name->c_str()), name->getLength());
1154f6
         std::unique_ptr<LinkDest> link_dest = catalog->getDestNameTreeDest(i);
1154f6
         if (link_dest) {
1154f6
+            auto name = catalog->getDestNameTreeName(i);
1154f6
+            key = poppler_named_dest_from_bytestring(reinterpret_cast<const guint8 *>(name->c_str()), name->getLength());
1154f6
             dest = _poppler_dest_new_goto(document, link_dest.get());
1154f6
             g_tree_insert(tree, key, dest);
1154f6
         }
1154f6
@@ -3405,6 +3405,7 @@ PopplerFormField *poppler_document_get_f
1154f6
     unsigned fieldNum;
1154f6
     FormPageWidgets *widgets;
1154f6
     FormWidget *field;
1154f6
+    PopplerFormField *formField;
1154f6
 
1154f6
     FormWidget::decodeID(id, &pageNum, &fieldNum);
1154f6
 
1154f6
@@ -3417,8 +3418,14 @@ PopplerFormField *poppler_document_get_f
1154f6
         return nullptr;
1154f6
 
1154f6
     field = widgets->getWidget(fieldNum);
1154f6
-    if (field)
1154f6
-        return _poppler_form_field_new(document, field);
1154f6
+    if (field) {
1154f6
+        formField = _poppler_form_field_new(document, field);
1154f6
+        delete widgets;
1154f6
+
1154f6
+        return formField;
1154f6
+    }
1154f6
+
1154f6
+    delete widgets;
1154f6
 
1154f6
     return nullptr;
1154f6
 }
1154f6
--- poppler-21.01.0/glib/poppler-page.cc
1154f6
+++ poppler-21.01.0/glib/poppler-page.cc
1154f6
@@ -1530,15 +1530,18 @@ void poppler_page_add_annot(PopplerPage
1154f6
              * first remove cropbox of the prior page before adding cropbox of the new page later */
1154f6
             quads = new_quads_from_offset_cropbox(crop_box, annot_markup->getQuadrilaterals(), FALSE);
1154f6
             annot_markup->setQuadrilaterals(quads);
1154f6
+            delete quads;
1154f6
         }
1154f6
         if (page_is_rotated) {
1154f6
             /* Quadrilateral's coords need to be saved un-rotated (same as rect coords) */
1154f6
             quads = _page_new_quads_unrotated(page->page, annot_markup->getQuadrilaterals());
1154f6
             annot_markup->setQuadrilaterals(quads);
1154f6
+            delete quads;
1154f6
         }
1154f6
         /* Add to annot's quadrilaterals the offset for the cropbox of the new page */
1154f6
         quads = new_quads_from_offset_cropbox(page_crop_box, annot_markup->getQuadrilaterals(), TRUE);
1154f6
         annot_markup->setQuadrilaterals(quads);
1154f6
+        delete quads;
1154f6
     }
1154f6
 
1154f6
     page->page->addAnnot(annot->annot);
1154f6
--- poppler-21.01.0/glib/poppler-structure-element.cc
1154f6
+++ poppler-21.01.0/glib/poppler-structure-element.cc
1154f6
@@ -1090,6 +1090,8 @@ static inline void convert_doubles_array
1154f6
     for (guint i = 0; i < *n_values; i++) {
1154f6
         doubles[i] = object->arrayGet(i).getNum();
1154f6
     }
1154f6
+
1154f6
+    values = &doubles;
1154f6
 }
1154f6
 
1154f6
 static inline void convert_color(Object *object, PopplerColor *color)
1154f6
--- poppler-21.01.0/poppler/Form.cc
1154f6
+++ poppler-21.01.0/poppler/Form.cc
1154f6
@@ -549,6 +549,7 @@ bool FormWidgetSignature::signDocument(c
1154f6
     GooString *fname = new GooString(saveFilename);
1154f6
     if (doc->saveAs(fname, writeForceIncremental) != errNone) {
1154f6
         fprintf(stderr, "signDocument: error saving to file \"%s\"\n", saveFilename);
1154f6
+        delete fname;
1154f6
         return false;
1154f6
     }
1154f6
 
1154f6
@@ -563,6 +564,8 @@ bool FormWidgetSignature::signDocument(c
1154f6
     FILE *file = openFile(saveFilename, "r+b");
1154f6
     if (!updateOffsets(file, objStart, objEnd, &sigStart, &sigEnd, &fileSize)) {
1154f6
         fprintf(stderr, "signDocument: unable update byte range\n");
1154f6
+        delete fname;
1154f6
+        fclose(file);
1154f6
         return false;
1154f6
     }
1154f6
 
1154f6
@@ -574,15 +577,21 @@ bool FormWidgetSignature::signDocument(c
1154f6
     // and sign it
1154f6
     const std::unique_ptr<GooString> signature = sigHandler.signDetached(password);
1154f6
-    if (!signature)
1154f6
+    if (!signature) {
1154f6
+        delete fname;
1154f6
+        fclose(file);
1154f6
         return false;
1154f6
+    }
1154f6
 
1154f6
     // write signature to saved file
1154f6
     if (!updateSignature(file, sigStart, sigEnd, signature.get())) {
1154f6
         fprintf(stderr, "signDocument: unable update signature\n");
1154f6
+        delete fname;
1154f6
+        fclose(file);
1154f6
         return false;
1154f6
     }
1154f6
     signatureField->setSignature(*signature);
1154f6
 
1154f6
+    delete fname;
1154f6
     fclose(file);
1154f6
 
1154f6
     return true;
1154f6
@@ -662,22 +670,30 @@ bool FormWidgetSignature::updateOffsets(
1154f6
         }
1154f6
     }
1154f6
 
1154f6
-    if (*sigStart == -1 || *sigEnd == -1)
1154f6
+    if (*sigStart == -1 || *sigEnd == -1) {
1154f6
+        free(buf);
1154f6
         return false;
1154f6
+    }
1154f6
 
1154f6
     // Search for ByteRange array and update offsets
1154f6
     for (int i = 0; i < bufSize - 10; i++) {
1154f6
         if (buf[i] == '/' && strncmp(&buf[i], "/ByteRange", 10) == 0) {
1154f6
             // update range
1154f6
             char *p = setNextOffset(&buf[i], *sigStart);
1154f6
-            if (!p)
1154f6
+            if (!p) {
1154f6
+                free(buf);
1154f6
                 return false;
1154f6
+            }
1154f6
             p = setNextOffset(p, *sigEnd);
1154f6
-            if (!p)
1154f6
+            if (!p) {
1154f6
+                free(buf);
1154f6
                 return false;
1154f6
+            }
1154f6
             p = setNextOffset(p, *fileSize - *sigEnd);
1154f6
-            if (!p)
1154f6
+            if (!p) {
1154f6
+                free(buf);
1154f6
                 return false;
1154f6
+            }
1154f6
             break;
1154f6
         }
1154f6
     }
1154f6
--- poppler-21.01.0/poppler/JBIG2Stream.cc
1154f6
+++ poppler-21.01.0/poppler/JBIG2Stream.cc
1154f6
@@ -2834,6 +2834,8 @@ JBIG2Bitmap *JBIG2Stream::readGenericBit
1154f6
 
1154f6
         if (unlikely(!codingLine || !refLine)) {
1154f6
             error(errSyntaxError, curStr->getPos(), "Bad width in JBIG2 generic bitmap");
1154f6
+            gfree(refLine);
1154f6
+            gfree(codingLine);
1154f6
             delete bitmap;
1154f6
             return nullptr;
1154f6
         }
1154f6
--- poppler-21.01.0/poppler/Lexer.cc
1154f6
+++ poppler-21.01.0/poppler/Lexer.cc
1154f6
@@ -461,6 +461,7 @@ Object Lexer::getObj(int objNum)
1154f6
             } else if (n == tokBufSize) {
1154f6
                 error(errSyntaxError, getPos(), "Warning: name token is longer than what the specification says it can be");
1154f6
                 *p = c;
1154f6
+                delete s;
1154f6
                 s = new GooString(tokBuf, n);
1154f6
             } else {
1154f6
                 s->append((char)c);
1154f6
@@ -468,6 +468,7 @@ Object Lexer::getObj(int objNum)
1154f6
         }
1154f6
         if (n < tokBufSize) {
1154f6
             *p = '\0';
1154f6
+            delete s;
1154f6
             return Object(objName, tokBuf);
1154f6
         } else {
1154f6
             Object obj(objName, s->c_str());
1154f6
--- poppler/poppler/CairoOutputDev.cc
1154f6
+++ poppler/poppler/CairoOutputDev.cc
1154f6
@@ -2921,8 +2921,10 @@ void CairoOutputDev::setMimeData(GfxStat
1154f6
 
1154f6
     // colorspace in stream dict may be different from colorspace in jpx
1154f6
     // data
1154f6
-    if (strKind == strJPX && colorSpace)
1154f6
+    if (strKind == strJPX && colorSpace) {
1154f6
+        delete colorSpace;
1154f6
         return;
1154f6
+    }
1154f6
 
1154f6
     // only embed mime data for gray, rgb, and cmyk colorspaces.
1154f6
     if (colorSpace) {
1154f6
--- poppler/poppler/TextOutputDev.cc
1154f6
+++ poppler/poppler/TextOutputDev.cc
1154f6
@@ -20,7 +20,7 @@
1154f6
 // Copyright (C) 2006 Jeff Muizelaar <jeff@infidigm.net>
1154f6
 // Copyright (C) 2007, 2008, 2012, 2017 Adrian Johnson <ajohnson@redneon.com>
1154f6
 // Copyright (C) 2008 Koji Otani <sho@bbr.jp>
1154f6
-// Copyright (C) 2008, 2010-2012, 2014-2020 Albert Astals Cid <aacid@kde.org>
1154f6
+// Copyright (C) 2008, 2010-2012, 2014-2021 Albert Astals Cid <aacid@kde.org>
1154f6
 // Copyright (C) 2008 Pino Toscano <pino@kde.org>
1154f6
 // Copyright (C) 2008, 2010 Hib Eris <hib@hiberis.nl>
1154f6
 // Copyright (C) 2009 Ross Moore <ross@maths.mq.edu.au>
1154f6
@@ -1619,7 +1619,6 @@ TextBlock::~TextBlock()
1154f6
 
1154f6
 void TextBlock::addWord(TextWord *word)
1154f6
 {
1154f6
-    pool->addWord(word);
1154f6
     if (xMin > xMax) {
1154f6
         xMin = word->xMin;
1154f6
         xMax = word->xMax;
1154f6
@@ -1639,6 +1638,7 @@ void TextBlock::addWord(TextWord *word)
1154f6
             yMax = word->yMax;
1154f6
         }
1154f6
     }
1154f6
+    pool->addWord(word);
1154f6
 }
1154f6
 
1154f6
 void TextBlock::coalesce(const UnicodeMap *uMap, double fixedPitch)
1154f6
@@ -3064,11 +3064,13 @@ void TextPage::coalesce(bool physLayout,
1154f6
             word0 = pool->getPool(startBaseIdx);
1154f6
             pool->setPool(startBaseIdx, word0->next);
1154f6
             word0->next = nullptr;
1154f6
-            blk = new TextBlock(this, rot);
1154f6
-            blk->addWord(word0);
1154f6
 
1154f6
             fontSize = word0->fontSize;
1154f6
             minBase = maxBase = word0->base;
1154f6
+
1154f6
+            blk = new TextBlock(this, rot);
1154f6
+            blk->addWord(word0);
1154f6
+
1154f6
             colSpace1 = minColSpacing1 * fontSize;
1154f6
             colSpace2 = minColSpacing2 * fontSize;
1154f6
             lineSpace = maxLineSpacingDelta * fontSize;
1154f6
@@ -3095,9 +3097,9 @@ void TextPage::coalesce(bool physLayout,
1154f6
                             }
1154f6
                             word1 = word1->next;
1154f6
                             word2->next = nullptr;
1154f6
+                            newMinBase = word2->base;
1154f6
                             blk->addWord(word2);
1154f6
                             found = true;
1154f6
-                            newMinBase = word2->base;
1154f6
                         } else {
1154f6
                             word0 = word1;
1154f6
                             word1 = word1->next;
1154f6
@@ -3123,9 +3125,9 @@ void TextPage::coalesce(bool physLayout,
1154f6
                             }
1154f6
                             word1 = word1->next;
1154f6
                             word2->next = nullptr;
1154f6
+                            newMaxBase = word2->base;
1154f6
                             blk->addWord(word2);
1154f6
                             found = true;
1154f6
-                            newMaxBase = word2->base;
1154f6
                         } else {
1154f6
                             word0 = word1;
1154f6
                             word1 = word1->next;
1154f6
@@ -3198,12 +3200,12 @@ void TextPage::coalesce(bool physLayout,
1154f6
                                 }
1154f6
                                 word1 = word1->next;
1154f6
                                 word2->next = nullptr;
1154f6
-                                blk->addWord(word2);
1154f6
                                 if (word2->base < minBase) {
1154f6
                                     minBase = word2->base;
1154f6
                                 } else if (word2->base > maxBase) {
1154f6
                                     maxBase = word2->base;
1154f6
                                 }
1154f6
+                                blk->addWord(word2);
1154f6
                                 found = true;
1154f6
                                 break;
1154f6
                             } else {
1154f6
@@ -3246,12 +3248,12 @@ void TextPage::coalesce(bool physLayout,
1154f6
                                 }
1154f6
                                 word1 = word1->next;
1154f6
                                 word2->next = nullptr;
1154f6
-                                blk->addWord(word2);
1154f6
                                 if (word2->base < minBase) {
1154f6
                                     minBase = word2->base;
1154f6
                                 } else if (word2->base > maxBase) {
1154f6
                                     maxBase = word2->base;
1154f6
                                 }
1154f6
+                                blk->addWord(word2);
1154f6
                                 found = true;
1154f6
                                 break;
1154f6
                             } else {
1154f6
@@ -4456,13 +4456,18 @@ class TextSelectionSizer : public TextSe
1154f6
 {
1154f6
 public:
1154f6
     TextSelectionSizer(TextPage *page, double scale);
1154f6
-    ~TextSelectionSizer() override { }
1154f6
+    ~TextSelectionSizer() override { delete list; }
1154f6
 
1154f6
     void visitBlock(TextBlock *block, TextLine *begin, TextLine *end, const PDFRectangle *selection) override {};
1154f6
     void visitLine(TextLine *line, TextWord *begin, TextWord *end, int edge_begin, int edge_end, const PDFRectangle *selection) override;
1154f6
     void visitWord(TextWord *word, int begin, int end, const PDFRectangle *selection) override {};
1154f6
 
1154f6
-    std::vector<PDFRectangle *> *getRegion() { return list; }
1154f6
+    std::vector<PDFRectangle *> *takeRegion()
1154f6
+    {
1154f6
+        auto aux = list;
1154f6
+        list = nullptr;
1154f6
+        return aux;
1154f6
+    }
1154f6
 
1154f6
 private:
1154f6
     std::vector<PDFRectangle *> *list;
1154f6
@@ -5063,7 +5068,7 @@ std::vector<PDFRectangle *> *TextPage::g
1154f6
 
1154f6
     visitSelection(&sizer, selection, style);
1154f6
 
1154f6
-    return sizer.getRegion();
1154f6
+    return sizer.takeRegion();
1154f6
 }
1154f6
 
1154f6
 GooString *TextPage::getSelectionText(const PDFRectangle *selection, SelectionStyle style)
1154f6
--- poppler/poppler/XRef.cc
1154f6
+++ poppler/poppler/XRef.cc
1154f6
@@ -402,6 +402,7 @@ int XRef::reserve(int newSize)
1154f6
 
1154f6
         void *p = greallocn_checkoverflow(entries, realNewSize, sizeof(XRefEntry));
1154f6
         if (p == nullptr) {
1154f6
+            entries = nullptr;
1154f6
             return 0;
1154f6
         }
1154f6
 
1154f6
@@ -835,7 +836,6 @@ bool XRef::constructXRef(bool *wasRecons
1154f6
     int offset = 0;
1154f6
 
1154f6
     resize(0); // free entries properly
1154f6
-    gfree(entries);
1154f6
     capacity = 0;
1154f6
     size = 0;
1154f6
     entries = nullptr;
1154f6
--- poppler/test/pdf-inspector.cc
1154f6
+++ poppler/test/pdf-inspector.cc
1154f6
@@ -43,6 +43,7 @@ class PdfInspector
1154f6
 {
1154f6
 public:
1154f6
     PdfInspector();
1154f6
+    ~PdfInspector();
1154f6
 
1154f6
     void set_file_name(const char *file_name);
1154f6
     void load(const char *file_name);
1154f6
@@ -108,6 +109,11 @@ PdfInspector::PdfInspector()
1154f6
     load(nullptr);
1154f6
 }
1154f6
 
1154f6
+PdfInspector::~PdfInspector(void)
1154f6
+{
1154f6
+    delete output;
1154f6
+}
1154f6
+
1154f6
 void PdfInspector::set_file_name(const char *file_name)
1154f6
 {
1154f6
     GtkWidget *widget;
1154f6
--- poppler/utils/HtmlOutputDev.cc
1154f6
+++ poppler/utils/HtmlOutputDev.cc
1154f6
@@ -1337,6 +1337,7 @@ void HtmlOutputDev::drawPngImage(GfxStat
1154f6
     // TODO can we calculate the resolution of the image?
1154f6
     if (!writer->init(f1, width, height, 72, 72)) {
1154f6
         error(errInternal, -1, "Can't init PNG for image '{0:t}'", fName);
1154f6
+        delete fName;
1154f6
         delete writer;
1154f6
         fclose(f1);
1154f6
         return;
1154f6
@@ -1378,6 +1378,7 @@ void HtmlOutputDev::drawPngImage(GfxStat
1154f6
 
1154f6
             if (!writer->writeRow(row_pointer)) {
1154f6
                 error(errIO, -1, "Failed to write into PNG '{0:t}'", fName);
1154f6
+                delete fName;
1154f6
                 delete writer;
1154f6
                 delete imgStr;
1154f6
                 fclose(f1);
1154f6
@@ -1413,6 +1414,7 @@ void HtmlOutputDev::drawPngImage(GfxStat
1154f6
 
1154f6
             if (!writer->writeRow(&png_row)) {
1154f6
                 error(errIO, -1, "Failed to write into PNG '{0:t}'", fName);
1154f6
+                delete fName;
1154f6
                 delete writer;
1154f6
                 fclose(f1);
1154f6
                 gfree(png_row);
1154f6
--- poppler/utils/pdftotext.cc
1154f6
+++ poppler/utils/pdftotext.cc
1154f6
@@ -329,6 +329,7 @@ int main(int argc, char *argv[])
1154f6
             fputs("
\n", f);
1154f6
             if (f != stdout) {
1154f6
                 fclose(f);
1154f6
+                f = nullptr;
1154f6
             }
1154f6
         }
1154f6
     }
1154f6
@@ -348,8 +349,9 @@ int main(int argc, char *argv[])
1154f6
                 printWordBBox(f, doc, textOut, firstPage, lastPage);
1154f6
             }
1154f6
         }
1154f6
-        if (f != stdout) {
1154f6
+        if (f != stdout && f != nullptr) {
1154f6
             fclose(f);
1154f6
+            f = nullptr;
1154f6
         }
1154f6
     } else {
1154f6
         textOut = new TextOutputDev(textFileName->c_str(), physLayout, fixedPitch, rawOrder, htmlMeta, discardDiag);
1154f6
@@ -390,7 +392,7 @@ int main(int argc, char *argv[])
1154f6
             fputs("\n", f);
1154f6
         fputs("</body>\n", f);
1154f6
         fputs("</html>\n", f);
1154f6
-        if (f != stdout) {
1154f6
+        if (f != stdout && f != nullptr) {
1154f6
             fclose(f);
1154f6
         }
1154f6
     }
1154f6
@@ -533,7 +533,9 @@ void printWordBBox(FILE *f, PDFDoc *doc,
1154f6
         for (int i = 0; i < word_length; ++i) {
1154f6
             word = wordlist->get(i);
1154f6
             word->getBBox(&xMinA, &yMinA, &xMaxA, &yMaxA);
1154f6
-            const std::string myString = myXmlTokenReplace(word->getText()->c_str());
1154f6
+            GooString *wordText = word->getText();
1154f6
+            const std::string myString = myXmlTokenReplace(wordText->c_str());
1154f6
+            delete wordText;
1154f6
             fprintf(f, "    <word xMin=\"%f\" yMin=\"%f\" xMax=\"%f\" yMax=\"%f\">%s</word>\n", xMinA, yMinA, xMaxA, yMaxA, myString.c_str());
1154f6
         }
1154f6
         fprintf(f, "  </page>\n");