Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2018-01-26
10:15 • Ticket [e5691de2eb] Tcl_ObjPrintf() - 'Unable to format "%p" with ... artifact: 40268316e5 user: jan.nijtmans
10:14 • Ticket [e5691de2eb]: 8 changes artifact: 2147c79722 user: jan.nijtmans
2018-01-24
07:45 • Closed ticket [e5691de2eb]. artifact: 5126c803ae user: oehhar
05:58 • Ticket [e5691de2eb]: 3 changes artifact: 3da7ec8a4b user: zakhmak
2018-01-23
20:26 • Ticket [e5691de2eb]: 4 changes artifact: fb9773265f user: oehhar
20:18 • Ticket [e5691de2eb]: 3 changes artifact: e51d4a9ea7 user: dgp
19:56 • Ticket [e5691de2eb]: 3 changes artifact: 66d0a8f68a user: zakhmak
19:54 • Ticket [e5691de2eb]: 3 changes artifact: 1289b62766 user: zakhmak
19:39 • New ticket [e5691de2eb]. artifact: 156b227115 user: zakhmak

Tcl_ObjPrintf() - 'Unable to format "%p" with supplied arguments: '

Closed / Duplicatee5691de2eb49302428e77726a057449a06f499daCreated 9 years agoUpdated 9 years ago
Descriptionplain2018-01-23 19:39:35
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;
}
zakhmak Ticket [e5691de2eb]: 3 changes [History diff]2018-01-23 19:54:00
zakhmak Ticket [e5691de2eb]: 3 changes [History diff]2018-01-23 19:56:14
dgpplain2018-01-23 20:18:51
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.
oehharwiki2018-01-23 20:26:23

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:

% format "%p" 12345
bad field specifier "p"

I have no experience here, just reading the man page...

Hope this helps, Harald

zakhmakplain2018-01-24 05:58:13
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
oehhar Closed ticket [e5691de2eb]. [History diff]2018-01-24 07:45:39
jan.nijtmansplain2018-01-26 10:14:21
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.
jan.nijtmanswiki2018-01-26 10:15:03

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 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 z_modifier branch, to see if this approach works for you.

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