autoconf-patches
[Top][All Lists]
Advanced

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

`cat` vs. read


From: Eric Blake
Subject: `cat` vs. read
Date: Mon, 27 Oct 2008 16:44:03 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

POSIX does not require read to work on non-text files; unfortunately, 
AC_COMPUTE_INT creates a non-text file (it intentionally lacks a newline, due 
to \r\n issues encountered with cat when targetting mingw).  However, every 
shell I've tried (ash, pdksh, bash 2.05a, bash 3.2, zsh, Solaris /bin/sh, 
OpenBSD /bin/sh) successfully and silently populates foo, even though $? is 
(properly) set to 1 due to the early EOF:

$ foo=; printf 'abc' > file; read foo < file; echo $? $foo
1 abc

Does anyone know of a shell where this patch would fail (either because the 
shell spits out an error message, or because foo is unpopulated due to the 
nonzero status)?  Should I go ahead and apply this patch, particularly since 
the testsuite will catch any broken shells?

There are other uses of foo=`cat file` that I did not replace, because I could 
not guarantee that file contained only one line, no IFS characters, and no \.

From: Eric Blake <address@hidden>
Date: Mon, 27 Oct 2008 10:36:59 -0600
Subject: [PATCH] Use read, rather than `cat`, for safe one-line files.

* lib/autotest/general.m4 (AT_CLEANUP): Avoid a fork, since it is
known that the file has only one line and no \.
* lib/autoconf/general.m4 (_AC_COMPUTE_INT_RUN): Likewise.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog               |    7 +++++++
 lib/autoconf/general.m4 |    5 ++++-
 lib/autotest/general.m4 |    2 +-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a01ce1d..f77faa1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-10-27  Eric Blake  <address@hidden>
+
+       Use read, rather than `cat`, for safe one-line files.
+       * lib/autotest/general.m4 (AT_CLEANUP): Avoid a fork, since it is
+       known that the file has only one line and no \.
+       * lib/autoconf/general.m4 (_AC_COMPUTE_INT_RUN): Likewise.
+
 2008-10-27  Paolo Bonzini  <address@hidden>
 
        * lib/autoconf/types.m4 (_AC_CHECK_TYPE_NEW_BODY): Extract
diff --git a/lib/autoconf/general.m4 b/lib/autoconf/general.m4
index 2a20d0d..ece0421 100644
--- a/lib/autoconf/general.m4
+++ b/lib/autoconf/general.m4
@@ -2906,9 +2906,12 @@ esac[]dnl
 # _AC_COMPUTE_INT_RUN(EXPRESSION, VARIABLE, PROLOGUE, [IF-FAILS])
 # ---------------------------------------------------------------
 # Store the evaluation of the integer EXPRESSION in VARIABLE.
+#
+# This use of read(1) will always return non-zero status, since
+# AC_LANG_INT_SAVE intentionally does not end the file in a newline.
 m4_define([_AC_COMPUTE_INT_RUN],
 [_AC_RUN_IFELSE([AC_LANG_INT_SAVE([$3], [$1])],
-               [$2=`cat conftest.val`], [$4])])
+               [read $2 <conftest.val; :], [$4])])
 
 
 # AC_COMPUTE_INT(VARIABLE, EXPRESSION, PROLOGUE, [IF-FAILS])
diff --git a/lib/autotest/general.m4 b/lib/autotest/general.m4
index 149fff1..00817b1 100644
--- a/lib/autotest/general.m4
+++ b/lib/autotest/general.m4
@@ -1674,7 +1674,7 @@ m4_undivert([TEST_SCRIPT])dnl Insert the code here
   $at_traceoff
   $at_times_p && times >"$at_times_file"
 ) AS_MESSAGE_LOG_FD>&1 2>&1 | eval $at_tee_pipe
-at_status=`cat "$at_status_file"`
+read at_status <"$at_status_file"
 [#AT_STOP_]AT_ordinal
 m4_divert_pop([TEST_GROUPS])dnl Back to KILL.
 ])# AT_CLEANUP
-- 
1.6.0.2







reply via email to

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