[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug #17457] "grep -r foo . > somefile" goes into an infinite loop

From: anonymous
Subject: [bug #17457] "grep -r foo . > somefile" goes into an infinite loop
Date: Fri, 18 Aug 2006 00:08:07 +0000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20060626 Firefox/ (Debian-1.5.dfsg+


                 Summary: "grep -r foo . > somefile" goes into an infinite
                 Project: grep
            Submitted by: None
            Submitted on: vendredi 18.08.2006 à 00:08 UTC
                Category: None
                Severity: 3 - Normal
              Item Group: None
                  Status: None
                 Privacy: Public
             Assigned to: None
             Open/Closed: Open



When using grep -r and redirecting output to a file in the traversed
directory, grep sometimes enters in an infinite loop, and size of output file
grows endelessly.

What happens is : directory is traversed, and every file is searched for a
pattern. The line containing the pattern is written to somefile. When it
comes to somefile, its content is buffered. Pattern is found in it (because
lines containing pattern have already been written). More output is then
written to somefile. Then somefile is checked for more content (function
fillbuff in grep.c), and newly written lines are parsed. Then more output,
then more content, etc.

If output of grep is less than 4096bytes (my system is debian i386) before
coming to somefile,  and grep is not called with --line-buffered option, that
behaviour never occurs. I think that's because data have not been flushed yet.
If output is more than 4096, that behaviour doesn't always occur. I couldn't
figure out what makes the difference, but I can always reproduce it with that
script :


# creates a directory
mkdir tmpdir

cd tmpdir

# creates two files
touch a
touch b

# writes 5000 caracters in another file
# name of that file comes after two other in lexical order
while [ $I -lt 5000 ]
    echo "a" >> c

# recursively grep directory,  redirects output
# output of grep must be more than 4096,
grep -r a . > log

One way to solve this problem would be to prevent grep from searching inside
a file if that file is associated with stdout. `cat' has that feature. I'll
submit a patch based on cat's implementation.


Reply to this item at:


  Message posté via/par Savannah

reply via email to

[Prev in Thread] Current Thread [Next in Thread]