Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from shellcheck #463

Merged
merged 2 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
needs: [build_windows]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
name: patchelf
Expand Down
14 changes: 7 additions & 7 deletions tests/add-debug-tag.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)
READELF=${READELF:-readelf}

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp libsimple.so ${SCRATCH}/
cp libsimple.so "${SCRATCH}"/

# check there is no DT_DEBUG tag
debugTag=$($READELF -d ${SCRATCH}/libsimple.so)
debugTag=$($READELF -d "${SCRATCH}/libsimple.so")
echo ".dynamic before: $debugTag"
if echo "$debugTag" | grep -q DEBUG; then
echo "failed --add-debug-tag test. Expected no line with (DEBUG), got: $debugTag"
exit 1
fi

# set DT_DEBUG
../src/patchelf --add-debug-tag ${SCRATCH}/libsimple.so
../src/patchelf --add-debug-tag "${SCRATCH}/libsimple.so"

# check there is DT_DEBUG tag
debugTag=$($READELF -d ${SCRATCH}/libsimple.so)
debugTag=$($READELF -d "${SCRATCH}/libsimple.so")
echo ".dynamic before: $debugTag"
if ! echo "$debugTag" | grep -q DEBUG; then
echo "failed --add-debug-tag test. Expected line with (DEBUG), got: $debugTag"
Expand Down
25 changes: 13 additions & 12 deletions tests/add-rpath.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
mkdir -p ${SCRATCH}/libsA
mkdir -p ${SCRATCH}/libsB
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"
mkdir -p "${SCRATCH}/libsA"
mkdir -p "${SCRATCH}/libsB"

cp main ${SCRATCH}/
cp libfoo.so ${SCRATCH}/libsA/
cp libbar.so ${SCRATCH}/libsB/
cp main "${SCRATCH}"/
cp libfoo.so "${SCRATCH}/libsA/"
cp libbar.so "${SCRATCH}/libsB/"

../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsA ${SCRATCH}/main
../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsB ${SCRATCH}/main
../src/patchelf --force-rpath --add-rpath "$(pwd)/${SCRATCH}/libsA" "${SCRATCH}/main"
../src/patchelf --force-rpath --add-rpath "$(pwd)/${SCRATCH}/libsB" "${SCRATCH}/main"

if test "$(uname)" = FreeBSD; then
export LD_LIBRARY_PATH=$(pwd)/${SCRATCH}/libsB
LD_LIBRARY_PATH="$(pwd)/${SCRATCH}/libsB"
export LD_LIBRARY_PATH
fi

exitCode=0
(cd ${SCRATCH} && ./main) || exitCode=$?
(cd "${SCRATCH}" && ./main) || exitCode=$?

if test "$exitCode" != 46; then
echo "bad exit code!"
Expand Down
20 changes: 10 additions & 10 deletions tests/args-from-file.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp main ${SCRATCH}/
RANDOM_PATH=$(pwd)/${SCRATCH}/$RANDOM
echo -n ${RANDOM_PATH} >> ${SCRATCH}/add-rpath
cp main "${SCRATCH}"/
SOME_PATH=$(pwd)/${SCRATCH}/some-path
printf "%s" "$SOME_PATH" >> "${SCRATCH}"/add-rpath

! ../src/patchelf --print-rpath ${SCRATCH}/main | grep $RANDOM_PATH
../src/patchelf --add-rpath @${SCRATCH}/add-rpath ${SCRATCH}/main
../src/patchelf --print-rpath ${SCRATCH}/main | grep $RANDOM_PATH
../src/patchelf --print-rpath "${SCRATCH}"/main | grep "$SOME_PATH" && exit 1
../src/patchelf --add-rpath @"${SCRATCH}"/add-rpath "${SCRATCH}"/main
../src/patchelf --print-rpath "${SCRATCH}"/main | grep "$SOME_PATH"

# should print error message and fail
../src/patchelf --set-rpath @${SCRATCH}/does-not-exist ${SCRATCH}/main 2>&1 | grep "getting info about"
../src/patchelf --set-rpath @"${SCRATCH}"/does-not-exist "${SCRATCH}"/main 2>&1 | grep "getting info about"
25 changes: 13 additions & 12 deletions tests/big-dynstr.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
mkdir -p ${SCRATCH}/libsA
mkdir -p ${SCRATCH}/libsB
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"
mkdir -p "${SCRATCH}/libsA"
mkdir -p "${SCRATCH}/libsB"

cp big-dynstr ${SCRATCH}/
cp libfoo.so ${SCRATCH}/libsA/
cp libbar.so ${SCRATCH}/libsB/
cp big-dynstr "${SCRATCH}/"
cp libfoo.so "${SCRATCH}/libsA/"
cp libbar.so "${SCRATCH}/libsB/"

