Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2009-03-28
05:02 • Ticket [2710920fff] file tail gives wrong result status still Closed with ... artifact: a469461e2f user: ferrieux
04:45 • Ticket [2710920fff]: 4 changes artifact: 225c558909 user: dgp
04:30 • Ticket [2710920fff]: 4 changes artifact: 2ecbb125a3 user: ferrieux
02:20 • Closed ticket [2710920fff]. artifact: 8dd326f08e user: dgp
02:04 • Add attachment 2710920.patch to ticket [2710920fff] artifact: 0922701d9d user: dgp
02:04 • Ticket [2710920fff] file tail gives wrong result status still Open with 4 ... artifact: 697ee271e7 user: dgp
01:47 • Ticket [2710920fff]: 4 changes artifact: e8ae894c67 user: dgp
01:07 • Ticket [2710920fff]: 4 changes artifact: 2cf6348f99 user: dgp
2009-03-27
04:20 • Ticket [2710920fff]: 4 changes artifact: 2a52606235 user: dgp
03:12 • Ticket [2710920fff]: 4 changes artifact: b4f05b2653 user: dgp
02:18 • Ticket [2710920fff]: 4 changes artifact: ef0fcb831e user: dgp
01:19 • Ticket [2710920fff]: 4 changes artifact: ee36442497 user: dgp
00:58 • Ticket [2710920fff]: 4 changes artifact: 32cf9a2bbf user: dgp
00:53 • Ticket [2710920fff]: 4 changes artifact: 44f935a1eb user: jepler
00:47 • Ticket [2710920fff]: 4 changes artifact: 8ad567ab63 user: dgp
2009-03-26
07:08 • Ticket [2710920fff]: 4 changes artifact: 3db5699e02 user: jepler
07:06 • Add attachment fff.tcl to ticket [2710920fff] artifact: 0b9c9862ea user: jepler
07:06 • Ticket [2710920fff] file tail gives wrong result status still Open with 4 ... artifact: 6b5685b4fc user: jepler
07:05 • Ticket [2710920fff]: 4 changes artifact: 3bd17eb637 user: jepler
2009-03-25
11:59 • Ticket [2710920fff]: 2 changes artifact: 0f29d4b80a user: dgp
10:07 • Ticket [2710920fff]: 4 changes artifact: 4099e54847 user: jepler
10:05 • Ticket [2710920fff]: 4 changes artifact: 5a47b6fda3 user: jepler
03:05 • New ticket [2710920fff]. artifact: 45a9f4c8d9 user: jepler

[file tail] gives wrong result

Closed / Fixed2710920fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2009-03-25 03:05:51
This problem occurs on a freshly built 8.5.6 (unmodified) on Ubuntu Hardy and is also seen on 8.5.3-2 on ubuntu jaunty alpha (ubuntu modified).  The bug is not seen in 8.4.16-4ubuntu1 (ubuntu modified) on ubuntu hardy.

In a specific scenario involving [glob] with a trailing slash, the resulting files are treated improperly by [file tail].  An attached script, which can be run by
$ make shell SCRIPT=filetailbug.tcl
demonstrates the problem on my system.

Expected output:
 /bin bin
Actual output:
 /bin {}
jeplerplain2009-03-25 10:05:52
File Added - 319464: filetailbug.tcl
jeplerplain2009-03-25 10:07:16
I am not familiar with the tcl path object, but I attempted to generate some useful debugging information.  I found that the 'normPathPtr' is an empty string, and that TclPathpart returns normPathPtr, leading to the result I am seeing.  Perhaps the problem is occurring earlier at the point where this path object is constructed?

Breakpoint 3, TclPathPart (interp=0x606400, pathPtr=0x62aa00, 
    portion=TCL_PATH_TAIL) at /tmp/tcl8.5.6/unix/../generic/tclPathObj.c:596
