bug-grep
[Top][All Lists]
Advanced

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

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


From: Jim Meyering
Subject: Re: bug #17457: "grep -r foo . > somefile" goes into an infinite loop
Date: Sun, 24 Jul 2011 16:52:58 +0200

Jim Meyering wrote:
> Jim Meyering wrote:
> ...
>> Here's a preliminary patch.
>> I will add a test case and update NEWS.  In the mean time, if anyone knows
>> who submitted the proposed patch, I'll mention that it inspired mine.
>>
>> With that change, now I see this behavior:
>>
>>     $ mkdir d && touch d/k && ./grep -r pat d > d/k
>>     ./grep: input file `d/k' is also the output
>>     [Exit 2]
>>
>> Before, depending on pattern/contents, it would
>> either exit 0, 1 or fill your partition.
> ...
>> +      if (S_ISREG (stats->stat.st_mode) && out_stat.st_ino
>> +          && SAME_REGULAR_FILE (stats->stat, out_stat))
>> +        {
>> +          if (! suppress_errors)
>> +            error (0, 0, _("input file %s is also the output"),
>> +                   quote (file));
>> +          errseen = 1;
>> +          return 1;
>> +        }
>
> I've made the diagnostic unconditional,
> added a test script and a NEWS entry:
> (FYI, you don't need to use -r to trigger the bug -- see the test)
>
> Subject: [PATCH 1/2] fail with exit status 2 when an input file is the same
>  as the output
>
> This avoids a potential "infinite" disk-filling loop.
> Reported in http://savannah.gnu.org/patch/?5316
> and http://savannah.gnu.org/bugs/?17457.
> * src/main.c: Include "quote.h".
> (out_stat): New global.
> (grepfile): Compare each regular file's dev/ino/etc.
> with those from the file on stdout (if it too is regular).
> (main): Set out_stat, if stdout is a regular file.
> * src/system.h: Include "same-inode.h".
> (same_file_attributes): Define.  From diffutils.
> (SAME_REGULAR_FILE): Define.
> * bootstrap.conf (gnulib_modules): Use quote, not quotearg.
> Use same-inode.
> * NEWS (Bug fixes): Mention it.
> ---
>  NEWS           |    5 +++++
>  bootstrap.conf |    3 ++-
>  src/main.c     |   26 ++++++++++++++++++++++++++
>  src/system.h   |   42 +++++++++++++++++++++++++++++++++++++++++-
>  4 files changed, 74 insertions(+), 2 deletions(-)
>
> diff --git a/NEWS b/NEWS
> index 6e5d38b..7d87bb1 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -2,6 +2,11 @@ GNU grep NEWS                                    -*- outline 
> -*-
>
>  * Noteworthy changes in release ?.? (????-??-??) [?]
>
> +  grep now rejects a command like "grep -r pattern . > out",
> +  in which the output file is also one of the inputs,
> +  because it can result in an "infinite" disk-filling loop.
> +  [bug introduced in grep-???]

Fixed a nit by replacing that line with this one:

  [bug present since "the beginning"]



reply via email to

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