describe_command: fix incorrect path
PATCH 1/1 describe_command: fix incorrect path
$ PATH=/extra/path:/usr/sbin:/usr/bin:/sbin:/bin \
Quoted 1 line
sh -xc 'command -V ls; command -V ls; command -Vp ls; command -vp ls'
+ command -V ls
ls is /bin/ls
+ command -V ls
ls is a tracked alias for /bin/ls
+ command -Vp ls
ls is a tracked alias for (null)
+ command -vp ls
Segmentation fault (core dumped)
describe_command should respect `path' argument. Looking up in the hash table may gives incorrect index in entry.u.index and finally causes incorrect output or SIGSEGV.
--- src/exec.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
src/exec.c +2 -8
@@ -767,14 +767,8 @@ describe_command(out, command, path, verbose) goto out; } -/* Then check if it is a tracked alias */ -if ((cmdp =cmdlookup (command,0 )) !=NULL ) { - entry.cmdtype = cmdp->cmdtype; - entry.u = cmdp->param; - }else { -/* Finally use brute force */ -find_command (command, &entry,DO_ABS , path); - } +/* Brute force */ +find_command (command, &entry,DO_ABS , path);switch (entry.cmdtype) {case CMDNORMAL : {
Signature · 1 line
2.8.4 (Apple Git-73)
Hi,
Quoted 11 lines · Youfu Zhang
$ PATH=/extra/path:/usr/sbin:/usr/bin:/sbin:/bin \Quoted 1 line
sh -xc 'command -V ls; command -V ls; command -Vp ls; command -vp ls'
+ command -V ls
ls is/bin/ls
+ command -V ls
ls is a tracked alias for/bin/ls
+ command -Vp ls
ls is a tracked alias for (null)
+ command -vp ls
Segmentation fault (core dumped)
Quoted 3 lines
describe_commandshould respect `path' argument. Looking up in the hash table may gives incorrect index in entry.u.index and finally causes incorrect output or SIGSEGV.
True, but only when a path is passed in. If the default path is used, looking up in the hash table is correct, and printing tracked aliases is intentional.
If it's desirable to drop that feature, then it should be dropped completely, code shouldn't be left in that can no longer be used. But it's possible to keep it working: how about this instead?
--- src/exec.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
src/exec.c +9 -4
@@ -743,8 +743,6 @@ describe_command(out, command, path, verbose)struct tblentry *cmdp;const struct alias *ap; - path = path ?:pathval (); -if (verbose) {outstr (command, out); }
@@ -767,8 +765,15 @@ describe_command(out, command, path, verbose) goto out; } -/* Then check if it is a tracked alias */ -if ((cmdp =cmdlookup (command,0 )) !=NULL ) { +/* Then if the standard search path is used, check if it is a tracked alias. */ +if (path ==NULL ) { + path =pathval (); + cmdp =cmdlookup (command,0 ); + }else { + cmdp =NULL ; + } + +if (cmdp !=NULL ) { entry.cmdtype = cmdp->cmdtype; entry.u = cmdp->param; }else {
Your patch looks better than mine :)
Please consider post this patch to busybox as well. Their ash has same code and same issue.
See http://lists.busybox.net/pipermail/busybox/2017-May/085459.html
Quoted 69 lines · Harald van Dijk
Hi,
Quoted 14 lines · Youfu Zhang
$ PATH=/extra/path:/usr/sbin:/usr/bin:/sbin:/bin \Quoted 2 lines
sh -xc 'command -V ls; command -V ls; command -Vp ls; command -vp ls'
+ command -V ls
ls is/bin/ls
+ command -V ls
ls is a tracked alias for/bin/ls
+ command -Vp ls
ls is a tracked alias for (null)
+ command -vp ls
Segmentation fault (core dumped)Quoted 5 lines
describe_commandshould respect `path' argument. Looking up in the hash table
may gives incorrect index in entry.u.index and finally causes incorrect output
or SIGSEGV.True, but only when a path is passed in. If the default path is used, looking up in the hash table is correct, and printing tracked aliases is intentional.
If it's desirable to drop that feature, then it should be dropped completely, code shouldn't be left in that can no longer be used. But it's possible to keep it working: how about this instead?
Signed-off-by Harald van Dijk <[email protected]>--- src/exec.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)src/exec.c +1
-4@@ -743,8 +743,6 @@ describe_command(out, command, path, verbose)struct tblentry *cmdp;const struct alias *ap; - path = path ?:pathval (); -if (verbose) {outstr (command, out); }@@ -767,8 +765,15 @@ describe_command(out, command, path, verbose) goto out; } -/* Then check if it is a tracked alias */ -if ((cmdp =cmdlookup (command,0 )) !=NULL ) { +/* Then if the standard search path is used, check if it is a tracked alias. */
src/exec.c +8
-0+if (path ==NULL ) { + path =pathval (); + cmdp =cmdlookup (command,0 ); + }else { + cmdp =NULL ; + } + +if (cmdp !=NULL ) { entry.cmdtype = cmdp->cmdtype; entry.u = cmdp->param; }else {