|
Packit Service |
2723c6 |
#!/bin/sh
|
|
Packit Service |
2723c6 |
# Detect printf(3) failure even when it doesn't set stream error indicator
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Copyright (C) 2007-2018 Free Software Foundation, Inc.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
2723c6 |
# it under the terms of the GNU General Public License as published by
|
|
Packit Service |
2723c6 |
# the Free Software Foundation, either version 3 of the License, or
|
|
Packit Service |
2723c6 |
# (at your option) any later version.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# This program is distributed in the hope that it will be useful,
|
|
Packit Service |
2723c6 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
2723c6 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
2723c6 |
# GNU General Public License for more details.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# You should have received a copy of the GNU General Public License
|
|
Packit Service |
2723c6 |
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
prog=printf
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
|
|
Packit Service |
2723c6 |
print_ver_ printf
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
vm=$(get_min_ulimit_v_ env $prog %20f 0) \
|
|
Packit Service |
2723c6 |
|| skip_ "this shell lacks ulimit support"
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Up to coreutils-6.9, "printf %.Nf 0" would encounter an ENOMEM internal
|
|
Packit Service |
2723c6 |
# error from glibc's printf(3) function whenever N was large relative to
|
|
Packit Service |
2723c6 |
# the size of available memory. As of Oct 2007, that internal stream-
|
|
Packit Service |
2723c6 |
# related failure was not reflected (for any libc I know of) in the usual
|
|
Packit Service |
2723c6 |
# stream error indicator that is tested by ferror. The result was that
|
|
Packit Service |
2723c6 |
# while the printf command obviously failed (generated no output),
|
|
Packit Service |
2723c6 |
# it mistakenly exited successfully (exit status of 0).
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Testing it is tricky, because there is so much variance
|
|
Packit Service |
2723c6 |
# in quality for this corner of printf(3) implementations.
|
|
Packit Service |
2723c6 |
# Most implementations do attempt to allocate N bytes of storage.
|
|
Packit Service |
2723c6 |
# Using the maximum value for N (2^31-1) causes glibc-2.7 to try to
|
|
Packit Service |
2723c6 |
# allocate almost 2^64 bytes, while freeBSD 6.1's implementation
|
|
Packit Service |
2723c6 |
# correctly outputs almost 2GB worth of 0's, which takes too long.
|
|
Packit Service |
2723c6 |
# We want to test implementations that allocate N bytes, but without
|
|
Packit Service |
2723c6 |
# triggering the above extremes.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Some other versions of glibc-2.7 have a snprintf function that segfaults
|
|
Packit Service |
2723c6 |
# when an internal (technically unnecessary!) memory allocation fails.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# The compromise is to limit virtual memory to something reasonable,
|
|
Packit Service |
2723c6 |
# and to make an N-byte-allocating-printf require more than that, thus
|
|
Packit Service |
2723c6 |
# triggering the printf(3) misbehavior -- which, btw, is required by ISO C99.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
mkfifo_or_skip_ fifo
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
(trap '' PIPE && yes | :) 2>&1 | grep -qF 'Broken pipe' ||
|
|
Packit Service |
2723c6 |
skip_ 'trapping SIGPIPE is not supported'
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Disable MALLOC_PERTURB_, to avoid triggering this bug
|
|
Packit Service |
2723c6 |
# https://bugs.debian.org/481543#77
|
|
Packit Service |
2723c6 |
export MALLOC_PERTURB_=0
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Terminate any background process
|
|
Packit Service |
2723c6 |
cleanup_() { kill $pid 2>/dev/null && wait $pid; }
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
head -c 10 fifo > out & pid=$!
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Trigger large mem allocation failure
|
|
Packit Service |
2723c6 |
( trap '' PIPE && ulimit -v $vm && env $prog %20000000f 0 2>err-msg > fifo )
|
|
Packit Service |
2723c6 |
exit=$?
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# Map this longer, and rarer, diagnostic to the common one.
|
|
Packit Service |
2723c6 |
# printf: cannot perform formatted output: Cannot allocate memory"
|
|
Packit Service |
2723c6 |
sed 's/cannot perform .*/write error/' err-msg > k && mv k err-msg
|
|
Packit Service |
2723c6 |
err_msg=$(tr '\n' : < err-msg)
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# By some bug, on Solaris 11 (5.11 snv_86), err_msg ends up
|
|
Packit Service |
2723c6 |
# containing '1> fifo:printf: write error:'. Recognize that, too.
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
case $err_msg in
|
|
Packit Service |
2723c6 |
"$prog: write error:"*) diagnostic=y ;;
|
|
Packit Service |
2723c6 |
"1> fifo:$prog: write error:") diagnostic=y ;;
|
|
Packit Service |
2723c6 |
'') diagnostic=n ;;
|
|
Packit Service |
2723c6 |
*) diagnostic=unexpected ;;
|
|
Packit Service |
2723c6 |
esac
|
|
Packit Service |
2723c6 |
n_out=$(wc -c < out)
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
case $n_out:$diagnostic:$exit in
|
|
Packit Service |
2723c6 |
10:n:0) ;; # ok, succeeds w/no diagnostic: FreeBSD 6.1
|
|
Packit Service |
2723c6 |
10:y:1) ;; # ok, fails with EPIPE diagnostic: musl libc
|
|
Packit Service |
2723c6 |
0:y:1) ;; # ok, glibc-2.8 and newer, when printf(3) fails with ENOMEM
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# With MALLOC_PERTURB_=0, this no longer happens.
|
|
Packit Service |
2723c6 |
# *:139) # segfault; known bug at least in debian unstable's libc6 2.7-11
|
|
Packit Service |
2723c6 |
# echo 1>&2 "$0: bug in snprintf causes low-mem use of printf to segfault"
|
|
Packit Service |
2723c6 |
# fail=77;;
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
# 10:y) ;; # Fail: doesn't happen: nobody succeeds with a diagnostic
|
|
Packit Service |
2723c6 |
# 0:n) ;; # Fail pre-patch: no output, no diag
|
|
Packit Service |
2723c6 |
*) fail=1;;
|
|
Packit Service |
2723c6 |
esac
|
|
Packit Service |
2723c6 |
|
|
Packit Service |
2723c6 |
Exit $fail
|