Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-06-20
19:35 • Closed ticket [c383ebb5e4]: "glob -path a" core dumps instead of argum... artifact: 8f0fe6021a user: dgp
19:31
[c383ebb5e4] Match DStringInit and DStringFree properly. check-in: 6aef1de4ea user: dgp tags: trunk
19:23
[c383ebb5e4] Match DStringInit and DStringFree properly. check-in: d30be00a35 user: dgp tags: core-8-6-branch
2016-06-16
18:22 • Ticket [c383ebb5e4] "glob -path a" core dumps instead of argument erro... artifact: 3b783d7141 user: dgp
2016-06-14
23:48 • Ticket [c383ebb5e4]: 4 changes artifact: 95bac0ca5f user: dah
2016-06-13
18:27 • Ticket [c383ebb5e4]: 4 changes artifact: 9c26d88e76 user: dah
15:25 • Ticket [c383ebb5e4]: 4 changes artifact: dd72da17d2 user: dah
12:16 • New ticket [c383ebb5e4]. artifact: c5ab08f723 user: oehhar

"glob -path a" core dumps instead of argument error

Closed / Fixedc383ebb5e414458cd386e6016e59f693e3840e81Created 10 years agoUpdated 10 years ago
Descriptionwiki2016-06-13 12:16:57
In 8.6.5, when typing:
glob -path a
my wish core dumps. In 8.5.18, I get:
% glob -path a
wrong # args: should be "glob ?switches? name ?name ...?"

System:

  • Win 10 64 bit
  • Wish 8.6.5 compiled by MSVC6, PSDK2003SP1, makefile.vc
Referenced by2
dahplain2016-06-13 15:25:57
I can confirm the crash.

I can't reproduce it compiling with mingw gcc or gcc on linux.

In func Tcl_GlobObjCmd in tclFileName.c it is hitting the 
"} else if (dir == PATH_GENERAL) {" branch and objc = 0 for Linux+Windows.

Tcl_DString str isn't being initialized with MSVC. I guess gcc is doing some optimization trick where it does do the initialization (gdb shows the struct initialized.)? I don't understand assembly well enough to be able to see it myself.

So it is doing a Tcl_DStringFree on a DString that was never initialized causing the crash.
dahplain2016-06-13 18:27:30
I can't create a patch atm but here's a naive solution:

In file tclFileName.c @ line 1569

  skipTypes:
    /*
     * Now we perform the actual glob below. This may involve joining together
     * the pattern arguments, dealing with particular file types etc. We use a
     * 'goto' to ensure we free any memory allocated along the way.
     */

    objc -= i;
    /* Fix crash due to not checking if glob pattern exists */
    if (objc == 0) {
        Tcl_SetObjResult(interp, Tcl_NewStringObj("missing glob pattern", -1));
	result = TCL_ERROR;
	Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "BAD", NULL);
	join = 0;
	goto endOfGlob;
    }
    objv += i;
    result = TCL_OK;
dahplain2016-06-14 23:48:38
Ok, in case anyone is interested, it doesn't crash when compiled with gcc due to an optimization where it reuses local variable storage. There is a DString "pref" initialization done before the code block triggering the crash with MSVC, and that same storage is reused later for DString "str" (with gcc).

Below is essentially what is happening and shouldn't crash with gcc either, but does with MSVC:

#include <tcl.h>
#include <stdio.h>
int main (int argc, char *argv[]) {
        if (1) {
                Tcl_DString one;
                Tcl_DStringInit(&one);
                Tcl_DStringFree(&one);
        }
        if (2) {
                /* Should crash, but gcc saves the day! two -> one */
                Tcl_DString two;
                Tcl_DStringFree(&two);
        }
}


This isn't a Windows only bug, I suspect it will manifest itself with other compilers.
dgpplain2016-06-16 18:22:01
Can anyone explain how we reach "skipTypes:"
with (objc == i) ?
dgpplain2016-06-20 19:35:05
The 8.5 -> 8.6 difference is rooted in TIP 323.

[glob] has been revised to accept zero pattern arguments.
Proper behavior for [glob -path a] is now:

  no files matched glob patterns ""

and [glob -nocomplain -path a] would succeed
and return an empty list.

A stanza of C code was written in a way that 
implicitly assumed 1 or more pattern arguments.
Likely not on purpose. Just a misplaced initialization.
Then as described, gcc quirks hid the consequences
from a lot of us.

Fixed for 8.6.6 and beyond.
Log in (anonymous is fine) to add a comment.