oldRPath=$(../src/patchelf --print-rpath ${SCRATCH}/big-dynstr)
oldRPath=$(../src/patchelf --print-rpath "${SCRATCH}/big-dynstr")
if test -z "$oldRPath"; then oldRPath="/oops"; fi
../src/patchelf --force-rpath --set-rpath $oldRPath:$(pwd)/${SCRATCH}/libsA:$(pwd)/${SCRATCH}/libsB ${SCRATCH}/big-dynstr
../src/patchelf --force-rpath --set-rpath "$oldRPath:$(pwd)/${SCRATCH}/libsA:$(pwd)/${SCRATCH}/libsB" "${SCRATCH}/big-dynstr"

if test "$(uname)" = FreeBSD; then
export LD_LIBRARY_PATH=$(pwd)/${SCRATCH}/libsB
LD_LIBRARY_PATH="$(pwd)/${SCRATCH}/libsB"
export LD_LIBRARY_PATH
fi

exitCode=0
cd ${SCRATCH} && ./big-dynstr || exitCode=$?
cd "${SCRATCH}" && ./big-dynstr || exitCode=$?

if test "$exitCode" != 46; then
echo "bad exit code!"
Expand Down
2 changes: 1 addition & 1 deletion tests/build-id.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)
READELF=${READELF:-readelf}

rm -rf "${SCRATCH}"
Expand Down
45 changes: 15 additions & 30 deletions tests/change-abi.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
#! /bin/sh -e

SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp simple-pie ${SCRATCH}/simple-pie
cp simple-pie "${SCRATCH}/simple-pie"

# Save the old OS ABI
OLDABI=`../src/patchelf --print-os-abi ${SCRATCH}/simple-pie`
OLDABI=$(../src/patchelf --print-os-abi "${SCRATCH}/simple-pie")
# Ensure it's not empty
test -n "$OLDABI"

# Change OS ABI and verify it has been changed
{
echo "System V"
echo "HP-UX"
echo "NetBSD"
echo "Linux"
echo "GNU Hurd"
echo "Solaris"
echo "AIX"
echo "IRIX"
echo "FreeBSD"
echo "Tru64"
echo "OpenBSD"
echo "OpenVMS"
} | {
while IFS="\n" read ABI; do
echo "Set OS ABI to '$ABI'..."
../src/patchelf --set-os-abi "$ABI" ${SCRATCH}/simple-pie

echo "Check is OS ABI is '$ABI'..."
NEWABI=`../src/patchelf --print-os-abi ${SCRATCH}/simple-pie`
test "$NEWABI" = "$ABI"
done
}
for ABI in "System V" "HP-UX" "NetBSD" "Linux" "GNU Hurd" "Solaris" "AIX" "IRIX" "FreeBSD" "Tru64" "OpenBSD" "OpenVMS"; do
echo "Set OS ABI to '$ABI'..."
../src/patchelf --set-os-abi "$ABI" "${SCRATCH}/simple-pie"

echo "Check is OS ABI is '$ABI'..."
NEWABI=$(../src/patchelf --print-os-abi "${SCRATCH}/simple-pie")
test "$NEWABI" = "$ABI"
done

# Reset OS ABI to the saved one
../src/patchelf --set-os-abi "$OLDABI" ${SCRATCH}/simple-pie
../src/patchelf --set-os-abi "$OLDABI" "${SCRATCH}/simple-pie"

# Verify we still can run the executable
${SCRATCH}/simple-pie
"${SCRATCH}/simple-pie"
4 changes: 2 additions & 2 deletions tests/contiguous-note-sections.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/sh -e

SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)

rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"
Expand All @@ -10,4 +10,4 @@ cp contiguous-note-sections "${SCRATCH}/"
# Running --set-interpreter on this binary should not produce the following
# error:
# patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections
../src/patchelf --set-interpreter ld-linux-x86-64.so.2 ${SCRATCH}/contiguous-note-sections
../src/patchelf --set-interpreter ld-linux-x86-64.so.2 "${SCRATCH}/contiguous-note-sections"
10 changes: 5 additions & 5 deletions tests/empty-note.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#! /bin/sh -e

SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
cp $(dirname $(readlink -f $0))/empty-note ${SCRATCH}/
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"
cp "$(dirname "$(readlink -f "$0")")/empty-note" "${SCRATCH}/"

# Running --set-interpreter on this binary should not produce the following
# error:
# patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections
../src/patchelf --set-interpreter ld-linux-x86-64.so.2 ${SCRATCH}/empty-note
../src/patchelf --set-interpreter ld-linux-x86-64.so.2 "${SCRATCH}/empty-note"
14 changes: 7 additions & 7 deletions tests/endianness.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)
PATCHELF="../src/patchelf"

for arch in ppc64 ppc64le; do
rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp ${srcdir}/endianness/${arch}/main ${srcdir}/endianness/${arch}/libtest.so ${SCRATCH}/
cp "${srcdir}/endianness/${arch}/main" "${srcdir}/endianness/${arch}/libtest.so" "${SCRATCH}/"

