Skip to content
Tcl Source Code History Of Ticket 8c4082563d6a7ebd
Login

History Of Ticket 8c4082563d6a7ebd

Artifacts Associated With Ticket 8c4082563d6a7ebd

  1. Ticket change [4ef39dee60] (rid 228088) by serhiy.storchaka on 2024-06-23 12:50:42:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-markdown"
    4. comment initialized to:
      I cannot reproduce this bug with pure Tcl, I can only reproduce it with Tkinter (where it was first [discovered](https://github.com/python/cpython/issues/119614)) and with the C API.
      
      Simplified Python/Tkinter script:
      
      ```py
      import tkinter
      tcl = tkinter.Tcl()
      tcl.setvar('a', 'abc\0def')
      print(repr(tcl.getvar('a')))
      print(repr(tcl.call('set', 'a')))
      print(repr(tcl.eval('set a')))
      ```
      Output:
      ```
      'abc\x00def'
      'abc\x00def'
      'abc'
      ```
      
      The corresponding C program:
      ```c
      #include <tcl.h>
      #ifndef TCL_SIZE_MAX
      typedef int Tcl_Size;
      #endif
      
      int AppInit(Tcl_Interp *interp)
      {
          Tcl_Obj *val;
          Tcl_Size slen, ulen;
          Tcl_Init(interp);
      //     val = Tcl_NewStringObj("abc\0def", 7);
          val = Tcl_NewStringObj("abc\300\200def", 8);
          Tcl_SetVar2Ex(interp, "a", NULL, val, TCL_LEAVE_ERR_MSG);
      
          val = Tcl_GetVar2Ex(interp, "a", NULL, TCL_LEAVE_ERR_MSG);
          Tcl_GetStringFromObj(val, &slen);
          Tcl_GetUnicodeFromObj(val, &ulen);
          printf("Tcl_GetVar2Ex: %d %d\n", (int)slen, (int)ulen);
      
          Tcl_Obj *objv[2] = {Tcl_NewStringObj("set", -1), Tcl_NewStringObj("a", -1)};
          Tcl_EvalObjv(interp, 2, objv, TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL);
          val = Tcl_GetObjResult(interp);
          Tcl_GetStringFromObj(val, &slen);
          Tcl_GetUnicodeFromObj(val, &ulen);
          printf("Tcl_EvalObjv: %d %d\n", (int)slen, (int)ulen);
      
          Tcl_Eval(interp, "set a");
          val = Tcl_GetObjResult(interp);
          Tcl_GetStringFromObj(val, &slen);
          Tcl_GetUnicodeFromObj(val, &ulen);
          printf("Tcl_Eval: %d %d\n", (int)slen, (int)ulen);
          return TCL_OK;
      }
      
      int main(int argc, char *argv[])
      {
          Tcl_Main(argc, argv, AppInit);
          return 0;
      }
      ```
      Output:
      ```
      Tcl_GetVar2Ex: 7 7
      Tcl_EvalObjv: 7 7
      Tcl_Eval: 3 3
      ```
      
      You can see that `Tcl_GetVar2Ex` and `Tcl_EvalObjv` return a value with length 7, as expected, but `Tcl_Eval` returns a truncated value with length 3. According to the documentation, `Tcl_NewStringObj` supports a string with embedded null bytes if the non-negative length is provided. And it works in most cases except `Tcl_Eval`.
      
      When replace the null byte with `\300\200`, or when use `Tcl_NewUnicodeObj` with embedded null character, all methods return the same value.
      
      I suppose that this is the cause of truncating strings with embedded null bytes in the Tk widgets. Here is a Python/Tkinter example that shows the truncation in Tk:
      
      ```py
      import tkinter
      b = tkinter.Button(text='abc\0def')
      b.pack()  # shows a button with text "abc"
      print(repr(b.tk.call(str(b), 'cget', '-text')))  # outputs 'abc\x00def'
      print(repr(b.tk.eval(str(b) + ' cget -text')))  # outputs 'abc'
      tkinter.mainloop()
      ```
      
    5. foundin initialized to: "8.6"
    6. is_private initialized to: "0"
    7. login: "serhiy.storchaka"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "c62cfa96d21fcf0294bdb27e141b4604475a3f46"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "serhiy.storchaka"
    14. subsystem initialized to: "11. Conversions from String"
    15. title initialized to: "Tcl_Eval() result can be truncated at NUL"
    16. type initialized to: "Bug"
  2. Ticket change [189037c6bd] (rid 228089) by serhiy.storchaka on 2024-06-23 12:51:59:

    1. comment changed to:
      I cannot reproduce this bug with pure Tcl, I can only reproduce it with Tkinter (where it was first [discovered](https://github.com/python/cpython/issues/119614)) and with the C API.
      
      Simplified Python/Tkinter script:
      
      ```py
      import tkinter
      tcl = tkinter.Tcl()
      tcl.setvar('a', 'abc\0def')
      print(repr(tcl.getvar('a')))
      print(repr(tcl.call('set', 'a')))
      print(repr(tcl.eval('set a')))
      ```
      Output:
      ```
      'abc\x00def'
      'abc\x00def'
      'abc'
      ```
      
      The corresponding C program:
      
      ```c
      #include <tcl.h>
      #ifndef TCL_SIZE_MAX
      typedef int Tcl_Size;
      #endif
      
      int AppInit(Tcl_Interp *interp)
      {
          Tcl_Obj *val;
          Tcl_Size slen, ulen;
          Tcl_Init(interp);
      //     val = Tcl_NewStringObj("abc\0def", 7);
          val = Tcl_NewStringObj("abc\300\200def", 8);
          Tcl_SetVar2Ex(interp, "a", NULL, val, TCL_LEAVE_ERR_MSG);
      
          val = Tcl_GetVar2Ex(interp, "a", NULL, TCL_LEAVE_ERR_MSG);
          Tcl_GetStringFromObj(val, &slen);
          Tcl_GetUnicodeFromObj(val, &ulen);
          printf("Tcl_GetVar2Ex: %d %d\n", (int)slen, (int)ulen);
      
          Tcl_Obj *objv[2] = {Tcl_NewStringObj("set", -1), Tcl_NewStringObj("a", -1)};
          Tcl_EvalObjv(interp, 2, objv, TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL);
          val = Tcl_GetObjResult(interp);
          Tcl_GetStringFromObj(val, &slen);
          Tcl_GetUnicodeFromObj(val, &ulen);
          printf("Tcl_EvalObjv: %d %d\n", (int)slen, (int)ulen);
      
          Tcl_Eval(interp, "set a");
          val = Tcl_GetObjResult(interp);
          Tcl_GetStringFromObj(val, &slen);
          Tcl_GetUnicodeFromObj(val, &ulen);
          printf("Tcl_Eval: %d %d\n", (int)slen, (int)ulen);
          return TCL_OK;
      }
      
      int main(int argc, char *argv[])
      {
          Tcl_Main(argc, argv, AppInit);
          return 0;
      }
      ```
      Output:
      ```
      Tcl_GetVar2Ex: 7 7
      Tcl_EvalObjv: 7 7
      Tcl_Eval: 3 3
      ```
      
      You can see that `Tcl_GetVar2Ex` and `Tcl_EvalObjv` return a value with length 7, as expected, but `Tcl_Eval` returns a truncated value with length 3. According to the documentation, `Tcl_NewStringObj` supports a string with embedded null bytes if the non-negative length is provided. And it works in most cases except `Tcl_Eval`.
      
      When replace the null byte with `\300\200`, or when use `Tcl_NewUnicodeObj` with embedded null character, all methods return the same value.
      
      I suppose that this is the cause of truncating strings with embedded null bytes in the Tk widgets. Here is a Python/Tkinter example that shows the truncation in Tk:
      
      ```py
      import tkinter
      b = tkinter.Button(text='abc\0def')
      b.pack()  # shows a button with text "abc"
      print(repr(b.tk.call(str(b), 'cget', '-text')))  # outputs 'abc\x00def'
      print(repr(b.tk.eval(str(b) + ' cget -text')))  # outputs 'abc'
      tkinter.mainloop()
      ```
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/plain"
  3. Ticket change [583f1bef7f] (rid 228090) by sebres on 2024-06-23 13:44:11:

    1. closedate changed to: "2460485.07235287"
    2. closer changed to: "sebres"
    3. icomment:
      Null-char is not always equal 0.<br/>
      0-byte normally always means a NTS-zero char and can cause end of string in some internal operations...<br/>
      Moreover in Tcl's utf-8 it is not proper character: you have to use either `\xC0\x80` instead of `\0`-char (for pure utf-8 string tcl-objects) or `Tcl_ExternalToUtf`, `Tcl_ExternalToUtfDString` (for strings from external data using encoding) or `Tcl_NewByteArrayObj`, `Tcl_SetByteArrayObj` etc (for binary data).
      
      Thus closed as invalid.
      
    4. login: "sebres"
    5. mimetype: "text/x-markdown"
    6. resolution changed to: "Invalid"
    7. status changed to: "Closed"
  4. Ticket change [67e146dfbe] (rid 228091) by serhiy.storchaka on 2024-06-23 14:04:21:

    1. closedate changed to: "2460485.08636199"
    2. closer changed to: "serhiy.storchaka"
    3. icomment:
      I tested also other eval functions (they are not used in Tkinter). `Tcl_EvalObjEx`, `Tcl_EvalEx` and `Tcl_GlobalEvalObj` do not truncate the result. `Tcl_VarEval` and `Tcl_GlobalEval` truncate the result.
      
      Looking at the code of `Tcl_Eval`, it is simply `Tcl_EvalEx` followed by `Tcl_GetStringResult`. So the result is truncated in `Tcl_GetStringResult`.
      
      The documentation for `Tcl_NewStringObj` implies that embedded null bytes are acceptable if the length is specified.
      
    4. login: "serhiy.storchaka"
    5. mimetype: "text/x-markdown"