bug-grep
[Top][All Lists]
Advanced

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

Re: [bug #34917] grep refuses to run when input file is also the output,


From: Jim Meyering
Subject: Re: [bug #34917] grep refuses to run when input file is also the output, even when the -q option is used
Date: Mon, 28 Nov 2011 16:03:36 +0100

Nicolas Vigier wrote:
> Follow-up Comment #1, bug #34917 (project grep):
>
> Oops, I misunderstood the issue reported on Mageia bugzilla.
>
> The issue is a little different :
> When the -q option is used, there should not be any error output. grep version
> 2.10 outputs errors about "input file `./file' is also the output" even when
> the -q options is used.
>
> But not rejecting the command when the -q option is used should also fix this.

Thanks for forwarding that and for the patch.
I've added a comment and a proper commit log to your patch, but
that means you'll have to sign off on the grammar/etc. before I push.
Please reply here, and not on the savannah tracker.

I'll update NEWS and add a test separately.

Note that using -q and redirecting stdout is slightly nonsensical,
since you know there will be no standard output.


>From 5d24405b7748492c78291b739a37847c0206984c Mon Sep 17 00:00:00 2001
From: Nicolas Vigier <address@hidden>
Date: Mon, 28 Nov 2011 15:45:19 +0100
Subject: [PATCH] do not reject "grep -qr . > out"

The recent fix to avoid an infinite disk-filling loop, commit 5e20a38a,
introduced a minor regression.  If you use grep with -q and -r, and
redirect output to a file that will be traversed, then grep would
reject the command, even though it will generate no output.
In that case, there is no risk of an infinite loop.
A similar argument holds for uses of --max-count=N, though for
sufficiently large N, our sun will expire before we reach the limit,
infinite loop or not.
* src/main.c (grepfile): Do not reject input == output when
using either --quiet/--silent (-q) or --max-count (-m).
Reported by J H Wilson in http://bugs.mageia.org/show_bug.cgi?id=3501
---
 src/main.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index c70156f..efb9676 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1225,8 +1225,11 @@ grepfile (char const *file, struct stats *stats)
          disk before we open this file, we can end up reading and matching
          those lines and appending them to the file from which we're reading.
          Then we'd have what appears to be an infinite loop that'd terminate
-         only upon filling the output file system or reaching a quota.  */
-      if (S_ISREG (stats->stat.st_mode) && out_stat.st_ino
+         only upon filling the output file system or reaching a quota.
+         However, there is no risk of an infinite loop if grep is generating
+         no output (-q) or when limiting output via --max-count=N.  */
+      if (!out_quiet && max_count < TYPE_MAXIMUM (off_t)
+          && S_ISREG (stats->stat.st_mode) && out_stat.st_ino
           && SAME_REGULAR_FILE (stats->stat, out_stat))
         {
           error (0, 0, _("input file %s is also the output"), quote (file));
--
1.7.8.rc3.31.g017d1



reply via email to

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