Skip to content

Commit ad1f9e0

Browse files
committed
linting: shell: fix shellcheck warnings
Fix errors brought up by the linter. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
1 parent 86ec9b1 commit ad1f9e0

File tree

6 files changed

+49
-47
lines changed

6 files changed

+49
-47
lines changed

META.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ META=META.yml
1717
# Manual extraction of metadata with basic cmd line tools
1818
VAL=$(cat $META |
1919
grep "name\|$2" |
20-
grep $1 -A 1 |
21-
grep $2 |
20+
grep "$1" -A 1 |
21+
grep "$2" |
2222
cut -d ":" -f 2 | tr -d ' ')
2323

2424
# More robust extraction using yq
25-
if (which yq 2>&1 >/dev/null); then
25+
if (which yq >/dev/null 2>&1); then
2626
QUERY=".implementations | .[] | select(.name==\"$1\") | .\"$2\""
27-
echo "cat $META | yq "$QUERY" -r"
27+
echo "cat $META | yq \"$QUERY\" -r"
2828
VAL_JQ=$(cat $META | yq "$QUERY" -r)
2929

30-
if [[ $VAL_JQ != $VAL ]]; then
30+
if [[ $VAL_JQ != "$VAL" ]]; then
3131
echo "ERROR parsing metadata file $META"
3232
exit 1
3333
fi
@@ -43,5 +43,5 @@ if [[ $INPUT != "" ]]; then
4343
exit 0
4444
fi
4545
else
46-
echo $VAL
46+
echo "$VAL"
4747
fi

proofs/cbmc/list_proofs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
# which are those containing a *harness.c file.
77

88
ROOT=$(git rev-parse --show-toplevel)
9-
cd $ROOT
9+
cd "$ROOT" || exit
1010
ls -1 proofs/cbmc/**/*harness.c | cut -d '/' -f 3

proofs/hol_light/x86_64/list_proofs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
# we have a spec and proof in HOL-Light.
77

