Subject: bug#13472: [PATCH] libtool: speed up by pre-cutting sed's input by dd

From: Peter Rosin <[email-masked]>
Date: Tue, 11 Feb 2014 18:46:26 +0100

On 2013-06-05 10:20, Pavel Raiskup wrote:
> On Monday, May 13, 2013 02:03:51 PM Pavel Raiskup wrote:
>> On Monday, April 29, 2013 08:37:49 Pavel Raiskup wrote:
>>> Ping & update :),
>>
>> Another ping.
> 
> ... and another, should I let it be?  Thanks, Pavel
> 
> Context catcher:
> http://lists.gnu.org/archive/html/bug-libtool/2013-04/msg00009.html

Ok Pavel, first of all, sorry for the "smallish" delay in handling
your request, and Nick, are you able to test this patch?

I didn't like some aspects of Pavels patch, it seemed convoluted and
the dd check didn't actually check if dd behaved as expected. But, I
rewrote the patch instead of describing in words exactly what I mean
and I hope I'm not stepping on any toes by that action... Anyway, this
has the dubious benefit that we do not have to wait for Pavel to assign
copyrights (I couldn't find him in any list).

Does my version work? The testsuite behaves OK for me, but other than
that I haven't tested.

Cheers,
Peter

>From 718f64eeeda938634fbdf14f99b33409376b7960 Mon Sep 17 00:00:00 2001
From: Peter Rosin <[email-masked]>
Date: Tue, 11 Feb 2014 18:39:39 +0100
Subject: [PATCH] libtool: speed up lalib detection in execute mode

Execute mode is slow and might even DOS the computer in extreme
cases when a parameter is a big binary file without newlines.
Work around this by truncation if the dd utility is found and
working.

Fixes bug#13472 and bug#16662.

Reported by Pavel Raiskup and Nick Bowler.

* m4/libtool.m4 (_LT_CMD_TRUNCATE): New macro, for finding out
how to truncate pipes to 4k (fallback to no truncation if that
is not possible).
(_LT_SETUP): Require _LT_CMD_TRUNCATE.
* build_aux/ltmain.in (func_lalib_p): Truncate the pipe before
passing it on to sed.
* THANKS: Update.

Signed-off-by: Peter Rosin <[email-masked]>
---
 THANKS              |    2 ++
 build-aux/ltmain.in |    2 +-
 m4/libtool.m4       |   22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+), 1 deletions(-)

diff --git a/THANKS b/THANKS
index e895aee..85c5150 100644
--- a/THANKS
+++ b/THANKS
@@ -150,6 +150,7 @@
   Mike Gorchak			[email-masked]
   Mike Frysinger		[email-masked]
   Mike Miller			[email-masked]
+  Nick Bowler			[email-masked]
   Nix				[email-masked]
   Olaf Lenz			[email-masked]
   Olly Betts			[email-masked]
@@ -161,6 +162,7 @@
   Paul Eggert			[email-masked]
   Paul Laight			[email-masked]
   Paul Seidler			[email-masked]
+  Pavel Raiskup			[email-masked]
   Paweł Daniluk			[email-masked]
   Peter Eisentraut		[email-masked]
   Peter Fritzsche		[email-masked]
diff --git a/build-aux/ltmain.in b/build-aux/ltmain.in
index 3b4e6ec..bff642e 100644
--- a/build-aux/ltmain.in
+++ b/build-aux/ltmain.in
@@ -577,7 +577,7 @@ _LTECHO_EOF'
 func_lalib_p ()
 {
     test -f "$1" &&
-      $SED -e 4q "$1" 2>/dev/null \
+      $lt_truncate_4k < "$1" 2>/dev/null | $SED -e 4q 2>/dev/null \
         | $GREP "^# Generated by .*$PACKAGE" > /dev/null 2>&1
 }
 
diff --git a/m4/libtool.m4 b/m4/libtool.m4
index ce58f31..d5967d2 100644
--- a/m4/libtool.m4
+++ b/m4/libtool.m4
@@ -169,6 +169,7 @@ m4_require([_LT_CHECK_SHAREDLIB_FROM_LINKLIB])dnl
 m4_require([_LT_CMD_OLD_ARCHIVE])dnl
 m4_require([_LT_CMD_GLOBAL_SYMBOLS])dnl
 m4_require([_LT_WITH_SYSROOT])dnl
+m4_require([_LT_CMD_TRUNCATE])dnl
 
 _LT_CONFIG_LIBTOOL_INIT([
 # See if we are running on zsh, and set the options that allow our
@@ -3216,6 +3217,27 @@ _LT_TAGDECL([], [reload_cmds], [2])dnl
 ])# _LT_CMD_RELOAD

+# _LT_CMD_TRUNCATE
+# ----------------
+# find command to truncate a pipe to 4k
+m4_defun([_LT_CMD_TRUNCATE],
+[AC_PATH_PROG([DD], [dd])
+test -z "$DD" && DD=dd
+
+AC_CACHE_CHECK([how to truncate pipes to 4k], [lt_cv_truncate_4k],
+[lt_cv_truncate_4k=cat
+printf 0123456789abcdef0123456789abcdef >conftest.i
+cat conftest.i conftest.i >conftest2.i
+if "$DD" bs=32 count=1 <conftest2.i >conftest.out 2>/dev/null; then
+  cmp -s conftest.i conftest.out && lt_cv_truncate_4k="$DD bs=4096 count=1"
+fi
+rm -f conftest.i conftest2.i conftest.out])
+
+_LT_DECL([lt_truncate_4k], [lt_cv_truncate_4k], [1],
+  [Command to truncate a pipe to 4k])
+])# _LT_CMD_TRUNCATE
+
+
 # _LT_CHECK_MAGIC_METHOD
 # ----------------------
 # how to check for library dependencies
-- 
1.7.9