Artifacts Associated With Ticket e5691de2eb493024
Ticket change [156b227115] (rid 160430) by zakhmak on 2018-01-23 19:39:35:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
Hello, I post this ticket at https://core.tcl.tk/tcllib/tktview?name=d94018f4763cde00c8e8976da8a4cf86d8509de4 I was told to post my ticket here instead. You will find at the end a C code example of a simplify case that nevertheless show the point I discuss below. --------- I am working to wrap by myself some C functions to Tcl commands. I use for this purpose handles as command as suggested on wiki.tcl.tk/1089 - First part of the handle string is a short text telling the kind of struct tranfered as ClientData. - Second part consists in the pointer value concerning the ClientData struct so that I can also retrieve the pointer of other structs and not only the one of the current called command. I just discover the Tcl_ObjPrintf() function and I tried to used it for this prupose but the format specifier %p seems not to be supported. I get following message returned: 'Unable to format "%p" with supplied arguments: ' I check my pointer before calling ObjPrintf("%p",ptr) and it's valid. Since the function name is related to printf() that supports "%p" format specifier, it looks like a bug for me. Could anyone maybe explain me wether I am right or wrong and why concerning that point? Thanks in advance for any reply and regards --------- #include <tcl.h> int Test_ObjPrintfObjCmd( ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { Tcl_DString dynstr; Tcl_Obj *obj = NULL; char *text = NULL, buftxt[128] = ""; static const char *modes[] = {"without_ObjPrintf","with_ObjPrintf",NULL}; static const char formatstr[] = "answer is at %p and its value is %d"; int ok, imode = 0, answer = 42; Tcl_DStringInit(&dynstr); if (objc != 2) { sprintf(buftxt, "Usage : Test_ObjPrintf mode"); Tcl_WrongNumArgs(interp,0,objv,buftxt); return TCL_ERROR; } ok = Tcl_GetIndexFromObj(interp, objv[1], modes, "unknown mode", TCL_EXACT, &imode); if (ok == TCL_ERROR) return TCL_ERROR; switch (imode) { case 0: /* without_ObjPrintf */ sprintf(buftxt, formatstr,&answer, answer); obj = Tcl_NewStringObj(buftxt,-1); break; case 1: /* with_ObjPrintf */ obj = Tcl_ObjPrintf(formatstr,&answer, answer); break; } if (obj == NULL) return TCL_ERROR; text = Tcl_GetStringFromObj(obj,NULL); if (text == NULL) return TCL_ERROR; Tcl_DStringAppend(&dynstr,text,-1); Tcl_DStringResult(interp,&dynstr); return TCL_OK; } int Pb_objprintf_Init(Tcl_Interp *interp) { Tcl_Command cmd; cmd = Tcl_CreateObjCommand(interp, "Test_ObjPrintf", Test_ObjPrintfObjCmd, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); if (cmd == NULL) return TCL_ERROR; return TCL_OK; } - foundin initialized to: "Tcl 8.6.7"
- is_private initialized to: "0"
- login: "zakhmak"
- priority initialized to: "5 Medium"
- private_contact initialized to: "a3cbfdb98acf5ab533a2e1324bab5c113eeb5754"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "zakhmak"
- subsystem initialized to: "11. Conversions from String"
- title initialized to:
Tcl_ObjPrintf() - 'Unable to format "%p" with supplied arguments: '
- type initialized to: "Bug"
Ticket change [1289b62766] (rid 160431) by zakhmak on 2018-01-23 19:54:00:
- comment changed to:
Hello, I posted this ticket at https://core.tcl.tk/tcllib/tktview?name=d94018f4763cde00c8e8976da8a4cf86d8509de4 I was told to post my ticket here instead since it should be more relevant. You will find at the end a C code example of a simplify case that nevertheless show the point I discuss below. --------- I am working to wrap by myself some C functions to Tcl commands. I use for this purpose handles as command as suggested on wiki.tcl.tk/1089 - First part of the handle string is a short text telling the kind of struct tranfered as ClientData. - Second part consists in the pointer value concerning the ClientData struct so that I can also retrieve the pointer of other structs and not only the one of the current called command. I just discover the Tcl_ObjPrintf() function and I tried to used it for this prupose but the format specifier %p seems not to be supported. I get following message returned: 'Unable to format "%p" with supplied arguments: ' I check my pointer before calling ObjPrintf("%p",ptr) and it's valid. Since the function name is related to printf() that supports "%p" format specifier, it looks like a bug for me. Could anyone maybe explain me wether I am right or wrong and why concerning that point? Thanks in advance for any reply and regards --------- #include <tcl.h> int Test_ObjPrintfObjCmd( ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { Tcl_DString dynstr; Tcl_Obj *obj = NULL; char *text = NULL, buftxt[128] = ""; static const char *modes[] = {"without_ObjPrintf","with_ObjPrintf",NULL}; static const char formatstr[] = "answer is at %p and its value is %d"; int ok, imode = 0, answer = 42; Tcl_DStringInit(&dynstr); if (objc != 2) { sprintf(buftxt, "Usage : Test_ObjPrintf mode"); Tcl_WrongNumArgs(interp,0,objv,buftxt); return TCL_ERROR; } ok = Tcl_GetIndexFromObj(interp, objv[1], modes, "unknown mode", TCL_EXACT, &imode); if (ok == TCL_ERROR) return TCL_ERROR; switch (imode) { case 0: /* without_ObjPrintf */ sprintf(buftxt, formatstr,&answer, answer); obj = Tcl_NewStringObj(buftxt,-1); break; case 1: /* with_ObjPrintf */ obj = Tcl_ObjPrintf(formatstr,&answer, answer); break; } if (obj == NULL) return TCL_ERROR; text = Tcl_GetStringFromObj(obj,NULL); if (text == NULL) return TCL_ERROR; Tcl_DStringAppend(&dynstr,text,-1); Tcl_DStringResult(interp,&dynstr); return TCL_OK; } int Pb_objprintf_Init(Tcl_Interp *interp) { Tcl_Command cmd; cmd = Tcl_CreateObjCommand(interp, "Test_ObjPrintf", Test_ObjPrintfObjCmd, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); if (cmd == NULL) return TCL_ERROR; return TCL_OK; } - login: "zakhmak"
- mimetype: "text/plain"
- comment changed to:
Ticket change [66d0a8f68a] (rid 160432) by zakhmak on 2018-01-23 19:56:14:
- comment changed to:
Hello, I posted this ticket at https://core.tcl.tk/tcllib/tktview?name=d94018f4763cde00c8e8976da8a4cf86d8509de4 I was told to post my ticket here instead because here should be a more convenient place complying with problem topic. You will find at the end a C code example of a simplify case that nevertheless show the point I discuss below. --------- I am working to wrap by myself some C functions to Tcl commands. I use for this purpose handles as command as suggested on wiki.tcl.tk/1089 - First part of the handle string is a short text telling the kind of struct tranfered as ClientData. - Second part consists in the pointer value concerning the ClientData struct so that I can also retrieve the pointer of other structs and not only the one of the current called command. I just discover the Tcl_ObjPrintf() function and I tried to used it for this prupose but the format specifier %p seems not to be supported. I get following message returned: 'Unable to format "%p" with supplied arguments: ' I check my pointer before calling ObjPrintf("%p",ptr) and it's valid. Since the function name is related to printf() that supports "%p" format specifier, it looks like a bug for me. Could anyone maybe explain me wether I am right or wrong and why concerning that point? Thanks in advance for any reply and regards --------- #include <tcl.h> int Test_ObjPrintfObjCmd( ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { Tcl_DString dynstr; Tcl_Obj *obj = NULL; char *text = NULL, buftxt[128] = ""; static const char *modes[] = {"without_ObjPrintf","with_ObjPrintf",NULL}; static const char formatstr[] = "answer is at %p and its value is %d"; int ok, imode = 0, answer = 42; Tcl_DStringInit(&dynstr); if (objc != 2) { sprintf(buftxt, "Usage : Test_ObjPrintf mode"); Tcl_WrongNumArgs(interp,0,objv,buftxt); return TCL_ERROR; } ok = Tcl_GetIndexFromObj(interp, objv[1], modes, "unknown mode", TCL_EXACT, &imode); if (ok == TCL_ERROR) return TCL_ERROR; switch (imode) { case 0: /* without_ObjPrintf */ sprintf(buftxt, formatstr,&answer, answer); obj = Tcl_NewStringObj(buftxt,-1); break; case 1: /* with_ObjPrintf */ obj = Tcl_ObjPrintf(formatstr,&answer, answer); break; } if (obj == NULL) return TCL_ERROR; text = Tcl_GetStringFromObj(obj,NULL); if (text == NULL) return TCL_ERROR; Tcl_DStringAppend(&dynstr,text,-1); Tcl_DStringResult(interp,&dynstr); return TCL_OK; } int Pb_objprintf_Init(Tcl_Interp *interp) { Tcl_Command cmd; cmd = Tcl_CreateObjCommand(interp, "Test_ObjPrintf", Test_ObjPrintfObjCmd, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); if (cmd == NULL) return TCL_ERROR; return TCL_OK; } - login: "zakhmak"
- mimetype: "text/plain"
- comment changed to:
Ticket change [e51d4a9ea7] (rid 160434) by dgp on 2018-01-23 20:18:51:
- icomment:
Documentation for Tcl_ObjPrintf() is here http://www.tcl.tk/man/tcl8.6/TclLib/StringObj.htm Tcl_ObjPrintf() is a C routine providing access to the same formatting engine that drives the [format] command. http://www.tcl.tk/man/tcl8.6/TclCmd/format.htm The [format] command does not support a %p conversion specifier, so neither does Tcl_ObjPrintf(). No bug here. It's conceivable that adding %p to both [format] and Tcl_ObjPrintf() could be a feature request, but it seems like a risky and ill-considered one. Exposing pointer values to scripts or messages for anything other than debugging purposes does not seem like a good idea to me at all.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [fb9773265f] (rid 160435) by oehhar on 2018-01-23 20:26:23:
- icomment:
Thank you for the ticket. If you read the manpage of Tcl_ObjPrintf [http://www.tcl.tk/man/tcl8.6/TclLib/StringObj.htm] It states: The formatting is done with the same core formatting engine used by Tcl_Format. This means the set of supported conversion specifiers is that of the *format* command and not that of the *sprintf* routine where the two sets differ. If you look on the *format* man page [http://www.tcl.tk/man/tcl8.6/TclCmd/format.htm], there is no "%p" as format. Pointers are not supported. Nevertheless, the provided error message might be missleading or even a bug. The format error message is: <verbatim> % format "%p" 12345 bad field specifier "p" </verbatim> I have no experience here, just reading the man page... Hope this helps, Harald
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [3da7ec8a4b] (rid 160444) by zakhmak on 2018-01-24 05:58:13:
- icomment:
Hello, Thanks for your explanations both of you. I understand better now. The documentation is huge and I hadn't the reflex to had a closer look on the sections you mentioned. Concerning my problem, I think, I have to develop this lookup bidirectional function to map Tcl representation with their C pointer and vice versa. I guess finally it is ok so and that you can close my ticket. Thanks for your support again
- login: "zakhmak"
- mimetype: "text/plain"
- icomment:
Ticket change [5126c803ae] (rid 160445) by oehhar on 2018-01-24 07:45:39:
- closedate changed to: "2458142.82337153"
- closer changed to: "oehhar"
- login: "oehhar"
- mimetype: "text/plain"
- resolution changed to: "Rejected"
- status changed to: "Closed"
Ticket change [2147c79722] (rid 160508) by jan.nijtmans on 2018-01-26 10:14:21:
- assignee changed to: "jan.nijtmans"
- closedate changed to: "2458144.92663234"
- closer changed to: "jan.nijtmans"
- icomment:
Actually, I don't think formatting a pointer value is dangerous at all: It just gives the hex representation of the memory address. What would be dangerous to - possibly at a later time - use this hex value to access this same memory again: The memory content could be different then (possibly freed), waiting for a disaster to happen. A proposal for adding %p is part of [http://core.tcl.tk/tips/doc/trunk/tip/476.md|TIP 476]. Therefore marking this ticket as RFE (Request for Extension) and as a duplicate for this TIP. Thanks for reporting it! You could try the [http://core.tcl.tk/tcl/info/afb68da53ed1658f|z_modifier] branch, to see if this approach works for you.
- login: "jan.nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Duplicate"
- type changed to: "RFE"
Ticket change [40268316e5] (rid 160509) by jan.nijtmans on 2018-01-26 10:15:03:
- closedate changed to: "2458144.92712806"
- icomment:
Actually, I don't think formatting a pointer value is dangerous at all: It just gives the hex representation of the memory address. What would be dangerous to - possibly at a later time - use this hex value to access this same memory again: The memory content could be different then (possibly freed), waiting for a disaster to happen. A proposal for adding %p is part of [http://core.tcl.tk/tips/doc/trunk/tip/476.md|TIP 476]. Therefore marking this ticket as RFE (Request for Extension) and as a duplicate for this TIP. Thanks for reporting it! You could try the [http://core.tcl.tk/tcl/info/afb68da53ed1658f|z_modifier] branch, to see if this approach works for you.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"