Skip to content
Tcl Source Code View Ticket
Login

View Ticket

glob of symlink returns incorrect err

Closed / Fixed434876ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2001-06-20 18:50:24
In all versions of tcl that I have tested the following
seems to be incorrect:

$ tclsh
% ln -s /lwv /tmp/notthere
% glob /tmp/notthere*
/tmp/notthere
% glob /tmp/notthere
no files matched glob pattern "/tmp/notthere"
while evaluating {glob /tmp/notthere}

For some reason, glob reads too 'deeply' into
the fact that the file being pointed at does not yet
appear.

In cases where the file name is a value being passed by
the user, it becomes nearly impossible to work around
this bug.
vincentdarleyplain2001-08-16 22:03:04
Logged In: YES 
user_id=32170

There have been some changes to this code in the 8.4a3 
(after the vfs code was added), do you still see this 
problem?

It does appear as if Tcl is doing something slightly wrong 
wrt stat-vs-lstat...
vincentdarleyplain2001-08-17 00:00:42
Logged In: YES 
user_id=32170

There have been some changes to this code in the 8.4a3 
(after the vfs code was added), do you still see this 
problem?

It does appear as if Tcl is doing something slightly wrong 
wrt stat-vs-lstat...
lvirdenplain2001-08-17 00:09:30
Logged In: YES 
user_id=15949

Yes, I still see this behavior with the latest (last nite)
version of tcl.
vincentdarleyplain2001-08-18 00:16:56
Logged In: YES 
user_id=32170

glob, or more precisely 'TclDoGlob' in tclFileName.c (near 
the very end of the file) uses this piece of code to decide 
whether to add a complete file name (no pattern involved) 
to the result:

    if (Tcl_Access(name, F_OK) == 0) {
then add to glob result
    }

This differs to the pattern matching code 
(TclpMatchInDirectory) which will simply add anything 
returned by the OS 'opendir/readdir' combination, without 
actually checking whether the file exists (unless you pass 
in additional '-types' arguments).

So, the solution would seem to be to change that Tcl_Access 
to something else.  Or at least, if the access fails, to 
try 'lstat' instead.  Could you test what happens if you 
replace if (Tcl_Access .. == 0) by 'if (Tcl_LStat(..) == 
0)'?  (or something like that)....
lvirdenplain2001-08-20 17:13:29
Logged In: YES 
user_id=15949

Unfortunately, I wasn't able to figure out how to add
the code.  Tcl_FSLstat takes an TclObj for a pathname
and I don't have the background to figure out how to create
one of those...

If someone can come up with a patch, I can test it out -
I provided a simple script in the original bug report
for recreating the problem on at least SPARC Solaris...
I wasn't aware that the bug didn't exist everywhere.
vincentdarleyplain2001-08-20 18:08:58
Logged In: YES 
user_id=32170

Could you test this:

case TCL_PLATFORM_UNIX: {
    Tcl_Obj *nameObj;
    struct stat buf;
    
    if (Tcl_DStringLength(headPtr) == 0) {
if ((*name == '\\' && name[1] == '/') || (*name 
== '/')) {
    Tcl_DStringAppend(headPtr, "/", 1);
} else {
    Tcl_DStringAppend(headPtr, ".", 1);
}
    }
    nameObj = Tcl_NewStringObj(Tcl_DStringValue(headPtr), 
       Tcl_DStringLength(headPtr));
    if (Tcl_FSLstat(nameObj, &buf) == 0) {
Tcl_ListObjAppendElement(interp, Tcl_GetObjResult
(interp), 
 nameObj);
    } else {
Tcl_DecrRefCount(nameObj);
    }
    break;
}
lvirdenplain2001-08-20 18:40:09
Logged In: YES 
user_id=15949

I have made the change you requested.  However, 
it doesn't fix the problem:

$ tclsh
% glob /tmp/notthere*
/tmp/notthere
% glob /tmp/notthere
no files matched glob pattern "/tmp/notthere"


Note there is one difference - now the message no longer
says "while evaluating {glob /tmp/notthere}"; I have no
idea why that is now missing!
vincentdarleyplain2001-08-20 18:42:38
Logged In: YES 
user_id=32170

Actually the patch I gave below can segfault under some 
circumstances.  This is better:

    Tcl_Obj *nameObj;
    struct stat buf;
    
    if (Tcl_DStringLength(headPtr) == 0) {
if ((*name == '\\' && name[1] == '/') || 
(*name == '/')) {
    Tcl_DStringAppend(headPtr, "/", 1);
} else {
    Tcl_DStringAppend(headPtr, ".", 1);
}
    }
    nameObj = Tcl_NewStringObj(Tcl_DStringValue
(headPtr), 
       Tcl_DStringLength
(headPtr));
    Tcl_IncrRefCount(nameObj);
    if (Tcl_FSLstat(nameObj, &buf) == 0) {
Tcl_ListObjAppendElement(interp, 
Tcl_GetObjResult(interp), 
 nameObj);
    }
    Tcl_DecrRefCount(nameObj);
    break;
lvirdenplain2001-08-20 19:57:03
Logged In: YES 
user_id=15949

even with your improved patch, the glob continues to
work in the same fashion as it did.
dkfplain2001-08-20 21:14:52
Logged In: YES 
user_id=79902

Vince, with your later change it works exactly as advertised
for me under Solaris; I cannot reproduce Larry's problem.
Should this (corrected) behaviour and its fix be propagated
through to the other platforms too?  (I suppose if they can
have broken links...)  Would that allow any refactoring of
code?
nobodyplain2001-08-20 21:16:24
Logged In: NO 

I've found a sunos machine I have access to, on which this 
fails, and have tried my patch below.  It works for me:

sfi ~> ls -l bbb
lrwxrwxrwx   1 vince    info            3 Aug 20 08:05 bbb -
> aaa
sfi ~> ls -l aaa
ls: aaa: No such file or directory
Exit 1
sfi ~> ./tcl/tcl/unix/tclsh
% info patchlevel
8.4a4
% parray tcl_platform
tcl_platform(byteOrder) = bigEndian
tcl_platform(machine)   = sun4u
tcl_platform(os)        = SunOS
tcl_platform(osVersion) = 5.7
tcl_platform(platform)  = unix
tcl_platform(user)      = vince
% glob bbb*
bbb
% glob bbb
bbb
%


Or, to give an example closer to yours:

% ln -s foo /tmp/notthere
% glob /tmp/notthere
/tmp/notthere

I think this means you've got a problem with the wrong 
version of Tcl being picked up in your testing.  Unless 
there are strenuous objections I'll mark this bug closed 
when I apply the patch (along with a variety of other fs 
changes) -- Vince..
lvirdenplain2001-08-20 21:20:01
Logged In: YES 
user_id=15949

I just tried from a different computer (same install image)
and I see it working from there as well.  So it
should be fine to make this one closed.
nobodyplain2001-08-20 23:33:33
Logged In: NO 

Responding to dkf below, yes this does allow refactoring of 
some code, which I've now done.  The end of TclDoGlob is 
now a bit simpler, and more code is shared.  I'm working on 
a variety of other small filesystem issues and will commit 
the patch when a few other things are done -- Vince.
vincentdarleyplain2001-08-24 01:26:50
Logged In: YES 
user_id=32170

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