Subject: Stop leaking allocated space when objifying a zero-length DString. [Bug 3598150] spotted by afredd.

From: ferrieux <[email-masked]>
Date: Sat, 22 Dec 2012 19:05:34 +0000

---
 ChangeLog         |  5 +++++
 generic/tclUtil.c | 18 ++++++++++--------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 336da37..49da827 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2012-12-22  Alexandre Ferrieux  <[email-masked]>
+
+	* generic/tclUtil.c: Stop leaking allocated space when objifying a
+	zero-length DString. [Bug 3598150] spotted by afredd.
+
 2012-12-21  Jan Nijtmans  <[email-masked]>
 
 	* unix/dltest/pkgb.c:  Inline compat Tcl_GetDefaultEncodingDir.
diff --git a/generic/tclUtil.c b/generic/tclUtil.c
index 13e54ec..ddf067b 100644
--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -2927,14 +2927,16 @@ TclDStringToObj(
 {
     Tcl_Obj *result;
 
-    if (dsPtr->length == 0) {
-	TclNewObj(result);
-    } else if (dsPtr->string == dsPtr->staticSpace) {
-	/*
-	 * Static buffer, so must copy.
-	 */
-
-	TclNewStringObj(result, dsPtr->string, dsPtr->length);
+    if (dsPtr->string == dsPtr->staticSpace) {
+	if (dsPtr->length == 0) {
+	    TclNewObj(result);
+	} else {
+	    /*
+	     * Static buffer, so must copy.
+	     */
+	    
+	    TclNewStringObj(result, dsPtr->string, dsPtr->length);
+	}
     } else {
 	/*
 	 * Dynamic buffer, so transfer ownership and reset.
-- 
cgit v0.12