Artifacts Associated With Ticket 8c4082563d6a7ebd
Ticket change [4ef39dee60] (rid 228088) by serhiy.storchaka on 2024-06-23 12:50:42:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- 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() ``` - foundin initialized to: "8.6"
- is_private initialized to: "0"
- login: "serhiy.storchaka"
- priority initialized to: "5 Medium"
- private_contact initialized to: "c62cfa96d21fcf0294bdb27e141b4604475a3f46"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "serhiy.storchaka"
- subsystem initialized to: "11. Conversions from String"
- title initialized to: "Tcl_Eval() result can be truncated at NUL"
- type initialized to: "Bug"
Ticket change [189037c6bd] (rid 228089) by serhiy.storchaka on 2024-06-23 12:51:59:
- 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() ``` - login: "serhiy.storchaka"
- mimetype: "text/plain"
- comment changed to:
Ticket change [583f1bef7f] (rid 228090) by sebres on 2024-06-23 13:44:11:
- closedate changed to: "2460485.07235287"
- closer changed to: "sebres"
- 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.
- login: "sebres"
- mimetype: "text/x-markdown"
- resolution changed to: "Invalid"
- status changed to: "Closed"
Ticket change [67e146dfbe] (rid 228091) by serhiy.storchaka on 2024-06-23 14:04:21:
- closedate changed to: "2460485.08636199"
- closer changed to: "serhiy.storchaka"
- 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.
- login: "serhiy.storchaka"
- mimetype: "text/x-markdown"