grep: fix a case when -o loops forever

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-12-04 02:48:14 +01:00
parent 6a110c969f
commit 6dc0ace109
2 changed files with 10 additions and 3 deletions

View File

@ -229,8 +229,8 @@ static int grep_file(FILE *file)
char *line = NULL; char *line = NULL;
ssize_t line_len; ssize_t line_len;
size_t line_alloc_len; size_t line_alloc_len;
#define rm_so start[0] # define rm_so start[0]
#define rm_eo end[0] # define rm_eo end[0]
#endif #endif
#if ENABLE_FEATURE_GREP_CONTEXT #if ENABLE_FEATURE_GREP_CONTEXT
int print_n_lines_after = 0; int print_n_lines_after = 0;
@ -238,7 +238,7 @@ static int grep_file(FILE *file)
int idx = 0; /* used for iteration through the circular buffer */ int idx = 0; /* used for iteration through the circular buffer */
#else #else
enum { print_n_lines_after = 0 }; enum { print_n_lines_after = 0 };
#endif /* ENABLE_FEATURE_GREP_CONTEXT */ #endif
while ( while (
#if !ENABLE_EXTRA_COMPAT #if !ENABLE_EXTRA_COMPAT
@ -377,6 +377,8 @@ static int grep_file(FILE *file)
print_line(line + gl->matched_range.rm_so, print_line(line + gl->matched_range.rm_so,
end - gl->matched_range.rm_so, end - gl->matched_range.rm_so,
linenum, ':'); linenum, ':');
if (old == '\0')
break;
line[end] = old; line[end] = old;
#if !ENABLE_EXTRA_COMPAT #if !ENABLE_EXTRA_COMPAT
if (regexec(&gl->compiled_regex, line + end, if (regexec(&gl->compiled_regex, line + end,

View File

@ -90,4 +90,9 @@ testing "grep -E -o prints all matches" \
"00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \ "00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \
"" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n" "" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n"
testing "grep -o does not loop forever" \
'grep -o "[^/]*$"' \
"test\n" \
"" "/var/test\n"
exit $FAILCOUNT exit $FAILCOUNT