Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2015-08-18
07:03 • Closed ticket [7a87a9bc5b]: Invalid write in tclExecute.c plus 7 other chang... artifact: bb893f8edf user: dkf
07:01
[7a87a9bc5b] Eliminate invalid write, found by Rolf Ade using Valgrind. check-in: 82cf3748de user: dkf tags: trunk
2015-08-17
20:57 • Ticket [7a87a9bc5b] Invalid write in tclExecute.c status still Open with 3 o... artifact: 4b4814e5c9 user: pointsman
18:05 • Ticket [7a87a9bc5b]: 4 changes artifact: 5d8221351e user: dkf
17:08 • Ticket [7a87a9bc5b]: 4 changes artifact: 2c3b0c9c21 user: dkf
12:36 • Ticket [7a87a9bc5b]: 3 changes artifact: 122391f8d3 user: dkf
2015-08-13
16:54 • Ticket [7a87a9bc5b]: 4 changes artifact: 06e770b0fa user: dgp
13:22 • New ticket [7a87a9bc5b]. artifact: ee4213af8f user: pointsman

Invalid write in tclExecute.c

Closed / Fixed7a87a9bc5b213d2d57848e263f12636bb8d945a2Created 11 years agoUpdated 11 years ago
Descriptionplain2015-08-13 13:22:13
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.
Referenced by1
  • 82cf3748de[7a87a9bc5b] Eliminate invalid write, found by Rolf Ade using Valgrind. check-in2015-08-18 07:01:41
dgp Ticket [7a87a9bc5b]: 4 changes [History diff]2015-08-13 16:54:47
dkf Ticket [7a87a9bc5b]: 3 changes [History diff]2015-08-17 12:36:55
dkfhtml2015-08-17 17:08:44

I suspect that simply deleting the line

		varPtr->value.objPtr = NULL;
would be sufficient to fix this bug. This is because I know that being undefined is the same as having that field be NULL; either the value is not NULL, in which case the first if clause above activates (and makes the field NULL) or it is NULL in which case the write ought to have no effect.

I'd appreciate if you could check if my theory is correct. I don't have valgrind on this system (OSX).

dkfwiki2015-08-17 18:05:05

My proposed fix is [4a4e3eb33d].

pointsmanplain2015-08-17 20:57:10
Switched to that branch, rebuild with the necessary valgrind magic and run the whole test suite (with -singleproc 1) - only without clock.test - under valgrind. No invalid memory read or write reported with this.
dkfhtml2015-08-18 07:03:58

Thanks for the check. Fixed.

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