396e5c
--- poppler/glib/poppler-document.cc
396e5c
+++ poppler/glib/poppler-document.cc
6a9036
@@ -3405,6 +3405,7 @@ PopplerFormField *poppler_document_get_f
6a9036
     unsigned fieldNum;
6a9036
     FormPageWidgets *widgets;
6a9036
     FormWidget *field;
6a9036
+    PopplerFormField *formField;
6a9036
 
6a9036
     FormWidget::decodeID(id, &pageNum, &fieldNum);
6a9036
 
6a9036
@@ -3417,8 +3418,14 @@ PopplerFormField *poppler_document_get_f
6a9036
         return nullptr;
6a9036
 
6a9036
     field = widgets->getWidget(fieldNum);
6a9036
-    if (field)
6a9036
-        return _poppler_form_field_new(document, field);
6a9036
+    if (field) {
6a9036
+        formField = _poppler_form_field_new(document, field);
6a9036
+        delete widgets;
396e5c
+
6a9036
+        return formField;
6a9036
+    }
396e5c
+
6a9036
+    delete widgets;
396e5c
 
6a9036
     return nullptr;
396e5c
 }
396e5c
--- poppler/poppler/CairoOutputDev.cc
396e5c
+++ poppler/poppler/CairoOutputDev.cc
6a9036
@@ -2921,8 +2921,10 @@ void CairoOutputDev::setMimeData(GfxStat
6a9036
 
6a9036
     // colorspace in stream dict may be different from colorspace in jpx
6a9036
     // data
6a9036
-    if (strKind == strJPX && colorSpace)
6a9036
+    if (strKind == strJPX && colorSpace) {
6a9036
+        delete colorSpace;
6a9036
         return;
6a9036
+    }
6a9036
 
6a9036
     // only embed mime data for gray, rgb, and cmyk colorspaces.
6a9036
     if (colorSpace) {
396e5c
--- poppler/poppler/TextOutputDev.cc
396e5c
+++ poppler/poppler/TextOutputDev.cc
6a9036
@@ -1619,7 +1619,6 @@ TextBlock::~TextBlock()
6a9036
 
6a9036
 void TextBlock::addWord(TextWord *word)
6a9036
 {
6a9036
-    pool->addWord(word);
6a9036
     if (xMin > xMax) {
6a9036
         xMin = word->xMin;
6a9036
         xMax = word->xMax;
6a9036
@@ -1639,6 +1638,7 @@ void TextBlock::addWord(TextWord *word)
6a9036
             yMax = word->yMax;
6a9036
         }
396e5c
     }
6a9036
+    pool->addWord(word);
396e5c
 }
396e5c
 
6a9036
 void TextBlock::coalesce(const UnicodeMap *uMap, double fixedPitch)
6a9036
@@ -3064,11 +3064,13 @@ void TextPage::coalesce(bool physLayout,
6a9036
             word0 = pool->getPool(startBaseIdx);
6a9036
             pool->setPool(startBaseIdx, word0->next);
6a9036
             word0->next = nullptr;
6a9036
-            blk = new TextBlock(this, rot);
6a9036
-            blk->addWord(word0);
396e5c
 
6a9036
             fontSize = word0->fontSize;
6a9036
             minBase = maxBase = word0->base;
396e5c
+
6a9036
+            blk = new TextBlock(this, rot);
6a9036
+            blk->addWord(word0);
396e5c
+
6a9036
             colSpace1 = minColSpacing1 * fontSize;
6a9036
             colSpace2 = minColSpacing2 * fontSize;
6a9036
             lineSpace = maxLineSpacingDelta * fontSize;
6a9036
@@ -3095,9 +3097,9 @@ void TextPage::coalesce(bool physLayout,
6a9036
                             }
6a9036
                             word1 = word1->next;
6a9036
                             word2->next = nullptr;
6a9036
+                            newMinBase = word2->base;
6a9036
                             blk->addWord(word2);
6a9036
                             found = true;
6a9036
-                            newMinBase = word2->base;
6a9036
                         } else {
6a9036
                             word0 = word1;
6a9036
                             word1 = word1->next;
6a9036
@@ -3123,9 +3125,9 @@ void TextPage::coalesce(bool physLayout,
6a9036
                             }
6a9036
                             word1 = word1->next;
6a9036
                             word2->next = nullptr;
6a9036
+                            newMaxBase = word2->base;
6a9036
                             blk->addWord(word2);
6a9036
                             found = true;
6a9036
-                            newMaxBase = word2->base;
6a9036
                         } else {
6a9036
                             word0 = word1;
6a9036
                             word1 = word1->next;
6a9036
@@ -3198,12 +3200,12 @@ void TextPage::coalesce(bool physLayout,
6a9036
                                 }
6a9036
                                 word1 = word1->next;
6a9036
                                 word2->next = nullptr;
6a9036
-                                blk->addWord(word2);
6a9036
                                 if (word2->base < minBase) {
6a9036
                                     minBase = word2->base;
6a9036
                                 } else if (word2->base > maxBase) {
6a9036
                                     maxBase = word2->base;
6a9036
                                 }
6a9036
+                                blk->addWord(word2);
6a9036
                                 found = true;
6a9036
                                 break;
6a9036
                             } else {
6a9036
@@ -3246,12 +3248,12 @@ void TextPage::coalesce(bool physLayout,
6a9036
                                 }
6a9036
                                 word1 = word1->next;
6a9036
                                 word2->next = nullptr;
6a9036
-                                blk->addWord(word2);
6a9036
                                 if (word2->base < minBase) {
6a9036
                                     minBase = word2->base;
6a9036
                                 } else if (word2->base > maxBase) {
6a9036
                                     maxBase = word2->base;
6a9036
                                 }
6a9036
+                                blk->addWord(word2);
6a9036
                                 found = true;
6a9036
                                 break;
6a9036
                             } else {
396e5c
--- poppler/poppler/XRef.cc
396e5c
+++ poppler/poppler/XRef.cc
6a9036
@@ -402,6 +402,7 @@ int XRef::reserve(int newSize)
6a9036
 
6a9036
         void *p = greallocn_checkoverflow(entries, realNewSize, sizeof(XRefEntry));
6a9036
         if (p == nullptr) {
6a9036
+            entries = nullptr;
6a9036
             return 0;
6a9036
         }
6a9036
 
6a9036
@@ -835,7 +836,6 @@ bool XRef::constructXRef(bool *wasRecons
6a9036
     int offset = 0;
6a9036
 
6a9036
     resize(0); // free entries properly
6a9036
-    gfree(entries);
6a9036
     capacity = 0;
6a9036
     size = 0;
6a9036
     entries = nullptr;
396e5c
--- poppler/test/pdf-inspector.cc
396e5c
+++ poppler/test/pdf-inspector.cc
6a9036
@@ -43,6 +43,7 @@ class PdfInspector
6a9036
 {
396e5c
 public:
6a9036
     PdfInspector();
6a9036
+    ~PdfInspector();
396e5c
 
6a9036
     void set_file_name(const char *file_name);
6a9036
     void load(const char *file_name);
6a9036
@@ -108,6 +109,11 @@ PdfInspector::PdfInspector()
6a9036
     load(nullptr);
396e5c
 }
6a9036
 
396e5c
+PdfInspector::~PdfInspector(void)
396e5c
+{
6a9036
+    delete output;
396e5c
+}
6a9036
+
6a9036
 void PdfInspector::set_file_name(const char *file_name)
6a9036
 {
6a9036
     GtkWidget *widget;
396e5c
--- poppler/utils/HtmlOutputDev.cc
396e5c
+++ poppler/utils/HtmlOutputDev.cc
6a9036
@@ -1337,6 +1337,7 @@ void HtmlOutputDev::drawPngImage(GfxStat
6a9036
     // TODO can we calculate the resolution of the image?
6a9036
     if (!writer->init(f1, width, height, 72, 72)) {
6a9036
         error(errInternal, -1, "Can't init PNG for image '{0:t}'", fName);
396e5c
+        delete fName;
396e5c
         delete writer;
396e5c
         fclose(f1);
6a9036
         return;
396e5c
--- poppler/utils/pdftotext.cc
396e5c
+++ poppler/utils/pdftotext.cc
6a9036
@@ -329,6 +329,7 @@ int main(int argc, char *argv[])
6a9036
             fputs("
\n", f);
6a9036
             if (f != stdout) {
6a9036
                 fclose(f);
6a9036
+                f = nullptr;
6a9036
             }
6a9036
         }
396e5c
     }
6a9036
@@ -348,8 +349,9 @@ int main(int argc, char *argv[])
6a9036
                 printWordBBox(f, doc, textOut, firstPage, lastPage);
6a9036
             }
6a9036
         }
6a9036
-        if (f != stdout) {
6a9036
+        if (f != stdout && f != nullptr) {
6a9036
             fclose(f);
6a9036
+            f = nullptr;
6a9036
         }
6a9036
     } else {
6a9036
         textOut = new TextOutputDev(textFileName->c_str(), physLayout, fixedPitch, rawOrder, htmlMeta, discardDiag);
6a9036
@@ -390,7 +392,7 @@ int main(int argc, char *argv[])
6a9036
             fputs("\n", f);
6a9036
         fputs("</body>\n", f);
6a9036
         fputs("</html>\n", f);
6a9036
-        if (f != stdout) {
6a9036
+        if (f != stdout && f != nullptr) {
6a9036
             fclose(f);
6a9036
         }
396e5c
     }