Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: b23cf44e38577bd17d04ccb399826713be249ddec2f32aa25f39c00580cd6c83
Ticket: f2ff05fc8411be80a93913b2b065f2c85fa9bcf0
Usage of static systemEncoding is not thread safe (vulnerability)
User & Date: sebres on 2021-04-15 15:33:49
Changes

  1. icomment:
    Well, TSD solution would be possible, but in simplest case it would imply indirectly that the setting of system encoding affects only current thread (and all of its interpreters), what would be backwards incompatible.
    
    Here is a diff explaining that:
    <code><pre style="padding-left:10pt">
    # [T0] main thread set system encoding to "cp1252"
    ...
    # [T1] then at some point thread 1 doing:
    % encoding system utf-8
    utf-8
    # [T2] hereafter in other thread:
    % encoding system
    <b style="color:red">-utf-8</b>
    <b style="color:green">+cp1252</b>
    </pre></code>
    
    Sure, one could additionally store an epoch in TSD and compare it with a common static epoch which gets incremented by Tcl_SetSystemEncoding, so an access by Tcl_GetEncoding(interp, NULL) could check it and invalidate cached in TSD encoding if epoch changed globally.
    
    Just whole performance of that (TSD+epoch) will be almost comparable to a simple lock variant (in case of NULL by encoding), so similat to: get and incr ref in lock, convert, and decr ref in lock at end.
    
    FWIW: in my fork I used similar way, without TSD and interim lock (with atomic refCount increment/decrement and an epoch used only for Tcl_ObjType "encoding" cached in tcl-object).
    
  2. login: "sebres"
  3. mimetype: "text/x-fossil-wiki"