tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / misc / printf-surprise
1 #!/bin/sh
2 # Detect printf(3) failure even when it doesn't set stream error indicator
3
4 # Copyright (C) 2007-2009 Free Software Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 prog=printf
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   env $prog --version
24 fi
25
26 . $srcdir/test-lib.sh
27 require_ulimit_
28
29
30 # Up to coreutils-6.9, "printf %.Nf 0" would encounter an ENOMEM internal
31 # error from glibc's printf(3) function whenever N was large relative to
32 # the size of available memory.  As of Oct 2007, that internal stream-
33 # related failure was not reflected (for any libc I know of) in the usual
34 # stream error indicator that is tested by ferror.  The result was that
35 # while the printf command obviously failed (generated no output),
36 # it mistakenly exited successfully (exit status of 0).
37
38 # Testing it is tricky, because there is so much variance
39 # in quality for this corner of printf(3) implementations.
40 # Most implementations do attempt to allocate N bytes of storage.
41 # Using the maximum value for N (2^31-1) causes glibc-2.7 to try to
42 # allocate almost 2^64 bytes, while freeBSD 6.1's implementation
43 # correctly outputs almost 2GB worth of 0's, which takes too long.
44 # We want to test implementations that allocate N bytes, but without
45 # triggering the above extremes.
46
47 # Some other versions of glibc-2.7 have a snprintf function that segfaults
48 # when an internal (technically unnecessary!) memory allocation fails.
49
50 # The compromise is to limit virtual memory to something reasonable,
51 # and to make an N-byte-allocating-printf require more than that, thus
52 # triggering the printf(3) misbehavior -- which, btw, is required by ISO C99.
53
54 mkfifo fifo || framework_failure
55
56 # Disable MALLOC_PERTURB_, to avoid triggering this bug
57 # http://bugs.debian.org/481543#77
58 export MALLOC_PERTURB_=0
59
60 head -c 10 fifo > out &
61 ( ulimit -v 10000; env $prog %20000000f 0 2>err-msg > fifo )
62 exit=$?
63
64 # Map this longer, and rarer, diagnostic to the common one.
65 # printf: cannot perform formatted output: Cannot allocate memory" \
66 sed 's/cannot perform .*/write error/' err-msg > k && mv k err-msg
67 err_msg=$(cat err-msg|tr '\n' :)
68
69 # By some bug, on Solaris 11 (5.11 snv_86), err_msg ends up
70 # containing '1> fifo:printf: write error:'.  Recognize that, too.
71
72 case $err_msg in
73   "$prog: write error:") diagnostic=y ;;
74   "1> fifo:$prog: write error:") diagnostic=y ;;
75   '') diagnostic=n ;;
76   *) diagnostic=unexpected ;;
77 esac
78 n_out=$(wc -c < out)
79
80 case $n_out:$diagnostic:$exit in
81   10:n:0) ;; # ok, succeeds w/no diagnostic: FreeBSD 6.1
82   0:y:1)  ;; # ok, glibc-2.8 and newer, when printf(3) fails with ENOMEM
83
84   # With MALLOC_PERTURB_=0, this no longer happens.
85   # *:139)     # segfault; known bug at least in debian unstable's libc6 2.7-11
86   #    echo 1>&2 "$0: bug in snprintf causes low-mem use of printf to segfault"
87   #    fail=77;;
88
89   # 10:y) ;; # Fail: doesn't happen: nobody succeeds with a diagnostic
90   # 0:n)  ;; # Fail pre-patch: no output, no diag
91   *) fail=1;;
92 esac
93
94 Exit $fail