From e52a9a2b2f4cab99c68114c437d7a45685a07210 Mon Sep 17 00:00:00 2001 From: sebres Date: Thu, 30 Jul 2015 18:22:27 +0000 Subject: Fix bug [f00009f7ce]: memory (object) leaks in TclNativeCreateNativeRep for windows platform: missing decrement of refCount, because of confusing differently behavior Tcl_FSGetTranslatedPath vs Tcl_FSGetNormalizedPath. --- win/tclWinFile.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/win/tclWinFile.c b/win/tclWinFile.c index 5ee73d5..6b9d373 100755 --- a/win/tclWinFile.c +++ b/win/tclWinFile.c @@ -2897,7 +2897,7 @@ ClientData TclNativeCreateNativeRep( Tcl_Obj *pathPtr) { - WCHAR *nativePathPtr; + WCHAR *nativePathPtr = NULL; const char *str; Tcl_Obj *validPathPtr; size_t len; @@ -2911,15 +2911,21 @@ TclNativeCreateNativeRep( */ validPathPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr); + if (validPathPtr == NULL) { + return NULL; + } + /* refCount of validPathPtr was already incremented in Tcl_FSGetTranslatedPath */ } else { /* * Make sure the normalized path is set. */ validPathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr); - } - if (validPathPtr == NULL) { - return NULL; + if (validPathPtr == NULL) { + return NULL; + } + /* validPathPtr returned from Tcl_FSGetNormalizedPath is owned by Tcl, so incr refCount here */ + Tcl_IncrRefCount(validPathPtr); } str = Tcl_GetString(validPathPtr); @@ -2927,7 +2933,7 @@ TclNativeCreateNativeRep( if (strlen(str)!=(unsigned int)len) { /* String contains NUL-bytes. This is invalid. */ - return 0; + goto done; } /* For a reserved device, strip a possible postfix ':' */ len = WinIsReserved(str); @@ -2936,13 +2942,13 @@ TclNativeCreateNativeRep( * 0xC0 0x80 (== overlong NUL). See bug [3118489]: NUL in filenames */ len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1, 0, 0); if (len==0) { - return 0; + goto done; } } /* Overallocate 6 chars, making some room for extended paths */ wp = nativePathPtr = ckalloc( (len+6) * sizeof(WCHAR) ); if (nativePathPtr==0) { - return 0; + goto done; } MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1, nativePathPtr, len+1); /* @@ -2993,6 +2999,10 @@ TclNativeCreateNativeRep( } ++wp; } + + done: + + TclDecrRefCount(validPathPtr); return nativePathPtr; } -- cgit v0.12