Overview
| Artifact ID: | 4ef39dee603c092c1dde4607d61f79c38583c6a5b91b9b99749a931510fdac68 |
|---|---|
| Ticket: | 8c4082563d6a7ebda4b8804e221a0de31c4c40e4
Tcl_Eval() result can be truncated at NUL |
| User & Date: | serhiy.storchaka on 2024-06-23 12:50:42 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-markdown"
- 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() ``` - foundin changed to: "8.6"
- is_private changed to: "0"
- login: "serhiy.storchaka"
- priority changed to: "5 Medium"
- private_contact changed to: "c62cfa96d21fcf0294bdb27e141b4604475a3f46"
- resolution changed to: "None"
- severity changed to: "Minor"
- status changed to: "Open"
- submitter changed to: "serhiy.storchaka"
- subsystem changed to: "11. Conversions from String"
- title changed to: "Tcl_Eval() result can be truncated at NUL"
- type changed to: "Bug"