Shell static analysis

Add pre-commit hook for static analysis on shell scripts (shellcheck)
Fixed lots of issues flagged by shellcheck.
This commit is contained in:
Jesus Checa Hidalgo 2024-08-05 09:15:19 +02:00
commit cda9761d2e
13 changed files with 80 additions and 69 deletions

View file

@ -12,3 +12,10 @@ repos:
rev: 1.32.2
hooks:
- id: tmt-lint
- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.10.0
hooks:
- id: shellcheck
require_serial: true # Podman has trouble running concurrently
args: ["--exclude=SC1091"] # Ignore "Not following" sourced scripts

View file

@ -1,10 +1,11 @@
#!/bin/bash
# shellcheck disable=SC2086
set -ex pipefail
cflags=`rpm -D '%toolchain clang' -E %{build_cflags}`
cxxflags=`rpm -D '%toolchain clang' -E %{build_cxxflags}`
ldflags=`rpm -D '%toolchain clang' -E %{build_ldflags}`
cflags=$(rpm -D '%toolchain clang' -E '%{build_cflags}')
cxxflags=$(rpm -D '%toolchain clang' -E '%{build_cxxflags}')
ldflags=$(rpm -D '%toolchain clang' -E '%{build_ldflags}')
# Test a c program

View file

@ -1,4 +1,4 @@
#!/bin/sh -eux
#!/bin/bash -eux
tmp=$(mktemp -d)
@ -6,7 +6,7 @@ tmp=$(mktemp -d)
# gcc-toolset-XX, in such case we need to test the compatibility with that one.
# We can get that from `clang -v` output.
TOOLSET=$(clang -v |& grep "Selected GCC installation" | grep -P -o '(dev|gcc-)toolset-[0-9]*') ||:
if [[ "x" = "x${TOOLSET}" ]]; then
if [[ "" = "${TOOLSET}" ]]; then
GCC="g++"
else
GCC="scl enable ${TOOLSET} -- g++"
@ -14,13 +14,13 @@ else
fi
# Build the source with GCC, link it with clang
${GCC} -c hello.cpp -o ${tmp}/hello.o
clang++ -o ${tmp}/hello ${tmp}/hello.o
${tmp}/hello | grep "Hello world"
rm -rf ${tmp}/*
${GCC} -c hello.cpp -o "${tmp}/hello.o"
clang++ -o "${tmp}"/hello "${tmp}/hello.o"
"${tmp}/hello" | grep "Hello world"
rm -rf "${tmp:?}"/*
# Build the source with clang, link it with GCC
clang++ -c hello.cpp -o ${tmp}/hello.o
${GCC} -o ${tmp}/hello ${tmp}/hello.o
${tmp}/hello | grep "Hello world"
rm -rf ${tmp}/*
clang++ -c hello.cpp -o "${tmp}/hello.o"
${GCC} -o "${tmp}"/hello "${tmp}/hello.o"
"${tmp}/hello" | grep "Hello world"
rm -rf "${tmp:?}"/*

View file

@ -81,7 +81,7 @@ cloneKernelTree() {
sleep $delay
fi
done
return $retcode
return "$retcode"
}
# In a kernel-ark repository, do all the operations needed to generate an SRPM
@ -102,8 +102,17 @@ generateSRPM() {
rlRun "make dist-get-buildreqs > make-buildreqs.log 2>&1"
rlFileSubmit make-buildreqs.log
if grep -q 'Missing dependencies:' make-buildreqs.log; then
rlRun "KERNEL_BUILDREQS=\"$(sed -n 's/Missing dependencies://p' make-buildreqs.log)\""
rlRun 'dnf install -y ${KERNEL_BUILDREQS} > install-buildreqs.log 2>&1'
# Getting the build requirements is quite tricky as it might contain
# not only package names but also "provides" perl(ExtUtils::Embed) which
# can break the dnf command if not escaped properly. The safest way is
# to create an array which contains each req as argument, then pass the
# array to dnf. Bash will later pass each element properly quoted.
# rlRun can also easily break the command due to special characters
# so we don't use it here.
read -ra KERNEL_BUILDREQS <<< "$(sed -n 's/Missing dependencies://p' make-buildreqs.log)"
rlLog "Installing dependencies: ${KERNEL_BUILDREQS[*]}"
dnf install -y "${KERNEL_BUILDREQS[@]}" > install-buildreqs.log 2>&1 || \
rlFail "$(cat install-buildreqs.log)"
rlFileSubmit install-buildreqs.log
elif grep 'PASS:' make-buildreqs.log; then
rlLog "All dependencies were already installed"
@ -139,6 +148,7 @@ rlJournalStart
rlRun "free -h"
logTmtRequiredPackages
declare tmp
rlRun "tmp=\$(mktemp -d)" 0 "Create tmp directory"
rlRun "pushd $tmp"
rlRun "set -o pipefail"

View file

@ -2,23 +2,23 @@
set -exo pipefail
CLANG_PKG=$(rpm -qf --queryformat '%{name}' $(readlink -f $(type -p clang)))
CLANG_PKG=$(rpm -qf --queryformat '%{name}' "$(readlink -f "$(type -p clang)")")
# For compat packages, we want to check if there's a package suffix: clang17 instead clang for example
PKG_SUFFIX=${CLANG_PKG#clang}
CLANG_NVR=$(rpm -q $CLANG_PKG)
CLANG_VERSION=$(rpm --queryformat="%{version}" -q $CLANG_NVR)
CLANG_NVR=$(rpm -q "$CLANG_PKG")
CLANG_VERSION=$(rpm --queryformat="%{version}" -q "$CLANG_NVR")
LIBOMP_DEPENDENCIES="libomp${PKG_SUFFIX} libomp${PKG_SUFFIX}-devel"
# Ensure clang depends on the correct clang-libs version
rpm -q --requires ${CLANG_NVR} | grep "${CLANG_PKG}-libs.* = ${CLANG_VERSION}"
rpm -q --requires "$CLANG_NVR" | grep "${CLANG_PKG}-libs.* = ${CLANG_VERSION}"
# Check that weak dependencies are correct. The versions of these should be the same
# as clang's to guarantee the ABI compatibility, and that version should be actually
# installed as well.
for lomp_dep in $LIBOMP_DEPENDENCIES; do
rpm -q --recommends clang${PKG_SUFFIX}-libs | grep "${lomp_dep}.* = ${CLANG_VERSION}"
[[ "$(rpm --queryformat="%{version}" -q ${lomp_dep}.$(uname -m))" == "${CLANG_VERSION}" ]]
rpm -q --recommends "clang${PKG_SUFFIX}-libs" | grep "${lomp_dep}.* = ${CLANG_VERSION}"
[[ "$(rpm --queryformat="%{version}" -q "${lomp_dep}"."$(uname -m)")" == "${CLANG_VERSION}" ]]
done
# Perform a sanity test to ensure everything works as expected

View file

@ -1,4 +1,4 @@
set -e
#!/bin/bash -e
# Use __LDBL_MANT_DIG__ as a way to distinguish between long double formats.
# While this is not guaranteed to change for all formats, it provides a

View file

@ -1,8 +1,7 @@
set -e
#!/bin/bash -e
fedora_release=`rpm -E %{fedora}`
fedora_release=$(rpm -E "%{fedora}")
mock_root=fedora-$fedora_release-ppc64le
triple=ppc64le-redhat-linux
mock_cmd="mock -r $mock_root --isolation=simple"
@ -12,10 +11,10 @@ run_test () {
echo "Running $test_name"
echo "Expected output: $expected"
actual=$($mock_cmd -q --shell ./$test_name)
actual=$($mock_cmd -q --shell "./$test_name")
echo "Actual output: $actual"
if [[ x$expected == x$actual ]]; then
if [[ "$expected" == "$actual" ]]; then
return 0;
else
return 1;

View file

@ -1,7 +1,4 @@
set -e
triple=ppc64le-redhat-linux
#!/bin/bash -e
run_test () {
test_name=$1
@ -9,10 +6,10 @@ run_test () {
echo "Running $test_name"
echo "Expected output: $expected"
actual=$(./$test_name)
actual=$("./$test_name")
echo "Actual output: $actual"
if [[ x$expected == x$actual ]]; then
if [[ "$expected" == "$actual" ]]; then
return 0;
else
return 1;

View file

@ -1,9 +1,7 @@
#!/bin/sh
set -e
set -x
#!/bin/bash -ex
tmp_cpp=`mktemp -t XXXXX.cpp`
tmp_dir=`mktemp -d`
echo 'int main(int argc, char*argv[]) { while(argc--) new int(); return 0; }' > $tmp_cpp
scan-build -o $tmp_dir clang++ -c $tmp_cpp -o /dev/null
(scan-view --no-browser $tmp_dir/* & WPID=$! && sleep 10s && kill $WPID)
tmp_cpp=$(mktemp -t XXXXX.cpp)
tmp_dir=$(mktemp -d)
echo 'int main(int argc, char*argv[]) { while(argc--) new int(); return 0; }' > "$tmp_cpp"
scan-build -o "$tmp_dir" clang++ -c "$tmp_cpp" -o /dev/null
(scan-view --no-browser "$tmp_dir"/* & WPID=$! && sleep 10s && kill $WPID)

View file

@ -1,11 +1,11 @@
#!/bin/sh -eux
#!/bin/bash -eux
clang_pkg=${1:-"$(rpm -qf --queryformat '%{name}' $(readlink -f $(type -p clang)))"}
clang_pkg=${1:-"$(rpm -qf --queryformat '%{name}' "$(readlink -f "$(type -p clang)")")"}
macros_path="/usr/lib/rpm/macros.d/macros.clang"
set pipefail
if ! rpm -q $clang_pkg > /dev/null; then
if ! rpm -q "$clang_pkg" > /dev/null; then
echo "Could not find package $clang_pkg"
exit 1
fi
@ -23,7 +23,7 @@ echo
# suffix ~rcN. Meanwhile, the macro won't include it in order to allow packages
# built with an RC package to be fully supported later.
# In that case, we need to remove that prefix.
rpm_version=$(rpm -q $clang_pkg --qf "%{version}" | sed 's/~.*//')
rpm_version=$(rpm -q "$clang_pkg" --qf "%{version}" | sed 's/~.*//')
macro_version=$(rpm --eval "%{clang_version}")
if [[ "$rpm_version" != "$macro_version" ]]; then

View file

@ -1,4 +1,4 @@
#!/bin/sh -eux
#!/bin/bash -eu
set pipefail
@ -12,41 +12,40 @@ status=0
test_toolchain() {
toolchain=$@
toolchain=("$@")
args=""
while [ $# -gt 0 ]; do
case $1 in
for arg in "${toolchain[@]}"; do
case "$arg" in
clang)
compiler=$1
compiler=$arg
src=hello.c
;;
clang++)
compiler=$1
compiler=$arg
src=hello.cpp
;;
compiler-rt)
args="$args -rtlib=$1"
args="$args -rtlib=$arg"
;;
libc++)
args="$args -stdlib=$1"
args="$args -stdlib=$arg"
;;
libstdc++)
args="$args -stdlib=$1"
args="$args -stdlib=$arg"
;;
lld)
args="$args -fuse-ld=$1"
args="$args -fuse-ld=$arg"
;;
*)
args="$args $1"
args="$args $arg"
;;
esac
shift
done
cmd="$compiler $args $src"
rm -f a.out
echo "* $toolchain"
echo "* ${toolchain[*]}"
echo " command: $cmd"
if $cmd && ./a.out | grep -q 'Hello World'; then
echo " PASS"
@ -57,17 +56,17 @@ test_toolchain() {
}
clang --version
CLANG_PKG=$(rpm -qf --queryformat '%{name}' $(readlink -f $(type -p clang)))
CLANG_PKG=$(rpm -qf --queryformat '%{name}' "$(readlink -f "$(type -p clang)")")
# Repoquery is needed instead yum info for compatibility with RHEL-7
repoquery -i --installed $CLANG_PKG | grep ^Source
clang_version=$(rpm -q --queryformat "%{version}" $CLANG_PKG | grep -ioP "^[0-9]+")
repoquery -i --installed "$CLANG_PKG" | grep ^Source
clang_version=$(rpm -q --queryformat "%{version}" "$CLANG_PKG" | grep -ioP "^[0-9]+")
echo ""
for compiler in clang clang++; do
for rtlib in "" compiler-rt; do
for linker in "" lld; do
for cxxlib in "" $CXXLIBS; do
if [ "$compiler" = "clang" -a -n "$cxxlib" ]; then
if [[ "$compiler" = "clang" && -n "$cxxlib" ]]; then
continue
fi
for args in "" -static; do
@ -100,7 +99,7 @@ for compiler in clang clang++; do
continue
fi
test_toolchain $compiler $rtlib $linker $cxxlib $args
test_toolchain "$compiler" "$rtlib" "$linker" "$cxxlib" "$args"
done
done
done

View file

@ -1,4 +1,4 @@
set -eux
#!/bin/bash -eux
clang++ --target=x86_64-windows-gnu test.cpp
file a.exe | grep "PE32+ executable (console) x86-64, for MS Windows"

View file

@ -1,4 +1,4 @@
#!/bin/sh -eux
#!/bin/bash -eux
# Determine correct DWARF version to use. Defaults to version 5, but older
# distros might need to use version 4, which can be configured using the
@ -6,9 +6,9 @@
required_dwarf_version=${DWARF_VERSION:-5}
# Get clang version
CLANG_PKG=$(rpm -qf --queryformat '%{name}' $(readlink -f $(type -p clang)))
clang_version=$(rpm -q --queryformat "%{version}" $CLANG_PKG | grep -ioP "^[0-9]+")
if [ $clang_version -lt 18 ]; then
CLANG_PKG=$(rpm -qf --queryformat '%{name}' "$(readlink -f "$(type -p clang)")")
clang_version=$(rpm -q --queryformat "%{version}" "$CLANG_PKG" | grep -ioP "^[0-9]+")
if [ "$clang_version" -lt 18 ]; then
>&2 echo "clang is older than version 18";
required_dwarf_version=4
fi