Skip to content

Commit 36ce3a2

Browse files
committed
lddtree: fix shellcheck linting warnings and errors
1 parent dd447ed commit 36ce3a2

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ Kyuafile:
1616
echo "include('tests/Kyuafile')" >> $@.tmp
1717
mv $@.tmp $@
1818

19+
lint:
20+
shellcheck -e SC3043 lddtree.sh

lddtree.sh

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
argv0=${0##*/}
88
version=1.26
99

10-
: ${ROOT:=/}
10+
: "${ROOT:=/}"
1111

1212
[ "${ROOT}" = "${ROOT%/}" ] && ROOT="${ROOT}/"
1313
[ "${ROOT}" = "${ROOT#/}" ] && ROOT="${PWD}/${ROOT}"
@@ -24,14 +24,17 @@ error() {
2424

2525
# functions for scanelf backend
2626
elf_rpath_scanelf() {
27+
# shellcheck disable=SC2317
2728
scanelf -qF '#F%r' "$@"
2829
}
2930

3031
elf_interp_scanelf() {
32+
# shellcheck disable=SC2317
3133
scanelf -qF '#F%i' "$@"
3234
}
3335

3436
elf_needed_scanelf() {
37+
# shellcheck disable=SC2317
3538
scanelf -qF '#F%n' "$@"
3639
}
3740

@@ -44,12 +47,14 @@ elf_specs_scanelf() {
4447
# With glibc, the NONE, SYSV, GNU, and LINUX OSABI's are compatible.
4548
# LINUX and GNU are the same thing, as are NONE and SYSV, so normalize
4649
# GNU & LINUX to NONE. #442024 #464380
50+
# shellcheck disable=SC2317
4751
scanelf -BF '#F%a %M %D %I' "$1" | \
4852
sed -r 's: (LINUX|GNU)$: NONE:'
4953
}
5054

5155
# functions for binutils backend
5256
elf_rpath_binutils() {
57+
# shellcheck disable=SC2317
5358
objdump -x "$@" | awk '$1 == "RUNPATH" { if($2!="") {runpath=$2;} } $1 == "RPATH" { if($2!="") {rpath=$2;} } END { if(runpath!="") {print runpath;} else {print rpath;} }'
5459
}
5560

@@ -59,16 +64,19 @@ elf_interp_binutils() {
5964
# String dump of section '.interp':
6065
# [ 0] /lib/ld-musl-x86_64.so.1
6166
#
67+
# shellcheck disable=SC2317
6268
readelf -p .interp "$1" | sed -E -n '/\[\s*[0-9]\]/s/^\s*\[.*\]\s*(.*)/\1/p'
6369
}
6470

6571
elf_needed_binutils() {
72+
# shellcheck disable=SC2317
6673
objdump -x "$@" | awk '$1 == "NEEDED" { line=line sep $2 ; sep="," } END { print line }'
6774
}
6875

6976
elf_specs_binutils() {
7077
# get Class, Data, Machine and OS/ABI.
7178
# the OS/ABI 'GNU', 'System V' and 'Linux' are compatible so normalize
79+
# shellcheck disable=SC2317
7280
readelf -h "$1" \
7381
| awk -F: '$1 ~ /Class|Data|Machine|OS.ABI/ {gsub(/^ +/, "", $2); print $2}' \
7482
| sed -E -e 's/UNIX - (System V|Linux|GNU)/UNIX/' \
@@ -82,7 +90,7 @@ elf_needed() { elf_needed_$BACKEND "$@"; }
8290
elf_specs() { elf_specs_$BACKEND "$1"; }
8391

8492
unset lib_paths_fallback
85-
for p in ${ROOT}lib* ${ROOT}usr/lib* ${ROOT}usr/local/lib*; do
93+
for p in "${ROOT}"lib* "${ROOT}"usr/lib* "${ROOT}"usr/local/lib*; do
8694
lib_paths_fallback="${lib_paths_fallback}${lib_paths_fallback:+:}${p}"
8795
done
8896
c_ldso_paths_loaded='false'
@@ -98,12 +106,13 @@ find_elf() {
98106
local elf="$1"
99107
local pathstr="$2"
100108
IFS=:
109+
# shellcheck disable=SC2086
101110
set -- $pathstr
102111
unset IFS
103112
local path pe
104113
for path ; do
105-
: ${path:=${PWD}}
106-
if [ "${path#${ROOT}}" = "${path}" ]; then
114+
: "${path:=${PWD}}"
115+
if [ "${path#"${ROOT}"}" = "${path}" ]; then
107116
path="${ROOT}${path#/}"
108117
fi
109118
pe="${path%/}/${elf#/}"
@@ -139,10 +148,10 @@ find_elf() {
139148
# if the glob didnt match anything #360041,
140149
# or the files arent readable, skip it
141150
[ -r "${p}" ] || continue
142-
while read line ; do
151+
while read -r line ; do
143152
case ${line} in
144153
"#"*) ;;
145-
"include "*) read_ldso_conf ${line#* } ;;
154+
"include "*) read_ldso_conf "${line#* }" ;;
146155
*) c_ldso_paths="$c_ldso_paths:${ROOT}${line#/}";;
147156
esac
148157
done <"${p}"
@@ -162,7 +171,7 @@ find_elf() {
162171
if [ -r "$ldso_conf" ]; then
163172
read_ldso_conf "$ldso_conf"
164173
fi
165-
cd "$_oldpwd"
174+
cd "$_oldpwd" || return
166175
fi
167176
fi
168177
if [ -n "${c_ldso_paths}" ] ; then
@@ -189,20 +198,20 @@ resolv_links() {
189198
_list_files="$2"
190199
local oldpwd="$PWD"
191200
[ "$_list_files" = yes ] && list_existing_file "${_resolv_links}"
192-
cd "${_resolv_links%/*}"
201+
cd "${_resolv_links%/*}" || return
193202
while [ -L "$_resolv_links" ]; do
194203
_resolv_links=$(readlink "$_resolv_links")
195204
case "$_resolv_links" in
196205
/*) _resolv_links="${ROOT}${_resolv_links#/}"
197-
cd "${_resolv_links%/*}"
206+
cd "${_resolv_links%/*}" || break
198207
;;
199-
*/*) cd "${_resolv_links%/*}"
208+
*/*) cd "${_resolv_links%/*}" || break
200209
;;
201210
esac
202211
_resolv_links=$(pwd -P)/${_resolv_links##*/}
203212
[ "$_list_files" = yes ] && list_existing_file "${_resolv_links}"
204213
done
205-
cd "$oldpwd"
214+
cd "$oldpwd" || return
206215
}
207216

208217
show_elf() {
@@ -216,7 +225,7 @@ show_elf() {
216225
${LIST} || printf "%${indent}s%s => " "" "${elf}"
217226
case ",${parent_elfs}," in
218227
*,${elf},*)
219-
${LIST} || printf "!!! circular loop !!!\n" ""
228+
${LIST} || printf "!!! circular loop !!!\n"
220229
return
221230
;;
222231
esac
@@ -225,10 +234,10 @@ show_elf() {
225234
resolv_links "${resolved:-$1}" yes
226235
else
227236
resolv_links "${resolved:-$1}" no
228-
printf "${resolved:-not found}"
237+
printf "%s" "${resolved:-not found}"
229238
fi
230239
resolved=${_resolv_links}
231-
if [ ${indent:-0} -eq 0 ] ; then
240+
if [ "${indent:-0}" -eq 0 ] ; then
232241
elf_specs=$(elf_specs "${resolved}")
233242
interp=$(elf_interp "${resolved}")
234243
# ignore interpreters that do not have absolute path
@@ -238,7 +247,7 @@ show_elf() {
238247
if ${LIST} ; then
239248
[ -n "${interp}" ] && resolv_links "${interp}" yes
240249
else
241-
printf " (interpreter => ${interp:-none})"
250+
printf "%s" " (interpreter => ${interp:-none})"
242251
fi
243252
if [ -r "${interp}" ] ; then
244253
# Extract the default lib paths out of the ldso.
@@ -264,6 +273,7 @@ show_elf() {
264273

265274
oifs="$IFS"
266275
IFS=,
276+
# shellcheck disable=SC2086
267277
set -- ${libs}
268278
IFS="$oifs"
269279

@@ -300,8 +310,7 @@ SET_X=false
300310
LIST=false
301311
AUTO_ROOT=true
302312

303-
OPTS=$(getopt -n "$argv0" -o "ahlR:Vx" -l "all,help,list,no-auto-root,root:,version,debug" -- "$@")
304-
if [ $? -ne 0 ]; then
313+
if ! OPTS=$(getopt -n "$argv0" -o "ahlR:Vx" -l "all,help,list,no-auto-root,root:,version,debug" -- "$@"); then
305314
usage >&2
306315
exit 1
307316
fi
@@ -349,7 +358,7 @@ for elf ; do
349358
error "${elf}: file is not readable"
350359
elif [ -d "${elf}" ] ; then
351360
if $LIST; then
352-
echo ${elf}
361+
echo "${elf}"
353362
else
354363
error "${elf}: is a directory"
355364
fi

0 commit comments

Comments
 (0)