test: fix mishandling of "test '(' = '('" and similar

function                                             old     new   delta
test_main                                            246     350    +104

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2014-07-01 14:16:28 +02:00
parent d32fc647d7
commit 98654b995b
2 changed files with 55 additions and 34 deletions

View File

@ -826,7 +826,6 @@ int test_main(int argc, char **argv)
{ {
int res; int res;
const char *arg0; const char *arg0;
// bool negate = 0;
arg0 = bb_basename(argv[0]); arg0 = bb_basename(argv[0]);
if (arg0[0] == '[') { if (arg0[0] == '[') {
@ -844,6 +843,7 @@ int test_main(int argc, char **argv)
} }
argv[argc] = NULL; argv[argc] = NULL;
} }
/* argc is unused after this point */
/* We must do DEINIT_S() prior to returning */ /* We must do DEINIT_S() prior to returning */
INIT_S(); INIT_S();
@ -862,24 +862,25 @@ int test_main(int argc, char **argv)
*/ */
/*ngroups = 0; - done by INIT_S() */ /*ngroups = 0; - done by INIT_S() */
//argc--;
argv++; argv++;
args = argv;
/* Implement special cases from POSIX.2, section 4.62.4 */ /* Implement special cases from POSIX.2, section 4.62.4.
if (!argv[0]) { /* "test" */ * Testcase: "test '(' = '('"
* The general parser would misinterpret '(' as group start.
*/
if (1) {
int negate = 0;
again:
if (!argv[0]) {
/* "test" */
res = 1; res = 1;
goto ret; goto ret_special;
} }
#if 0 if (!argv[1]) {
// Now it's fixed in the parser and should not be needed /* "test [!] arg" */
if (LONE_CHAR(argv[0], '!') && argv[1]) { res = (argv[0][0] == '\0');
negate = 1; goto ret_special;
//argc--;
argv++;
}
if (!argv[1]) { /* "test [!] arg" */
res = (*argv[0] == '\0');
goto ret;
} }
if (argv[2] && !argv[3]) { if (argv[2] && !argv[3]) {
check_operator(argv[1]); check_operator(argv[1]);
@ -887,18 +888,19 @@ int test_main(int argc, char **argv)
/* "test [!] arg1 <binary_op> arg2" */ /* "test [!] arg1 <binary_op> arg2" */
args = argv; args = argv;
res = (binop() == 0); res = (binop() == 0);
ret_special:
/* If there was leading "!" op... */
res ^= negate;
goto ret; goto ret;
} }
} }
if (LONE_CHAR(argv[0], '!')) {
/* Some complex expression. Undo '!' removal */ argv++;
if (negate) { negate ^= 1;
negate = 0; goto again;
//argc++;
argv--;
} }
#endif }
args = argv;
res = !oexpr(check_operator(*args)); res = !oexpr(check_operator(*args));
if (*args != NULL && *++args != NULL) { if (*args != NULL && *++args != NULL) {
@ -911,6 +913,5 @@ int test_main(int argc, char **argv)
} }
ret: ret:
DEINIT_S(); DEINIT_S();
// return negate ? !res : res;
return res; return res;
} }

View File

@ -76,4 +76,24 @@ testing "test ! a = b -a ! c = d: should be true (0)" \
"0\n" \ "0\n" \
"" "" "" ""
testing "test '!' = '!': should be true (0)" \
"busybox test '!' = '!'; echo \$?" \
"0\n" \
"" ""
testing "test '(' = '(': should be true (0)" \
"busybox test '(' = '('; echo \$?" \
"0\n" \
"" ""
testing "test '!' '!' = '!': should be false (1)" \
"busybox test '!' '!' = '!'; echo \$?" \
"1\n" \
"" ""
testing "test '!' '(' = '(': should be false (1)" \
"busybox test '!' '(' = '('; echo \$?" \
"1\n" \
"" ""
exit $FAILCOUNT exit $FAILCOUNT