596const char *rest = TclGetString(fsPathPtr->normPathPtr);
(gdb) print fsPathPtr->translatedPathPtr->bytes
$32 = 0x636be0 "/bin"
(gdb) print fsPathPtr->normPathPtr->bytes
$33 = 0x7f2ed6902b48 ""
(gdb) next
598if (strchr(rest, '/') != NULL) {
(gdb) 
601if (tclPlatform == TCL_PLATFORM_WINDOWS
(gdb) 
605Tcl_IncrRefCount(fsPathPtr->normPathPtr);
(gdb) 
606return fsPathPtr->normPathPtr;
(gdb) 
747}
(gdb)
dgp Ticket [2710920fff]: 2 changes [History diff]2009-03-25 11:59:04
jeplerplain2009-03-26 07:05:49
File Deleted - 319464:
jeplerplain2009-03-26 07:06:19
File Added - 319626: fff.tcl
jeplerplain2009-03-26 07:08:33
I failed to correctly minimize the test case in my original submission.  Please see the attached 'fff.tcl' instead of the original attachment.

$ cat fff.tcl 
proc f {} {
    set d [glob -nocomplain /*/]
    set d [lindex $d 0]
    #set d [format %s $d]   # uncommenting this line makes both 8.4 and 8.5 work
    puts stderr [list $d [file tail $d]]
}

f

$ make shell SCRIPT=fff.tcl
LD_LIBRARY_PATH="`pwd`:${LD_LIBRARY_PATH}"; export LD_LIBRARY_PATH; \
TCL_LIBRARY="/tmp/tcl8.5.6/library"; export TCL_LIBRARY; \
./tclsh fff.tcl
/mnt/ {}
$
dgpplain2009-03-27 00:47:41
I can now reproduce the problem with Tcl 8.5.6:

$ make shell SCRIPT=fff.tcl
...
LD_LIBRARY_PATH="`pwd`:${LD_LIBRARY_PATH}"; export LD_LIBRARY_PATH; \
        TCL_LIBRARY="/home/dgp/cvs/tcl8.5.6/library"; export TCL_LIBRARY; \
        ./tclsh fff.tcl
/opt/ {}

Note that a very similar attempt yields different results:

$ make shell SCRIPT=~/fff.tcl
...
LD_LIBRARY_PATH="`pwd`:${LD_LIBRARY_PATH}"; export LD_LIBRARY_PATH; \
        TCL_LIBRARY="/home/dgp/cvs/tcl8.5.6/library"; export TCL_LIBRARY; \
        ./tclsh ~/fff.tcl
/opt/ opt

Same script in a different place and accessed via
an absolute path.  So inspired by that difference, try:

$ make shell SCRIPT=`pwd`/fff.tcl
...
LD_LIBRARY_PATH="`pwd`:${LD_LIBRARY_PATH}"; export LD_LIBRARY_PATH; \
        TCL_LIBRARY="/home/dgp/cvs/tcl8.5.6/library"; export TCL_LIBRARY; \
        ./tclsh /home/dgp/cvs/tcl8.5.6/unix/fff.tcl
/opt/ opt
jeplerplain2009-03-27 00:53:36
I'm glad you found how to reproduce it!

I can confirm what dgp says: whether the script is given as an absolute path or not affects the output.
dgpplain2009-03-27 00:58:50
Further testing indicates that
Tcl 8.5.0 works, while 8.5.1 has
the bug.
dgpplain2009-03-27 01:19:48
Sorry, I was misled by --enable-shared builds.
dgpplain2009-03-27 02:18:07
Bug introduced between 8.5.2 and 8.5.3.
dgpplain2009-03-27 03:12:28
The fix for Bug 1972879 is to blame.
dgpplain2009-03-27 04:20:26
Appears that the fix of 1972879 did
not create this bug, but just exposed it,
by permitting some otherwise untested
codepaths to execute.

Problem is that [file tail] on the
PATHFLAGS !=0 intrep for path values
is just broken when paths have trailing
separators.
dgpplain2009-03-28 01:07:00
The mystery as to why the behavior
is different whether or not the demo
script is [source]d by relative or absolute
name is complex, but explainable.

When the file name is absolute, there's
never any need to ask what the [pwd]
value is in order to open and read that
file.  In this simple test script, it happens
that nothing else needed to know that value
either, so the value of [pwd] has never
been determined.

In the innermost part of the [glob] operation,
we need to query the actual filesystem to
determine what files exist.  The routine
TclpMatchInDirectory() in tclUnixFile.c does
this, and it must construct the "native"
name of the file to do so.  It calls the
routine Tcl_FSGetNativePath() to get that
native name.  This works its way back to
the routine TclNativeCreateNativeRep()
also in tclUnixFile.c.  TNCNR has two
branches depending on whether or not
the [pwd] value is known to be in the
native filesystem.  In the case where we've
never had reason to determine [pwd]
up to that point, [pwd] is not known to
be in the native filesystem, so a less
efficient, safer path to generating the
native name is taken by normalizing the
path first.  Normalizing the path converts
it out of the intrep which triggers the bug.

On the other hand, when the pathname
of the demo script is relative, we had to
compute [pwd] just to find and source the
script, so we have the value and we know
it's in the native filesystem, so within
TNCNR we take a more efficient path to
computing the native name which also
preserves the intrep which is needed to
demo the [file tail] bug.

Perhaps more than you wanted to know,
but I figured it should be written down
somewhere.
dgpplain2009-03-28 01:47:57
[file dirname] suffers an analogous bug.
dgpplain2009-03-28 02:04:53
File Added - 319929: 2710920.patch
dgpplain2009-03-28 02:20:43
allow_comments - 1


Fixed for 8.5.7 and 8.6b2
ferrieuxplain2009-03-28 04:30:10
Perhaps some chosen pieces of the very detailed and interesting description below could find a place in Filesystem.3 ?
dgpplain2009-03-28 04:45:39
No; I like to limit the docs to
things I like and want to keep.

Internal details of dubious wisdom
deserve writeups sometimes, but
not in the docs.
ferrieuxplain2009-03-28 05:02:23
It hardly gets colder than that, eh ?
Log in (anonymous is fine) to add a comment.

Attachments: