Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Tcl_Eval() result can be truncated at NUL

Closed / Invalid8c4082563d6a7ebda4b8804e221a0de31c4c40e4Created 2 years agoUpdated 2 years ago
Descriptionmarkdown2024-06-23 12:50:42

I cannot reproduce this bug with pure Tcl, I can only reproduce it with Tkinter (where it was first discovered) and with the C API.

Simplified Python/Tkinter script:

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:

#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:

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()
serhiy.storchaka Ticket [8c4082563d]: 3 changes [History diff]2024-06-23 12:51:59
sebresmarkdown2024-06-23 13:44:11

Null-char is not always equal 0.
0-byte normally always means a NTS-zero char and can cause end of string in some internal operations...
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.

serhiy.storchakamarkdown2024-06-23 14:04:21

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.

Log in (anonymous is fine) to add a comment.