Hi
Your diff didn't come through, but I don't think your fix is correct.
The problem is not that the ambiguous flag is not being set to zero - it is
always set to zero unless a multiple match happens.
The error is that tmux returns if there are two ambiguous matches, even if
there are subsequent full matches. They should be checked separately.
Please try this diff instead:
Index: cmd.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/cmd.c,v
retrieving revision 1.27
diff -u -p -r1.27 cmd.c
--- cmd.c 26 Oct 2009 21:42:04 -0000 1.27
+++ cmd.c 2 Nov 2009 14:43:52 -0000
@@ -488,19 +488,25 @@ cmd_lookup_session(const char *name, int
*ambiguous = 0;
/*
- * Look for matches. Session names must be unique so an exact match
- * can't be ambigious and can just be returned.
+ * Look for matches. First look for exact matches - session names must
+ * be unique so an exact match can't be ambigious and can just be
+ * returned.
*/
- sfound = NULL;
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
continue;
-
- /* Check for an exact match and return it if found. */
if (strcmp(name, s->name) == 0)
return (s);
-
- /* Then check for pattern matches. */
+ }
+
+ /*
+ * Otherwise look for partial matches, returning early if it is found to
+ * be ambiguous.
+ */
+ sfound = NULL;
+ for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
+ if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
+ continue;
if (strncmp(name, s->name, strlen(name)) == 0 ||
fnmatch(name, s->name, 0) == 0) {
if (sfound != NULL) {
@@ -510,7 +516,6 @@ cmd_lookup_session(const char *name, int
sfound = s;
}
}
-
return (sfound);
}
On Mon, Nov 02, 2009 at 02:16:04PM +0100, Natacha Port? wrote:
> Hi,
>
> I think I have found a bug in the way tmux looks up sessions. Here is a
> sample of the symptoms:
>
> % tmux ls
> foo1: 1 windows (created Mon Nov 2 13:18:01 2009) [80x24]
> foo2: 1 windows (created Mon Nov 2 13:18:03 2009) [80x24]
> foo: 1 windows (created Mon Nov 2 13:19:00 2009) [80x24]
> bar: 1 windows (created Mon Nov 2 13:19:58 2009) [80x56] (attached)
> % tmux attach-session -t foo
> more than one session: foo
> %
>
> To reproduce the issue, you need a tmux server with at least three
> sessions, one of them having a name being a prefix of at least two
> sessions before it. In my example, "foo" is a prefix of "foo1" and
> "foo2", both of them being before "foo".
>
> Then trying to do anything with session foo will report an error of
> "foo" being ambiguous, while in reality it is not, there is really a
> session "foo".
>
>
> This can be easily explained by the code of cmd_lookup_session() from
> src/usr.bin/tmux/cmd.c. I am not an OpenBSD user, but from OpenBSD
> cvsweb I checked the file with this version:
> /* $OpenBSD: cmd.c,v 1.27 2009/10/26 21:42:04 deraadt Exp $ */
>
> The function cmd_lookup_session() starts at line 483. In this function,
> there is a for loop over the session names, when an exact match is found
> the loop is prematurely exited by a return statement (line 501), and
> when two prefix or pattern matches are found, *ambigous is set to 1.
>
> The problem is that when two prefix/pattern matches are found BEFORE an
> exact match, as in my testcase ("foo1" and "foo2" are prefix matches of
> "foo"), *ambiguous is not set back to 0 when the exact match is
> encountered.
>
> My fix proposition is therefore to add *ambiguous = 0; before
> return (s); in case of exact match. This is the content of the attached
> patch.
>
>
>
> Thanks for reading this mail,
> Natacha Porti
>
> [demime 1.01d removed an attachment of type text/x-diff]