Skip to content

Commit

Permalink
Fix [3fc3287497]: TclGetProcessGlobalValue encodes information twice …
Browse files Browse the repository at this point in the history
…on Windows
  • Loading branch information
jan.nijtmans committed May 23, 2024
2 parents 5a67e1d + f759aa8 commit 3d24fb2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion generic/tclIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,7 @@ FreeChannelState(
ReleaseChannelBuffer(statePtr->curOutPtr);
}
DiscardOutputQueued(statePtr);

DeleteTimerHandler(statePtr);

if (statePtr->chanMsg) {
Expand Down
2 changes: 1 addition & 1 deletion generic/tclIORChan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ CleanRefChannelInstance(
ReflectedChannel *rcPtr)
{
if (rcPtr->name) {
/*
/*
* Reset obj-type (channel is deleted or dead anyway) to avoid leakage
* by cyclic references (see bug [79474c58800cdf94]).
*/
Expand Down
16 changes: 12 additions & 4 deletions generic/tclUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -4212,6 +4212,7 @@ TclSetProcessGlobalValue(
Tcl_HashTable *cacheMap;
Tcl_HashEntry *hPtr;
int dummy;
Tcl_DString ds;

Tcl_MutexLock(&pgvPtr->mutex);

Expand All @@ -4226,8 +4227,11 @@ TclSetProcessGlobalValue(
Tcl_CreateExitHandler(FreeProcessGlobalValue, pgvPtr);
}
bytes = TclGetStringFromObj(newValue, &pgvPtr->numBytes);
Tcl_UtfToExternalDString(encoding, bytes, pgvPtr->numBytes, &ds);
pgvPtr->numBytes = Tcl_DStringLength(&ds);
pgvPtr->value = (char *)ckalloc(pgvPtr->numBytes + 1);
memcpy(pgvPtr->value, bytes, pgvPtr->numBytes + 1);
memcpy(pgvPtr->value, Tcl_DStringValue(&ds), pgvPtr->numBytes + 1);
Tcl_DStringFree(&ds);
if (pgvPtr->encoding) {
Tcl_FreeEncoding(pgvPtr->encoding);
}
Expand Down Expand Up @@ -4269,6 +4273,7 @@ TclGetProcessGlobalValue(
Tcl_HashTable *cacheMap;
Tcl_HashEntry *hPtr;
int epoch = pgvPtr->epoch;
Tcl_DString newValue;

if (pgvPtr->encoding) {
Tcl_Encoding current = Tcl_GetEncoding(NULL, NULL);
Expand All @@ -4280,7 +4285,7 @@ TclGetProcessGlobalValue(
* system encoding.
*/

Tcl_DString native, newValue;
Tcl_DString native;

Tcl_MutexLock(&pgvPtr->mutex);
epoch = ++pgvPtr->epoch;
Expand Down Expand Up @@ -4330,10 +4335,13 @@ TclGetProcessGlobalValue(
}

/*
* Store a copy of the shared value in our epoch-indexed cache.
* Store a copy of the shared value (but then in utf-8)
* in our epoch-indexed cache.
*/

value = Tcl_NewStringObj(pgvPtr->value, pgvPtr->numBytes);
Tcl_ExternalToUtfDString(NULL, pgvPtr->value, pgvPtr->numBytes, &newValue);
value = Tcl_NewStringObj(Tcl_DStringValue(&newValue), Tcl_DStringLength(&newValue));
Tcl_DStringFree(&newValue);
hPtr = Tcl_CreateHashEntry(cacheMap,
INT2PTR(pgvPtr->epoch), &dummy);
Tcl_MutexUnlock(&pgvPtr->mutex);
Expand Down
2 changes: 1 addition & 1 deletion tests/oo.test
Original file line number Diff line number Diff line change
Expand Up @@ -3311,7 +3311,7 @@ oo::class create WorkerSupport {
return [uplevel 1 $script]
} finally {
foreach worker $workers {$worker destroy}
}
}
}
method run {nworkers} {
set result {}
Expand Down
25 changes: 11 additions & 14 deletions win/tclWinSock.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,14 @@ InitializeHostName(

if (GetComputerNameExW(ComputerNamePhysicalDnsFullyQualified, wbuf, &length) != 0) {
/*
* Convert string from native to UTF then change to lowercase.
* Convert string from WCHAR to utf-8, then change to lowercase,
* then to system encoding.
*/
Tcl_DString inDs;

Tcl_UtfToLower(Tcl_WinTCharToUtf((TCHAR *)wbuf, -1, &ds));

Tcl_UtfToLower(Tcl_WinTCharToUtf((TCHAR *)wbuf, -1, &inDs));
Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&inDs), -1, &ds);
Tcl_DStringFree(&inDs);
} else {
Tcl_DStringInit(&ds);
if (TclpHasSockets(NULL) == TCL_OK) {
Expand All @@ -380,20 +383,14 @@ InitializeHostName(
* documents gethostname() as being always adequate.
*/

Tcl_DString inDs;

Tcl_DStringInit(&inDs);
Tcl_DStringSetLength(&inDs, 256);
if (gethostname(Tcl_DStringValue(&inDs),
Tcl_DStringLength(&inDs)) == 0) {
Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&inDs),
-1, &ds);
}
Tcl_DStringFree(&inDs);
Tcl_DStringInit(&ds);
Tcl_DStringSetLength(&ds, 256);
gethostname(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
Tcl_DStringSetLength(&ds, strlen(Tcl_DStringValue(&ds)));
}
}

*encodingPtr = Tcl_GetEncoding(NULL, "utf-8");
*encodingPtr = Tcl_GetEncoding(NULL, NULL);
*lengthPtr = Tcl_DStringLength(&ds);
*valuePtr = (char *)ckalloc(*lengthPtr + 1);
memcpy(*valuePtr, Tcl_DStringValue(&ds), *lengthPtr + 1);
Expand Down

0 comments on commit 3d24fb2

Please sign in to comment.