rpath="${PWD}/${SCRATCH}"

# set rpath to scratch dir
${PATCHELF} --output ${SCRATCH}/main-rpath --set-rpath $rpath ${SCRATCH}/main
${PATCHELF} --output "${SCRATCH}/main-rpath" --set-rpath "$rpath" "${SCRATCH}/main"

# attempt to shrink rpath, should not result in empty rpath
${PATCHELF} --output ${SCRATCH}/main-shrunk --shrink-rpath --debug ${SCRATCH}/main-rpath
${PATCHELF} --output "${SCRATCH}/main-shrunk" --shrink-rpath --debug "${SCRATCH}/main-rpath"

# check whether rpath is still present
if ! ${PATCHELF} --print-rpath ${SCRATCH}/main-shrunk | grep -q "$rpath"; then
if ! ${PATCHELF} --print-rpath "${SCRATCH}/main-shrunk" | grep -q "$rpath"; then
echo "rpath was removed for ${arch}"
exit 1
fi
Expand Down
28 changes: 16 additions & 12 deletions tests/force-rpath.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)
OBJDUMP=${OBJDUMP:-objdump}

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

SCRATCHFILE=${SCRATCH}/libfoo.so
cp libfoo.so $SCRATCHFILE
cp libfoo.so "$SCRATCHFILE"

doit() {
echo patchelf $*
../src/patchelf $* $SCRATCHFILE
set +x
../src/patchelf "$@" "$SCRATCHFILE"
set -x
}

expect() {
out=$(echo $($OBJDUMP -x $SCRATCHFILE | grep PATH))
out=$("$OBJDUMP" -x "$SCRATCHFILE" | grep PATH || true)

if [ "$out" != "$*" ]; then
echo "Expected '$*' but got '$out'"
exit 1
fi
for i in $out; do
if [ "$i" != "$1" ]; then
echo "Expected '$*' but got '$out'"
exit 1
fi
shift
done
}

doit --remove-rpath
expect
expect ""
doit --set-rpath foo
expect RUNPATH foo
doit --force-rpath --set-rpath foo
Expand Down
14 changes: 7 additions & 7 deletions tests/grow-file.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#! /bin/sh -e

SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp simple-pie ${SCRATCH}/simple-pie
cp simple-pie "${SCRATCH}/simple-pie"

# Add a 40MB rpath
tr -cd 'a-z0-9' < /dev/urandom | dd count=40 bs=1000000 > ${SCRATCH}/foo.bin
tr -cd 'a-z0-9' < /dev/urandom | dd count=40 bs=1000000 > "${SCRATCH}/foo.bin"

# Grow the file
../src/patchelf --add-rpath @${SCRATCH}/foo.bin ${SCRATCH}/simple-pie
../src/patchelf --add-rpath @"${SCRATCH}/foo.bin" "${SCRATCH}/simple-pie"
# Make sure we can still run it
${SCRATCH}/simple-pie
"${SCRATCH}/simple-pie"
4 changes: 2 additions & 2 deletions tests/invalid-elf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
# by a signal. This works because the exit code of processes that were
# killed by a signal is 128 plus the signal number.
killed_by_signal() {
[ $1 -ge 128 ]
[ "$1" -ge 128 ]
}


# The directory containing all our input files.
TEST_DIR=$(dirname $(readlink -f $0))/invalid-elf
TEST_DIR=$(dirname "$(readlink -f "$0")")/invalid-elf

# Each test case is listed here. The names should roughly indicate
# what makes the given ELF file invalid.
Expand Down
12 changes: 6 additions & 6 deletions tests/no-gnu-hash.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
SCRATCH=scratch/$(basename "$0" .sh)
STRIP=${STRIP:-strip}

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp simple ${SCRATCH}/
cp simple "${SCRATCH}/"

${STRIP} --remove-section=.gnu.hash ${SCRATCH}/simple
${STRIP} --remove-section=.gnu.hash "${SCRATCH}/simple"

# Check if patchelf handles binaries with GNU_HASH in dynamic section but
# without .gnu.hash section
../src/patchelf --set-interpreter /oops ${SCRATCH}/simple
../src/patchelf --set-interpreter /oops "${SCRATCH}/simple"
2 changes: 1 addition & 1 deletion tests/no-rpath-pie-powerpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fi
# Tests for powerpc PIE endianness regressions
readelfData=$(${READELF} -l ${SCRATCH}/no-rpath 2>&1)

if [ $(echo "$readelfData" | grep --count "PHDR") != 1 ]; then
if [ $(echo "$readelfData" | grep "PHDR" | wc -l) != 1 ]; then
# Triggered if PHDR errors appear on stderr
echo "Unexpected number of occurences of PHDR in readelf results"
exit 1
Expand Down
Loading