88
ROOT=$(git rev-parse --show-toplevel)
9-
cd $ROOT
9+
cd "$ROOT" || exit
1010
ls -1 proofs/hol_light/x86_64/mldsa/*.S | cut -d '/' -f 5 | sed 's/\.S//'

proofs/hol_light/x86_64/proofs/build-proof.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ output_path=$3
3434
output_dir=$(dirname "$output_path")
3535
[ -d "$output_dir" ] || mkdir -p "$output_dir"
3636

37-
export HOLLIGHT_DIR="$(dirname ${hol_sh_cmd})"
37+
HOLLIGHT_DIR="$(dirname "${hol_sh_cmd}")"
38+
export HOLLIGHT_DIR
3839
if [ ! -f "${HOLLIGHT_DIR}/hol_lib.cmxa" ]; then
3940
echo "hol_lib.cmxa does not exist in HOLLIGHT_DIR('${HOLLIGHT_DIR}')."
4041
echo "Did you compile HOL Light with HOLLIGHT_USE_MODULE set to 1?"
@@ -50,20 +51,20 @@ echo "Generating a template .ml that loads the file...: ${template_ml}"
5051
echo "check_axioms ();;"
5152
echo 'let proof_end_time = Unix.time();;'
5253
echo 'Printf.printf "Running time: %f sec, Start unixtime: %f, End unixtime: %f\n" (proof_end_time -. proof_start_time) proof_start_time proof_end_time;;'
53-
) >>${template_ml}
54+
) >>"${template_ml}"
5455

5556
inlined_prefix="$(mktemp)"
5657
inlined_ml="${inlined_prefix}.ml"
5758
inlined_cmx="${inlined_prefix}.cmx"
58-
(cd "${S2N_BIGNUM_DIR}" && HOLLIGHT_LOAD_PATH=${ROOT} ocaml ${HOLLIGHT_DIR}/inline_load.ml "${template_ml}" "${inlined_ml}")
59+
(cd "${S2N_BIGNUM_DIR}" && HOLLIGHT_LOAD_PATH=${ROOT} ocaml "${HOLLIGHT_DIR}"/inline_load.ml "${template_ml}" "${inlined_ml}")
5960

6061
# Give a large stack size.
6162
OCAMLRUNPARAM=l=2000000000 \
6263
ocamlopt.byte -pp "$(${hol_sh_cmd} -pp)" -I "${HOLLIGHT_DIR}" -I +unix -c \
63-
hol_lib.cmxa ${inlined_ml} -o ${inlined_cmx} -w -a
64+
hol_lib.cmxa "${inlined_ml}" -o "${inlined_cmx}" -w -a
6465
ocamlfind ocamlopt -package zarith,unix -linkpkg hol_lib.cmxa \
65-
-I "${HOLLIGHT_DIR}" ${inlined_cmx} \
66+
-I "${HOLLIGHT_DIR}" "${inlined_cmx}" \
6667
-o "${output_path}"
6768

6869
# Remove the intermediate files to save disk space
69-
rm -f ${inlined_cmx} ${template_ml} ${inlined_ml}
70+
rm -f "${inlined_cmx}" "${template_ml}" "${inlined_ml}"

scripts/format

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ ROOT="$(realpath "$(dirname "$0")"/../)"
1313
# Standard color definitions
1414
GREEN="\033[32m"
1515
RED="\033[31m"
16-
BLUE="\033[94m"
17-
BOLD="\033[1m"
1816
NORMAL="\033[0m"
1917

2018
# utility
@@ -29,36 +27,36 @@ error()
2927
}
3028

3129
info "Formatting nix files"
32-
if ! command -v nixpkgs-fmt 2>&1 >/dev/null; then
30+
if ! command -v nixpkgs-fmt >/dev/null 2>&1; then
3331
error "nixpkgs-fmt not found. Are you running in a nix shell? See BUILDING.md."
3432
exit 1
3533
fi
3634

3735
nixpkgs-fmt "$ROOT"
3836

3937
info "Formatting shell scripts"
40-
if ! command -v shfmt 2>&1 >/dev/null; then
38+
if ! command -v shfmt >/dev/null 2>&1; then
4139
error "shfmt not found. Are you running in a nix shell? See BUILDING.md."
4240
exit 1
4341
fi
44-
shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/))
42+
shfmt -s -w -l -i 2 -ci -fn "$(shfmt -f "$(git grep -l '' :/)")"
4543

4644
info "Formatting python scripts"
47-
if ! command -v black 2>&1 >/dev/null; then
45+
if ! command -v black >/dev/null 2>&1; then
4846
error "black not found. Are you running in a nix shell? See BUILDING.md."
4947
exit 1
5048
fi
5149
black --include "(scripts/tests|scripts/simpasm|scripts/cfify|scripts/autogen|scripts/check-namespace|\.py$)" "$ROOT"
5250

5351
info "Formatting c files"
54-
if ! command -v clang-format 2>&1 >/dev/null; then
52+
if ! command -v clang-format >/dev/null 2>&1; then
5553
error "clang-format not found. Are you running in a nix shell? See BUILDING.md."
5654
exit 1
5755
fi
5856

5957
nproc=$(getconf _NPROCESSORS_ONLN || echo 1)
6058

61-
git ls-files -- ":/*.c" ":/*.h" | xargs -P $nproc -I {} sh -c '
59+
git ls-files -- ":/*.c" ":/*.h" | xargs -P "$nproc" -I {} sh -c '
6260
# Ignore symlinks
6361
if [[ ! -L {} ]]; then
6462
clang-format -i {}
@@ -67,7 +65,7 @@ git ls-files -- ":/*.c" ":/*.h" | xargs -P $nproc -I {} sh -c '
6765
info "Checking for eol"
6866
check-eol()
6967
{
70-
git ls-files -- ":/" ":/!:*.png" | xargs -P $nproc -I {} sh -c '
68+
git ls-files -- ":/" ":/!:*.png" | xargs -P "$nproc" -I {} sh -c '
7169
# Ignore symlinks
7270
if [[ ! -L {} && $(tail -c1 "{}" | wc -l) == 0 ]]; then
7371
echo "" >>"{}"

scripts/lint

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ fi
2020
# Standard color definitions
2121
GREEN="\033[32m"
2222
RED="\033[31m"
23-
BLUE="\033[94m"
24-
BOLD="\033[1m"
2523
NORMAL="\033[0m"
2624

2725
info()
@@ -109,7 +107,23 @@ gh_error_simple()
109107
# Identify shell scripts that should be linted with shellcheck.
110108
shellcheck-targets()
111109
{
112-
git ls-files -z -- "*.sh" "scripts/*" "scripts/**/*"
110+
local file
111+
112+
# Emit every tracked file that explicitly ends with .sh.
113+
while IFS= read -r -d '' file; do
114+
printf '%s\0' "$file"
115+
done < <(git ls-files -z -- '*.sh')
116+
117+
# Add extensionless shell scripts that live under scripts/.
118+
while IFS= read -r -d '' file; do
119+
if [[ -L $file || ! -f $file ]]; then
120+
continue
121+
fi
122+
123+
if head -n1 "$file" | grep -qE '^#!.*/(env )?(ba|da|k|mk|z|)sh'; then
124+
printf '%s\0' "$file"
125+
fi
126+
done < <(git ls-files -z -- 'scripts/*' 'scripts/**/*')
113127
}
114128

115129
run-shellcheck()
@@ -125,18 +139,7 @@ run-shellcheck()
125139
local file
126140
local -a files=()
127141
while IFS= read -r -d '' file; do
128-
if [[ -L $file || ! -f $file ]]; then
129-
continue
130-
fi
131-
case "$file" in
132-
*.sh)
133-
files+=("$file")
134-
continue
135-
;;
136-
esac
137-
if head -n1 "$file" | grep -qE '^#!.*/(env )?(ba|da|k|mk|z|)sh'; then
138-
files+=("$file")
139-
fi
142+
files+=("$file")
140143
done < <(shellcheck-targets)
141144

