Subject: bug#22181: endless loop in grep 2.22

From: Jim Meyering <[email-masked]>
Date: Sat, 9 Jan 2016 01:21:33 -0800

On Fri, Jan 8, 2016 at 6:53 PM, Paul Eggert <[email-masked]> wrote:
> Jim Meyering wrote:
>>
>> It was trivial: printf does not necessarily support \xHH hexadecimal
>> escapes.
>
> Thanks for catching that. I looked and found one other problem of that kind.
> I tried running the tests on Solaris and AIX and found a few more porting
> issues in the grep tests, and installed the attached.

Hah! TIL head -N and yes are not portable. Thank you.
I've been spoiled/corrupted by writing coreutils tests for so long.

I would prefer to continue to use "yes" via the following, at least
in the first test. That way is clearer. In the second, I could go either
way, since your awk process replaces both yes and head, at the
expense of being a bit less concise and less readable.

What do you think of this patch?
From a86e79c4510dee3398d863a3892d75158db5eab1 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email-masked]>
Date: Fri, 8 Jan 2016 19:23:25 -0800
Subject: [PATCH] tests: do use "yes" but via an AWK replacement

Also, use sed Nq in place of head -N
* tests/init.cfg (yes): Define.
* tests/max-count-overread: Revert to using "yes".
* tests/mb-non-UTF8-performance: Likewise, and use
"sed Nq" in place of head -N.
---
 tests/init.cfg                | 3 +++
 tests/max-count-overread      | 2 +-
 tests/mb-non-UTF8-performance | 4 +---
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/tests/init.cfg b/tests/init.cfg
index be03357..1c7baaa 100644
--- a/tests/init.cfg
+++ b/tests/init.cfg
@@ -200,3 +200,6 @@ user_time_()
     $child_user *= 1000;
     print int ($child_user + 0.5)' "$@"
 }
+
+# yes is not portable, fake it with $AWK
+yes() { ${AWK-awk} 'BEGIN{for (;;) print "'"$*"'"}'; }
diff --git a/tests/max-count-overread b/tests/max-count-overread
index ec98ac8..d7aafd6 100755
--- a/tests/max-count-overread
+++ b/tests/max-count-overread
@@ -9,7 +9,7 @@ fail=0

 echo x > exp || framework_failure_

-${AWK-awk} 'BEGIN{for (;;) print "x"}' | timeout 3 grep -m1 x > out || fail=1
+yes x | timeout 3 grep -m1 x > out || fail=1
 compare exp out || fail=1

 Exit $fail
diff --git a/tests/mb-non-UTF8-performance b/tests/mb-non-UTF8-performance
index c350dfb..3f9209c 100755
--- a/tests/mb-non-UTF8-performance
+++ b/tests/mb-non-UTF8-performance
@@ -31,9 +31,7 @@ expensive_
 # the unibyte test takes at least 10ms of user time.
 n_lines=100000
 while :; do
-  ${AWK-awk} -v n_lines=$n_lines \
-      'BEGIN {while (n_lines--) printf "%078d\n", 0}' </dev/null >in ||
-    framework_failure_
+  yes $(printf '%078d' 0) | sed ${n_lines}q > in || framework_failure_
   ubyte_ms=$(LC_ALL=C user_time_ 1 grep -i foobar in) || fail=1
   test $ubyte_ms -ge 10 && break
   n_lines=$(expr $n_lines + 200000)
-- 
2.6.4