mirror of
https://github.com/sheumann/hush.git
synced 2025-01-10 16:29:44 +00:00
grep: fix -o match with empty string (suggested by Colin Watson <cjwatson@ubuntu.com>)
function old new delta grep_file 1216 1251 +35 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
b276e41835
commit
3d8b96d58d
@ -461,15 +461,19 @@ static int grep_file(FILE *file)
|
|||||||
if (found)
|
if (found)
|
||||||
print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
|
print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
|
||||||
} else while (1) {
|
} else while (1) {
|
||||||
|
unsigned start = gl->matched_range.rm_so;
|
||||||
unsigned end = gl->matched_range.rm_eo;
|
unsigned end = gl->matched_range.rm_eo;
|
||||||
|
unsigned len = end - start;
|
||||||
char old = line[end];
|
char old = line[end];
|
||||||
line[end] = '\0';
|
line[end] = '\0';
|
||||||
print_line(line + gl->matched_range.rm_so,
|
/* Empty match is not printed: try "echo test | grep -o ''" */
|
||||||
end - gl->matched_range.rm_so,
|
if (len != 0)
|
||||||
linenum, ':');
|
print_line(line + start, len, linenum, ':');
|
||||||
if (old == '\0')
|
if (old == '\0')
|
||||||
break;
|
break;
|
||||||
line[end] = old;
|
line[end] = old;
|
||||||
|
if (len == 0)
|
||||||
|
end++;
|
||||||
#if !ENABLE_EXTRA_COMPAT
|
#if !ENABLE_EXTRA_COMPAT
|
||||||
if (regexec(&gl->compiled_regex, line + end,
|
if (regexec(&gl->compiled_regex, line + end,
|
||||||
1, &gl->matched_range, REG_NOTBOL) != 0)
|
1, &gl->matched_range, REG_NOTBOL) != 0)
|
||||||
|
@ -98,5 +98,9 @@ testing "grep -o does not loop forever" \
|
|||||||
'grep -o "[^/]*$"' \
|
'grep -o "[^/]*$"' \
|
||||||
"test\n" \
|
"test\n" \
|
||||||
"" "/var/test\n"
|
"" "/var/test\n"
|
||||||
|
testing "grep -o does not loop forever on zero-length match" \
|
||||||
|
'grep -o "" | head -n1' \
|
||||||
|
"" \
|
||||||
|
"" "test\n"
|
||||||
|
|
||||||
exit $FAILCOUNT
|
exit $FAILCOUNT
|
||||||
|
Loading…
x
Reference in New Issue
Block a user