142145
if [[ ${#files[@]} == 0 ]]; then
@@ -146,7 +149,7 @@ run-shellcheck()
146149
fi
147150

148151
local shellcheck_out
149-
if ! shellcheck_out=$(shellcheck --format=gcc "${files[@]}" 2>&1); then
152+
if ! shellcheck_out=$(shellcheck --severity=warning "${files[@]}" 2>&1); then
150153
gh_error_simple "Shellcheck error" "$shellcheck_out"
151154
error "Lint shellcheck"
152155
SUCCESS=false
@@ -165,7 +168,7 @@ checkerr "Lint nix" "$(nixpkgs-fmt --check "$ROOT")"
165168
gh_group_end
166169

167170
gh_group_start "Linting shell scripts with shfmt"
168-
checkerr "Lint shell" "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))"
171+
checkerr "Lint shell" "$(shfmt -s -l -i 2 -ci -fn "$(shfmt -f "$(git grep -l '' :/)")")"
169172
gh_group_end
170173

171174
gh_group_start "Linting shell scripts with shellcheck"
@@ -185,7 +188,7 @@ fi
185188
gh_group_end
186189

187190
gh_group_start "Linting c files with clang-format"
188-
checkerr "Lint C" "$(clang-format $(git ls-files ":/*.c" ":/*.h") --Werror --dry-run 2>&1 | grep "error:" | cut -d ':' -f 1,2 | tr ':' ' ')"
191+
checkerr "Lint C" "$(clang-format "$(git ls-files ":/*.c" ":/*.h")" --Werror --dry-run 2>&1 | grep "error:" | cut -d ':' -f 1,2 | tr ':' ' ')"
189192
gh_group_end
190193

191194
check-eol-dry-run()
@@ -207,22 +210,22 @@ check-spdx()
207210
local success=true
208211
for file in $(git ls-files -- ":/" ":/!:*.json" ":/!:*.png" ":/!:*LICENSE*" ":/!:.git*" ":/!:flake.lock"); do
209212
# Ignore symlinks
210-
if [[ ! -L $file && $(grep "SPDX-License-Identifier:" $file | wc -l) == 0 ]]; then
213+
if [[ ! -L $file && $(grep "SPDX-License-Identifier:" "$file" | wc -l) == 0 ]]; then
211214
gh_error "$file" "${line:-1}" "Missing license header error" "$file is missing SPDX License header"
212215
success=false
213216
fi
214217
done
215218
for file in $(git ls-files -- "*.[chsS]" "*.py" "*.mk" "*.yml" "**/Makefile*" ":/!proofs/cbmc/*.py" ":/!examples/bring_your_own_fips202/custom_fips202/tiny_sha3/*"); do
216219
# Ignore symlinks
217-
if [[ ! -L $file && $(grep "Copyright (c) The mldsa-native project authors" $file | wc -l) == 0 ]]; then
220+
if [[ ! -L $file && $(grep "Copyright (c) The mldsa-native project authors" "$file" | wc -l) == 0 ]]; then
218221
gh_error "$file" "${line:-1}" "Missing copyright header error" "$file is missing copyright header"
219222
success=false
220223
fi
221224
done
222225
# For source files in dev/* and mldsa/*, we enforce `Apache-2.0 OR ISC OR MIT`
223226
for file in $(git ls-files -- "*.[chsSi]" | grep "^dev/\|^mldsa/"); do
224227
# Ignore symlinks
225-
if [[ ! -L $file && $(grep "SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT" $file | wc -l) == 0 ]]; then
228+
if [[ ! -L $file && $(grep "SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT" "$file" | wc -l) == 0 ]]; then
226229
gh_error "$file" "${line:-1}" "Missing license header error" "$file is not licensed under 'Apache-2.0 OR ISC OR MIT'"
227230
success=false
228231
fi
@@ -243,7 +246,7 @@ gh_group_end
243246

244247
check-autogenerated-files()
245248
{
246-
if python3 $ROOT/scripts/autogen --dry-run; then
249+
if python3 "$ROOT"/scripts/autogen --dry-run; then
247250
info "Check native auto-generated files"
248251
gh_summary_success "Check native auto-generated files"
249252
else
@@ -259,7 +262,7 @@ gh_group_end
259262

260263
check-magic()
261264
{
262-
if python3 $ROOT/scripts/check-magic >/dev/null; then
265+
if python3 "$ROOT"/scripts/check-magic >/dev/null; then
263266
info "Check magic constants"
264267
gh_summary_success "Check magic constants"
265268
else
@@ -279,7 +282,7 @@ fi
279282

280283
check-contracts()
281284
{
282-
if python3 $ROOT/scripts/check-contracts >/dev/null; then
285+
if python3 "$ROOT"/scripts/check-contracts >/dev/null; then
283286
info "Check contracts"
284287
gh_summary_success "Check contracts"
285288
else

0 commit comments

Comments
 (0)