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