diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2014-01-07 14:57:42 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-01-07 14:59:30 +0100 |
commit | cd55f2d9332489432ba2b3093903949c6c2e3e33 (patch) | |
tree | 448769abf7fbdd41af7f74d75ecad91c5714b89e | |
parent | 432fbd7a1a71ab5a91a8cfab57fad74fda9389bb (diff) | |
download | busybox-w32-cd55f2d9332489432ba2b3093903949c6c2e3e33.tar.gz busybox-w32-cd55f2d9332489432ba2b3093903949c6c2e3e33.tar.bz2 busybox-w32-cd55f2d9332489432ba2b3093903949c6c2e3e33.zip |
grep: fix two bugs with -w
Unfortunately, with !EXTRA_COMPAT, "grep -w ^str" still erroneously matches "strstr".
function old new delta
grep_file 1499 1510 +11
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | findutils/grep.c | 28 | ||||
-rwxr-xr-x | testsuite/grep.tests | 12 |
2 files changed, 37 insertions, 3 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index b8ad1bf8f..a7bc4ca9e 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -373,6 +373,9 @@ static int grep_file(FILE *file) | |||
373 | opt_f_not_found: ; | 373 | opt_f_not_found: ; |
374 | } | 374 | } |
375 | } else { | 375 | } else { |
376 | #if ENABLE_EXTRA_COMPAT | ||
377 | unsigned start_pos; | ||
378 | #endif | ||
376 | char *match_at; | 379 | char *match_at; |
377 | 380 | ||
378 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { | 381 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { |
@@ -389,15 +392,18 @@ static int grep_file(FILE *file) | |||
389 | #if !ENABLE_EXTRA_COMPAT | 392 | #if !ENABLE_EXTRA_COMPAT |
390 | gl->matched_range.rm_so = 0; | 393 | gl->matched_range.rm_so = 0; |
391 | gl->matched_range.rm_eo = 0; | 394 | gl->matched_range.rm_eo = 0; |
395 | #else | ||
396 | start_pos = 0; | ||
392 | #endif | 397 | #endif |
393 | match_at = line; | 398 | match_at = line; |
394 | opt_w_again: | 399 | opt_w_again: |
400 | //bb_error_msg("'%s' start_pos:%d line_len:%d", match_at, start_pos, line_len); | ||
395 | if ( | 401 | if ( |
396 | #if !ENABLE_EXTRA_COMPAT | 402 | #if !ENABLE_EXTRA_COMPAT |
397 | regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, 0) == 0 | 403 | regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, 0) == 0 |
398 | #else | 404 | #else |
399 | re_search(&gl->compiled_regex, match_at, line_len, | 405 | re_search(&gl->compiled_regex, match_at, line_len, |
400 | /*start:*/ 0, /*range:*/ line_len, | 406 | start_pos, /*range:*/ line_len, |
401 | &gl->matched_range) >= 0 | 407 | &gl->matched_range) >= 0 |
402 | #endif | 408 | #endif |
403 | ) { | 409 | ) { |
@@ -416,8 +422,24 @@ static int grep_file(FILE *file) | |||
416 | if (!c || (!isalnum(c) && c != '_')) { | 422 | if (!c || (!isalnum(c) && c != '_')) { |
417 | found = 1; | 423 | found = 1; |
418 | } else { | 424 | } else { |
419 | match_at += gl->matched_range.rm_eo; | 425 | /* |
420 | goto opt_w_again; | 426 | * Why check gl->matched_range.rm_eo? |
427 | * Zero-length match makes -w skip the line: | ||
428 | * "echo foo | grep ^" prints "foo", | ||
429 | * "echo foo | grep -w ^" prints nothing. | ||
430 | * Without such check, we can loop forever. | ||
431 | */ | ||
432 | #if !ENABLE_EXTRA_COMPAT | ||
433 | if (gl->matched_range.rm_eo != 0) { | ||
434 | match_at += gl->matched_range.rm_eo; | ||
435 | goto opt_w_again; | ||
436 | } | ||
437 | #else | ||
438 | if (gl->matched_range.rm_eo > start_pos) { | ||
439 | start_pos = gl->matched_range.rm_eo; | ||
440 | goto opt_w_again; | ||
441 | } | ||
442 | #endif | ||
421 | } | 443 | } |
422 | } | 444 | } |
423 | } | 445 | } |
diff --git a/testsuite/grep.tests b/testsuite/grep.tests index 64d99a905..412efffbb 100755 --- a/testsuite/grep.tests +++ b/testsuite/grep.tests | |||
@@ -147,6 +147,18 @@ testing "grep -w doesn't stop on 1st mismatch" \ | |||
147 | "foop foo\n" \ | 147 | "foop foo\n" \ |
148 | "" | 148 | "" |
149 | 149 | ||
150 | testing "grep -w ^str doesn't match str not at the beginning" \ | ||
151 | "grep -w ^str input" \ | ||
152 | "" \ | ||
153 | "strstr\n" \ | ||
154 | "" | ||
155 | |||
156 | testing "grep -w ^ doesn't hang" \ | ||
157 | "grep -w ^ input" \ | ||
158 | "" \ | ||
159 | "anything\n" \ | ||
160 | "" | ||
161 | |||
150 | # testing "test name" "commands" "expected result" "file input" "stdin" | 162 | # testing "test name" "commands" "expected result" "file input" "stdin" |
151 | # file input will be file called "input" | 163 | # file input will be file called "input" |
152 | # test can create a file "actual" instead of writing to stdout | 164 | # test can create a file "actual" instead of writing to stdout |