Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Simple conceptual error in code for Tcl_GetIndexFromObjStruc

Closed / Fixed219082ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:10:25
OriginalBugID: 5153 Bug
Version: 8.3
SubmitDate: '2000-04-18'
LastModified: '2000-06-03'
Severity: SER
Status: Released
Submitter: techsupp
ChangedBy: hobbs
OS: Linux
FixedDate: '2000-06-03'
FixedInVersion: 8.4a1
ClosedDate: '2000-10-25'


Name:

Eric Perkins


CVS:

RCS: @(#) $Id: tclIndexObj.c,v 1.4 1999/06/28 23:49:31 redman Exp $


CustomShell:

none


Comments:

the patch works as far as i've tested it.


ReproducibleScript:

//The following command is created in the interpreter and called with the syntax:

// "mycommand key2"


int mycommand(ClientData, Tcl_Interp * interp, int objc, Tcl_Obj * const objv[]) {


  typedef struct {char * key; ClientData d1; ClientData d2} MyStruct;


  MyStruct table[] = {

    {"key1",1,2},

    {"key2",3,4},

    {"key3",5,6}

  };

  int index;


  int rv = Tcl_GetIndexFromObjStruct(interp,objv[1],(char**)table,sizeof(MyStruct),"option",0,&index);


  Tcl_WrongNumArgs(interp,2,obvj," rest of args");

  return TCL_OK;

}


ObservedBehavior:

segv during Tcl_WrongNumArgs


The reason:

the of the object for the first argument gets set to &table[0].key as it should, but the    internalRep.twoPtrValue.ptr2 gets set to 1 (the number ofsets away from the beginning)

when the string is retrieved from the table in Tcl_WrongNumArgs, the following expression is used:

     tablePtr[(int) objv[i]->internalRep.twoPtrValue.ptr2]

which evaluates to ((char **)table)[1] which is, of course table[0].d1 and not table[1].key 

the internalRep.twoPtrValue.ptr2 should instead get set to (index * offset/sizeof(char*)), in this case, index*3 = 3. which evaluates correctly as ((char**)table)[3] = table[1].key


DesiredBehavior:

see above


Patch:

*** /home/edp/p1-edp/in/tcl8.3.0/generic/tclIndexObj.c  Mon Jun 28 19:49:31 1999

--- tclIndexObj.c       Tue Apr 18 14:59:59 2000

***************

*** 213,219 ****

        objPtr->typePtr->freeIntRepProc(objPtr);

      }

      objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) tablePtr;

!     objPtr->internalRep.twoPtrValue.ptr2 = (VOID *) index;

      objPtr->typePtr = &tclIndexType;

      *indexPtr = index;

      return TCL_OK;

--- 213,219 ----

        objPtr->typePtr->freeIntRepProc(objPtr);

      }

      objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) tablePtr;

!     objPtr->internalRep.twoPtrValue.ptr2 = (VOID *) (index*offset/sizeof(char*));

      objPtr->typePtr = &tclIndexType;

      *indexPtr = index;

      return TCL_OK;


PatchFiles:

tclIndexObj.c




Verified and patched for 8.4a1. 
-- 06/03/2000 hobbs
welch Open ticket [219082ffff]. [History diff]2000-10-26 08:34:55
sheltondplain2000-11-24 16:43:51
Unfortunately, this fix breaks the code which returns the cached value from a previous lookup using the same object. The code at the top of Tcl_GetIndexFromObjStruct simply returns the value from the ptr2 field of the structure. Due to the change above, this now contains (in the above example) 3. In fact, the value which should be returned is 1.

>if ((objPtr->typePtr == &tclIndexType) &&
>    (objPtr->internalRep.twoPtrValue.ptr1
>    == (VOID *) tablePtr)) {
>*indexPtr = (int)objPtr->internalRep.twoPtrValue.ptr2;
>return TCL_OK;
>}

I would suggest a fix along the lines of this (sorry about the abysmal formatting here, the input box it too small to see what one is doing):

>if ((objPtr->typePtr == &tclIndexType) &&
>    (objPtr->internalRep.twoPtrValue.ptr1
>    == (VOID *) tablePtr)) {
>*indexPtr = (int)objPtr->internalRep.twoPtrValue.ptr2 /
>           (offset/sizeof(char*));
>return TCL_OK;
>}

This will cause the cached value to be returned correctly.
sheltondplain2000-11-24 16:52:01
BTW, I am working with version 8.3.2, though the same problem appears to be in the head of CVS.
dkfplain2000-11-24 17:27:45
Reopened.
dkfplain2000-11-24 18:06:25
OK, I've got a test that detects the original bug and the fault with the first fix.  I'll check that, plus a fix that will work more universally, into CVS shortly
dkf Closed ticket [219082ffff]. [History diff]2000-11-24 18:28:07
Log in (anonymous is fine) to add a comment.