Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Memory validation failure in Tcl_FindExecutable

Closed / Duplicate219215ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 25 years ago
Descriptionplain2000-10-26 05:03:41
OriginalBugID: 3863 Bug
Version: 8.2.2
SubmitDate: '1999-12-13'
LastModified: '2000-04-03'
Severity: MED
Status: Assigned
Submitter: techsupp
ChangedBy: hobbs
OS: Solaris
OSVersion: 2.6
Machine: Sparc 5
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'


Name: John R. Jackson

Comments:
A word of advice (and maybe it will save you all the time it took me to figure out :-): if you run this
under gdb, it messes with argv[0] and uses a full path which does not trip the bug. In that case,
tweak the test case to pass an explicit value, e.g. "./bug".

ReproducibleScript:
Run the following example **exactly** like this against Tcl built with TCL_MEM_DEBUG:

  ./a.out /

#include <unistd.h>
#include <tcl.h>

int
main (argc, argv)
        int                     argc;
        char                    **argv;
{
        if (argc > 1) {
                (void) chdir(argv[1]);
        }
        Tcl_FindExecutable(argv[0]);
        return 0;
}

If the program is called without the ./ it works.
If the program is called without arguments (so it does not chdir), it works.
If the program is called with an argument other than /opt (e.g. /foozle), it works.

ObservedBehavior:
low guard byte 4 is 0xef         
low guard byte 5 is 0xff         
low guard byte 6 is 0xe7         
low guard byte 7 is 0xe0         
total mallocs                     34
total frees                        1
current packets allocated         33
current bytes allocated         1483
maximum packets allocated         33
maximum bytes allocated         1483
low guard failed at 230d0, ./../unix/tclUnixInit.c 327
20 bytes allocated at (./../generic/tclFileName.c 381)
Memory validation failure
Abort

DesiredBehavior:
My application tries to cd to a "safe" place as soon as possible to protect any core files that are
generated (not that my code would ever do that, of course :-), and this was done before the Tcl_FindExecutable call. After reading the man page again and stepping through the code to try
and find the problem (but I got tired and gave up), I can see that I should probably call Tcl_FindExecutable immediately on entry, which is OK. But there is still pretty clearly something
wrong in the guts of this thing -- it shouldn't trip a memory fault.



Verified with 8.3.0.  You can work-around it with:

        if (argc > 1) {
                (void) chdir(argv[1]);
          Tcl_FindExecutable(argv[1]);
        } else {
          Tcl_FindExecutable(argv[0]);
        }
        return 0;

Or doing the FindExecutable first. 
-- 04/03/2000 hobbs
dkf Ticket [219215ffff]: 1 change [History diff]2001-01-31 17:35:54
dgp Ticket [219215ffff]: 1 change [History diff]2001-02-08 13:55:51
andreas_kupries Ticket [219215ffff]: 1 change [History diff]2001-09-08 06:33:18
msoferplain2001-09-08 20:55:02
Logged In: YES 
user_id=148712

I could not find the memory error - and didn't even try to
reproduce it. However, some remarks before passing to area
maintainer:

The wording in the docs of should be peremptory: DO NOT
change the working directory before calling
Tcl_FindExecutable!

The code in unix/tclUnixFile.c (TclpFindExecutable) makes
clear that whenever argv[0] contains "/", the executable
name "found" will be rooted at the cwd *at the time of the
call to TclpFindExecutable*. So, a wrong name will be
returned ...

Also, the algorithm of TclpFindExecutable may be improved;
it sometimes reports "correct but very ugly" results, as
shown in:

[mig@mini generic]$ pwd
/CVS/tcl_SF_clean/generic
[mig@mini generic]$ ../unix/tclsh
% info nameofexecutable
/CVS/tcl_SF_clean/generic/../unix/tclsh
msoferplain2001-09-10 03:15:05
Logged In: YES 
user_id=148712

That last comment about what TclpFindExecutable does
contains a mistake, of course. It should be:

The code in unix/tclUnixFile.c (TclpFindExecutable)
makesclear that whenever argv[0] contains BUT DOES NOT START
WITH "/", the executable name "found" will be rooted at the
cwd *at the time of the call to TclpFindExecutable*. So, a
wrong name will be returned ...
dasplain2001-09-22 12:54:36
Logged In: YES 
user_id=90580

vince, can you take a look at this? not really my area of 
expertise
vincentdarley Ticket [219215ffff]: 1 change [History diff]2001-09-24 17:27:17
vincentdarleyplain2001-09-24 17:27:18
Logged In: YES 
user_id=32170

dgp's changes of 2001-01-04 may have fixed some issues here 
(When the executable is
installed in or near the root directory of the file 
system,
relative pathnames were being incorrectly 
generated, and in
the worst case, memory access violations were 
crashing the program).


In any case, we should definitely make sure the docs say 
not to change the pwd before calling Tcl_FindExecutable.
dgpplain2001-09-28 03:52:56
Logged In: YES 
user_id=80530

I agree.  This looks like a duplicate report
of Bug 219416, which is fixed.

The docs for Tcl_FindExecutable already say that
it is "typically invoked as the very first thing
in the applications main program; it must be
passed argv[0] as its argument"

Assigning to the Category and maintainers of 
doc/FileExec.3 to determine if more clarification
is needed and for closing.
msoferplain2001-09-29 00:10:13
Logged In: YES 
user_id=148712

Edited the docs to warn not to change the working directory
before calling.
Log in (anonymous is fine) to add a comment.