[Bug libc/27143] New: Calling `system("-some-tool")` fails (although it is a valid `sh` command)

ciprian.craciun at gmail dot com sourceware-bugzilla@sourceware.org
Sun Jan 3 17:30:07 GMT 2021


https://sourceware.org/bugzilla/show_bug.cgi?id=27143

            Bug ID: 27143
           Summary: Calling `system("-some-tool")` fails (although it is a
                    valid `sh` command)
           Product: glibc
           Version: 2.32
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: ciprian.craciun at gmail dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

As the subject states, the `system` function fails when calling it with a
command that starts with a hyphen, which is an accepted executable name, and an
accepted `sh` command.

Let's assume one has an executable starting with a hyphen, as in:
~~~~
ln -s -T /usr/bin/echo ~/.bin/-echo
~~~~

Assuming that `~/.bin` is in `$PATH` the following work in "plain" `sh` /
`bash`:
~~~~
type -P -- -echo
which -- -echo
-echo a b
~~~~

However the following simple `main.c` doesn't work:
~~~~
> cat ./main.c
main() { system("-echo a b"); }

> gcc -o ./main ./main.c

> ./main
sh: - : invalid option
~~~~



The issue is simple:

* `system("-echo a b")` end's up calling `sh -c '-echo a b'`;

* unfortunately the `-c` argument states that it will use as a command the
**first non-option argument** which in this case there is none as `-echo a b`
is mistaken as an option argument;



The solution would be updating `system` to issue `sh -c -- {command}`, as in
(taking the manual as example):
~~~~
execl("/bin/sh", "sh", "-c", "--", command, (char *) NULL);
                             ^^^^ this is added
~~~~



However this proposed solution might break systems where `sh` doesn't accept
the `--` separator.  (Is this the case?)

An alternative would be, if the command starts with a hyphen then a space could
be added at the beginning, as in: `sh -c ' -echo a b'`



My environment:

* `sh --version` -- `GNU bash, version 5.0.18(1)-release
(x86_64-suse-linux-gnu)`;

* `getconf --version` -- `getconf (GNU libc) 2.32`

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the Glibc-bugs mailing list