Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 2121dfbc375793b34deaaf510b72d2dc29d3dfb4
Ticket: 2089279fffffffffffffffffffffffffffffffff
StringObj.3 Tcl_ObjPrintf inaccuracies
User & Date: georgeps on 2008-09-02 16:27:27
Changes

  1. assignee changed to: "hobbs"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    StringObj.3 gives this example:
    long x = 5;
    Tcl_Obj *objPtr = Tcl_ObjPrintf("Value is %d", x);
    
    This is incorrect, because the Tcl_ObjPrintf code will expect an int, and use va_arg with int.
    
    As can be seen in the AppendPrintfToObjVA:
     case 'd':
       ...
       seekingConversion = 0;
       switch (size) {
          case -1:
          case 0:
            Tcl_ListObjAppendElement(NULL, list, Tcl_NewLongObj((long int)va_arg(argList, int)));
    
    The issue is that if the user uses %d for a long, the va_list will have the wrong offset.  The example will work in the 1 argument case, and even the multiple argument case if sizeof(int) = sizeof(long).  It however will fail in the case of sizeof(long) > sizeof(int), because the va_list will have the wrong offset.  Thus you'd be getting parts of the integer.  This can have disasterous consequences if the offset is wrong, because the next va_arg call becomes wrong, which if it's a pointer may lead to segfaults.
    
    Tcl works with some platforms where a long is 64-bit and an int is 32-bit.  #ifdef TCL_WIDE_INT_IS_LONG as I understand it refers to this.
    
    The next issue is that the format specifiers are not exactly the same as the [format] command, and probably shouldn't be.
    
    The format command uses (as documented) %ld for truncation to a 64-bit integer.  %lld for no truncation (BigNum) and %d for a 32-bit integer.  This is incomptible with AppendPrintfToObjVA() (used by Tcl_ObjPrintf), because it currently accepts a long for %ld which may or may not be 64-bit depending on the platform.  The wide and BigNum cases are also not handled by AppendPrintfToObjVA().  Thus the suggestion in the documentation that Tcl_ObjPrintf has the same syntax as the format command are wrong.
    
    
  4. foundin changed to: "obsolete: 8.6a2"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "georgeps"
  10. subsystem changed to: "55. Other Tools"
  11. title changed to: "StringObj.3 Tcl_ObjPrintf inaccuracies"
  12. type changed to: "Bug"