Skip to content

Commit 78d0658

Browse files
L-seriesmkannwischer
authored andcommitted
linting: shell: fix shellcheck warnings
Fix errors brought up by the linter. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
1 parent 942a72d commit 78d0658

8 files changed

Lines changed: 36 additions & 30 deletions

File tree

META.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ META=META.yml
1515
# Manual extraction of metadata with basic cmd line tools
1616
VAL=$(cat $META |
1717
grep "name\|$2" |
18-
grep $1 -A 1 |
19-
grep $2 |
18+
grep "$1" -A 1 |
19+
grep "$2" |
2020
cut -d ":" -f 2 | tr -d ' ')
2121

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

28-
if [[ $VAL_JQ != $VAL ]]; then
28+
if [[ $VAL_JQ != "$VAL" ]]; then
2929
echo "ERROR parsing metadata file $META"
3030
exit 1
3131
fi
@@ -41,5 +41,5 @@ if [[ $INPUT != "" ]]; then
4141
exit 0
4242
fi
4343
else
44-
echo $VAL
44+
echo "$VAL"
4545
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/aarch64/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/aarch64/mlkem/*.S | cut -d '/' -f 5 | sed 's/\.S//'

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

Lines changed: 2 additions & 1 deletion
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?"

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/mlkem/*.S | cut -d '/' -f 5 | sed 's/\.S//'

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

Lines changed: 2 additions & 1 deletion
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?"

scripts/format

Lines changed: 6 additions & 8 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,15 +27,15 @@ 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 CONTRIBUTING.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 CONTRIBUTING.md."
4240
exit 1
4341
fi
@@ -47,21 +45,21 @@ SHELL_SCRIPTS=$(git grep -l '' :/ | xargs printf "'%s' " | xargs -L 1 shfmt -f)
4745
echo $SHELL_SCRIPTS | xargs -L 1 shfmt -s -w -l -i 2 -ci -fn
4846

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

5654
info "Formatting c files"
57-
if ! command -v clang-format 2>&1 >/dev/null; then
55+
if ! command -v clang-format >/dev/null 2>&1; then
5856
error "clang-format not found. Are you running in a nix shell? See CONTRIBUTING.md."
5957
exit 1
6058
fi
6159

6260
nproc=$(getconf _NPROCESSORS_ONLN || echo 1)
6361

64-
git ls-files -- ":/*.c" ":/*.h" | xargs -P $nproc -I {} sh -c '
62+
git ls-files -- ":/*.c" ":/*.h" | xargs -P "$nproc" -I {} sh -c '
6563
# Ignore symlinks
6664
if [[ ! -L {} ]]; then
6765
clang-format -i {}
@@ -70,7 +68,7 @@ git ls-files -- ":/*.c" ":/*.h" | xargs -P $nproc -I {} sh -c '
7068
info "Checking for eol"
7169
check-eol()
7270
{
73-
git ls-files -- ":/" ":/!:*.png" | xargs -P $nproc -I {} sh -c '
71+
git ls-files -- ":/" ":/!:*.png" | xargs -P "$nproc" -I {} sh -c '
7472
# Ignore symlinks
7573
if [[ ! -L {} && $(tail -c1 "{}" | wc -l) == 0 ]]; then
7674
echo "" >>"{}"

scripts/lint

Lines changed: 17 additions & 11 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()
@@ -122,8 +120,7 @@ run-shellcheck()
122120
# Formatting
123121
SUCCESS=true
124122

125-
# Get list of shell scripts and .c/.h files for linting
126-
ALL_C_FILES=$(git ls-files ":/*.c" ":/*.h")
123+
# Get list of shell scripts for linting
127124
SHELL_SCRIPTS=$(git grep -l '' :/ | xargs printf "'%s' " | xargs -L 1 shfmt -f)
128125

129126
gh_group_start "Linting nix files with nixpkgs-fmt"
@@ -151,7 +148,16 @@ fi
151148
gh_group_end
152149

153150
gh_group_start "Linting c files with clang-format"
154-
checkerr "Lint C" "$(clang-format $(git ls-files ":/*.c" ":/*.h") --Werror --dry-run 2>&1 | grep "error:" | cut -d ':' -f 1,2 | tr ':' ' ')"
151+
lint-c-files()
152+
{
153+
for file in $(git ls-files -- ":/*.c" ":/*.h"); do
154+
# Ignore symlinks
155+
if [[ ! -L $file ]]; then
156+
clang-format --Werror --dry-run "$file" 2>&1 | grep "error:" | cut -d ':' -f 1,2 | tr ':' ' '
157+
fi
158+
done
159+
}
160+
checkerr "Lint C" "$(lint-c-files)"
155161
gh_group_end
156162

157163
check-eol-dry-run()
@@ -173,22 +179,22 @@ check-spdx()
173179
local success=true
174180
for file in $(git ls-files -- ":/" ":/!:*.json" ":/!:*.png" ":/!:*.patch" ":/!:*LICENSE*" ":/!:.git*" ":/!:flake.lock"); do
175181
# Ignore symlinks
176-
if [[ ! -L $file && $(grep "SPDX-License-Identifier:" $file | wc -l) == 0 ]]; then
182+
if [[ ! -L $file && $(grep "SPDX-License-Identifier:" "$file" | wc -l) == 0 ]]; then
177183
gh_error "$file" "${line:-1}" "Missing license header error" "$file is missing SPDX License header"
178184
success=false
179185
fi
180186
done
181187
for file in $(git ls-files -- "*.[chsS]" "*.py" "*.mk" "*.yml" "**/Makefile*" ":/!proofs/cbmc/*.py" ":/!examples/bring_your_own_fips202/custom_fips202/tiny_sha3/*" ":/!examples/custom_backend/mlkem_native/src/fips202/native/custom/src/*" ":/!:*.patch"); do
182188
# Ignore symlinks
183-
if [[ ! -L $file && $(grep "Copyright (c) The mlkem-native project authors" $file | wc -l) == 0 ]]; then
189+
if [[ ! -L $file && $(grep "Copyright (c) The mlkem-native project authors" "$file" | wc -l) == 0 ]]; then
184190
gh_error "$file" "${line:-1}" "Missing copyright header error" "$file is missing copyright header"
185191
success=false
186192
fi
187193
done
188194
# For source files in dev/* and mlkem/*, we enforce `Apache-2.0 OR ISC OR MIT`
189195
for file in $(git ls-files -- "*.[chsSi]" | grep "^dev/\|^mlkem/" | grep -v "\.patch$"); do
190196
# Ignore symlinks
191-
if [[ ! -L $file && $(grep "SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT" $file | wc -l) == 0 ]]; then
197+
if [[ ! -L $file && $(grep "SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT" "$file" | wc -l) == 0 ]]; then
192198
gh_error "$file" "${line:-1}" "Missing license header error" "$file is not licensed under 'Apache-2.0 OR ISC OR MIT'"
193199
success=false
194200
fi
@@ -209,7 +215,7 @@ gh_group_end
209215

210216
check-autogenerated-files()
211217
{
212-
if python3 $ROOT/scripts/autogen --dry-run; then
218+
if python3 "$ROOT"/scripts/autogen --dry-run; then
213219
info "Check native auto-generated files"
214220
gh_summary_success "Check native auto-generated files"
215221
else
@@ -225,7 +231,7 @@ gh_group_end
225231

226232
check-magic()
227233
{
228-
if python3 $ROOT/scripts/check-magic >/dev/null; then
234+
if python3 "$ROOT"/scripts/check-magic >/dev/null; then
229235
info "Check magic constants"
230236
gh_summary_success "Check magic constants"
231237
else
@@ -245,7 +251,7 @@ fi
245251

246252
check-contracts()
247253
{
248-
if python3 $ROOT/scripts/check-contracts >/dev/null; then
254+
if python3 "$ROOT"/scripts/check-contracts >/dev/null; then
249255
info "Check contracts"
250256
gh_summary_success "Check contracts"
251257
else

0 commit comments

Comments
 (0)