Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: b532f567b7a956620dc5b97230874b241db88f7c512e8fa70ab706b611168936
Ticket: 3fc3287497a7e281b96661f883c20e0e74a0fca7
TclGetProcessGlobalValue encodes information twice on Windows
User & Date: ralfixx on 2024-05-22 17:32:59
Changes

  1. comment changed to:
    If the hostname on Windows contains an Umlaut, [info hostname] will return a different value when invoked for the second time:
    
        % tclsh8.6
        % info hostname
        foo-läptop
        % info hostname
        foo-läpt
    
    BUG: The second time, the hostname is encoded another time.
    
    Looking at the code for [info hostname] on Windows, it seems to me:
    
    - information is stored in global variable
        static ProcessGlobalValue hostName =
        	{0, 0, NULL, NULL, InitializeHostName, NULL, NULL};
      (win/tclWinSock.c)
      
    - the first time the information is retrieved by
      TclGetProcessGlobalValue(), it is initialized by a
      call to InitializeHostName(), which sets the encoding of the
      information to utf-8 after retrieving it from windows and converting
      it to utf-8
            *encodingPtr = Tcl_GetEncoding(NULL, "utf-8");
      (win/tclWinSock.c)
      
    - the next time the information is retrieved by
      TclGetProcessGlobalValue(), it sees that the encoding is non-NULL and
      compares it to the system encoding, which on Windows is not utf-8, but
      (in my case) cp1252.  So the information is converted again.
    
      TclGetProcessGlobalValue()
        ...
        if (pgvPtr->encoding) {
    	Tcl_Encoding current = Tcl_GetEncoding(NULL, NULL);
    
    	if (pgvPtr->encoding != current) {
    
    This is due to a wrong first argument to Tcl_UtfToExternalDString() 
    in TclGetProcessGlobalValue() in generic/tclUtil.c:
    
    #4287	    Tcl_UtfToExternalDString(pgvPtr->encoding, pgvPtr->value,
    		    pgvPtr->numBytes, &native);
    
    The first argument is the encoding to convert TO, not convert FROM.
    
    I first thought that TCL9 had this fixed by using NULL as first argument, but it actually hasn't.
    
  2. icomment:
    And yet another STOP... :-)
    
    It seems to me that the real problem is setting encoding to "utf-8" in the first place in win/tclWinSock.c, proc InitializeHostName().
    
    All other initialization routines for ProcessGlobalValue variables
    use NULL as encoding, which would work as well here.
    
    So in addition, the following patch should be applied:
    
    diff -u tcl8.6.14/win/tclWinSock.c\~ tcl8.6.14/win/tclWinSock.c
    --- tcl8.6.14/win/tclWinSock.c~	2024-02-20 18:10:06.000000000 +0100
    +++ tcl8.6.14/win/tclWinSock.c	2024-05-22 19:31:05.113600529 +0200
    @@ -393,7 +393,7 @@
     	}
         }
     
    -    *encodingPtr = Tcl_GetEncoding(NULL, "utf-8");
    +    *encodingPtr = NULL;
         *lengthPtr = Tcl_DStringLength(&ds);
         *valuePtr = (char *)ckalloc(*lengthPtr + 1);
         memcpy(*valuePtr, Tcl_DStringValue(&ds), *lengthPtr + 1);
    
    Diff finished.  Wed May 22 19:31:12 2024
    
  3. login: "ralfixx"
  4. mimetype: "text/plain"