| Ticket UUID: | 7a87a9bc5b213d2d57848e263f12636bb8d945a2 | ||
| Title: | Invalid write in tclExecute.c | ||
| Type: | Bug | Created on: | 2015-08-13 13:22:13 |
| Submitter: | pointsman | Assigned to: | dkf |
| Subsystem: | 47. Bytecode Compiler | Severity: | Minor |
| Priority: | 8 | Last modified: | 2015-08-18 07:03:58 |
| Status: | Closed | Closed by: | dkf |
| Resolution: | Fixed | Closed on: | 2015-08-18 07:03:58 |
| Version: | trunk | ||
| Description: | ||||
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.
| ||||
| User Comments: | ||||
dkf added on 2015-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). dkf added on 2015-08-17 18:05:05:
My proposed fix is [4a4e3eb33d]. pointsman added on 2015-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. dkf added on 2015-08-18 07:03:58:
Thanks for the check. Fixed. | ||||
