From 8f25f4915310d084a6d0e973b8aa85ba18d7bee7 Mon Sep 17 00:00:00 2001 From: dgp Date: Thu, 6 Jul 2017 15:46:58 +0000 Subject: Alternative fix for memleaks in fs path join machinery. --- generic/tclPathObj.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/generic/tclPathObj.c b/generic/tclPathObj.c index 31ed68e..f8015b2 100644 --- a/generic/tclPathObj.c +++ b/generic/tclPathObj.c @@ -13,6 +13,7 @@ #include "tclInt.h" #include "tclFileSystem.h" +#include /* * Prototypes for functions defined later in this file. @@ -849,11 +850,17 @@ TclJoinPath( int elements, Tcl_Obj * const objv[]) { - Tcl_Obj *res; + Tcl_Obj *res = NULL; int i; const Tcl_Filesystem *fsPtr = NULL; - res = NULL; + assert ( elements >= 0 ); + + if (elements == 0) { + return Tcl_NewObj(); + } + + assert ( elements > 0 ); for (i = 0; i < elements; i++) { int driveNameLength, strEltLen, length; @@ -893,9 +900,7 @@ TclJoinPath( * the base itself is just fine! */ - if (res != NULL) { - TclDecrRefCount(res); - } + assert ( res == NULL ); return elt; } @@ -918,9 +923,8 @@ TclJoinPath( if ((tclPlatform != TCL_PLATFORM_WINDOWS) || (strchr(Tcl_GetString(elt), '\\') == NULL)) { - if (res != NULL) { - TclDecrRefCount(res); - } + + assert ( res == NULL ); if (PATHFLAGS(elt)) { return TclNewFSPathObj(elt, str, len); @@ -940,18 +944,14 @@ TclJoinPath( * more general code below handle things. */ } else if (tclPlatform == TCL_PLATFORM_UNIX) { - if (res != NULL) { - TclDecrRefCount(res); - } + assert ( res == NULL ); return tailObj; } else { const char *str = TclGetString(tailObj); if (tclPlatform == TCL_PLATFORM_WINDOWS) { if (strchr(str, '\\') == NULL) { - if (res != NULL) { - TclDecrRefCount(res); - } + assert ( res == NULL ); return tailObj; } } @@ -1087,6 +1087,7 @@ TclJoinPath( if (sep != NULL) { separator = TclGetString(sep)[0]; + Tcl_DecrRefCount(sep); } /* Safety check in case the VFS driver caused sharing */ if (Tcl_IsShared(res)) { @@ -1122,9 +1123,7 @@ TclJoinPath( Tcl_SetObjLength(res, length); } } - if (res == NULL) { - res = Tcl_NewObj(); - } + assert ( res != NULL ); return res; } -- cgit v0.12