From 2dafb5be0b7a2dcc0653f3774fe4b8138074b7a5 Mon Sep 17 00:00:00 2001
From: Dirk Lemstra <dirk@git.imagemagick.org>
Date: Thu, 9 Feb 2017 21:53:23 +0100
Subject: [PATCH] Fixed memory leak when creating nested exceptions in
 Magick++.

bug: : https://www.imagemagick.org/discourse-server/viewtopic.php?f=23&p=142634
bug-debian: https://bugs.debian.org/856880
origin: https://github.com/ImageMagick/ImageMagick/commit/3358f060fc182551822576b2c0a8850faab5d543
---
 Magick++/lib/Exception.cpp | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/Magick++/lib/Exception.cpp b/Magick++/lib/Exception.cpp
index c833fa1f2..64c432314 100644
--- a/Magick++/lib/Exception.cpp
+++ b/Magick++/lib/Exception.cpp
@@ -850,12 +850,18 @@ MagickPPExport void Magick::throwException(ExceptionInfo *exception_)
             exception_->description) != 0))
           {
             if (nestedException == (Exception *) NULL)
-              nestedException=createException(p);
+              {
+                nestedException=createException(p);
+                q=nestedException;
+              }
             else
               {
-                q=createException(p);
-                nestedException->nested(q);
-                nestedException=q;
+                Exception
+                  *r;
+
+                r=createException(p);
+                q->nested(r);
+                q=r;
               }
           }
       }
