Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ee4213af8f2c47fa08ba169fb47bebb5964db4ca
Ticket: 7a87a9bc5b213d2d57848e263f12636bb8d945a2
Invalid write in tclExecute.c
User & Date: pointsman on 2015-08-13 13:22:13
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    With [ca76ce47255c6] (current trunk), valgrind shows an invalid write:
    
    	==14375== Invalid write of size 8
    	==14375==    at 0x4F51D0C: TEBCresume (tclExecute.c:4189)
    	==14375==    by 0x4E7B147: TclNRRunCallbacks (tclBasic.c:4389)
    	==14375==    by 0x4E7AA15: Tcl_EvalObjv (tclBasic.c:4119)
    	==14375==    by 0x4E7CE94: TclEvalEx (tclBasic.c:5258)
    	==14375==    by 0x4E7C233: Tcl_EvalEx (tclBasic.c:4923)
    
    The bug is triggered for me by something, that the tcltest framework does. I see it e.g. with a call
    
    valgrind ./tcltest ../tests/all.tcl -file foo.bar
    
    (Note, that this actually doesn't run any tests. The invalid read also shows, if there are tests executed.)
    
    It seems, the problem was introduced by check-in [41d3e42dda] (2015-07-18). It's about this code block:
    
    4181: 		if (!TclIsVarUndefined(varPtr)) {
    4182: 		    TclDecrRefCount(varPtr->value.objPtr);
    4183: 		    TclSetVarUndefined(varPtr);
    4184: 		    TclClearVarNamespaceVar(varPtr);
    4185: 		    TclCleanupVar(varPtr, arrayPtr);
    4186: 		} else if (flags & TCL_LEAVE_ERR_MSG) {
    4187: 		    goto slowUnsetArray;
    4188: 		}
    4189: 		varPtr->value.objPtr = NULL;
    
    The check-in 41d3e42dda added at this place the 3 lines 4183-4185. Now, the new line 4185
    
     TclCleanupVar(varPtr, arrayPtr);
    
    obviously ckfrees varPtr. This results now - in line 4189 - in an invalid write.
    
    A possible fix seem easy (see below). With that change, the test suite run with the same result as without this change. But I'm just looking at this few lines, without deeper understandig of that code and what happens in detail. So, please check.
    
    --- tclExecute.c
    +++ tclExecute.c
    @@ -4183,12 +4183,13 @@
     		    TclSetVarUndefined(varPtr);
     		    TclClearVarNamespaceVar(varPtr);
     		    TclCleanupVar(varPtr, arrayPtr);
     		} else if (flags & TCL_LEAVE_ERR_MSG) {
     		    goto slowUnsetArray;
    -		}
    -		varPtr->value.objPtr = NULL;
    +		} else {
    +                    varPtr->value.objPtr = NULL;
    +                }
     		TRACE_APPEND(("OK\n"));
     		NEXT_INST_F(6, 1, 0);
     	    } else if (!varPtr && !(flags & TCL_LEAVE_ERR_MSG)) {
     		/*
     		 * Don't need to do anything here.
    
  5. foundin changed to: "trunk"
  6. is_private changed to: "0"
  7. login: "pointsman"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "pointsman"
  13. subsystem changed to: "- New Builtin Commands"
  14. title changed to: "Invalid write in tclExecute.c"
  15. type changed to: "Bug"