Bug 4832 - test builtin chokes on bare "!"
Summary: test builtin chokes on bare "!"
Status: RESOLVED FIXED
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: 1.19.x
Hardware: PC Linux
: P5 minor
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-02 16:39 UTC by Michael Tokarev
Modified: 2012-03-08 02:51 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Tokarev 2012-03-02 16:39:29 UTC
To reproduce:

$ busybox sh -xc 'test !'
+ test !
Segmentation fault

It happens on x64 this way, on i386 it succeeds but only because there's more room in argv vector, see below.

In coreutils/test.c, we see:

 test_main()
 -> oexpr(UNOT)
  -> aexpr(UNOT)
   -> nexpr(UNOT)

static number_t nexpr(enum token n)
{
at entry, *args points to the last NULL argv element.
        number_t res;

        nest_msg(">nexpr(%s)\n", TOKSTR[n]);
        if (n == UNOT) {
                n = check_operator(*++args);
                                   ^^^^^^^
we increment args, it points past the array.  check_operator retursn EOI.
                if (n == EOI) {
                        /* special case: [ ! ], [ a -a ! ] are valid */
                        /* IOW, "! ARG" may miss ARG */
                        unnest_msg("<nexpr:1 (!EOI)\n");
                        return 1;
                        ^^^^^^^^^ we are here

And back in aexpr(), we have:

static number_t aexpr(enum token n)
{
        number_t res;

        nest_msg(">aexpr(%s)\n", TOKSTR[n]);
        res = nexpr(n);
              ^^^^^^^ we called this nexpr, which returned
        dbg_msg("aexpr: nexpr:%lld, next args:%s\n", res, args[1]);
        if (check_operator(*++args) == BAND) {
                          ^^^^^^^^^
And there, we're referencing next-after-last argv element.

Maybe nexpr should not increment args in case of EOI ?

Thanks,

/mjt
Comment 1 Denys Vlasenko 2012-03-08 02:51:02 UTC
Thanks for reporting.

Fixed in git:

http://git.busybox.net/busybox/commit/?id=07fcaab595e9029ebe37f5240a10038c493af545