Skip to content

Commit

Permalink
scripts: always use double quotes and curly braces
Browse files Browse the repository at this point in the history
Command line:
shellcheck -s sh -i SC2248,SC2250 -o quote-safe-variables,require-variable-braces $(git ls-files "*.sh")

(There will be two complaints about wanting to double-quote
${CFLAGS_C99}, but it's constructing a command-line, so ignore those
warnings.)

Explanation:
- Prefer double quoting even when variables don't contain special
  characters.
  https://www.shellcheck.net/wiki/SC2248

- Prefer putting braces around variable references even when not
  strictly required.
  https://github.com/koalaman/shellcheck/wiki/SC2250

Reported by:	shellcheck 0.9.0
  • Loading branch information
gperciva committed Jun 30, 2023
1 parent ce82b05 commit 635bf6a
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 108 deletions.
6 changes: 3 additions & 3 deletions POSIX/posix-cflags-filter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ if [ -z "${CC}" ]; then
exit 1
fi
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

# Find directory of this script and the source files
D=$(dirname "$0")

if ! ${CC} -O2 "$D/posix-cflags-filter.c" 2>/dev/null; then
if ${CC} "$D/posix-cflags-filter.c" 2>/dev/null; then
if ! ${CC} -O2 "${D}/posix-cflags-filter.c" 2>/dev/null; then
if ${CC} "${D}/posix-cflags-filter.c" 2>/dev/null; then
echo 'CFLAGS_FILTERED=""'
echo 'for OPT in $CFLAGS; do'
echo ' if [ "$OPT" = "-O2" ]; then'
Expand Down
42 changes: 21 additions & 21 deletions POSIX/posix-cflags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ if [ -z "${CC}" ]; then
exit 1
fi
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

# Find directory of this script and the source files
D=$(dirname "$0")

# Check if we can compile & run a binary.
if ! ${CC} ${CFLAGS} "$D/posix-trivial.c" 2>/dev/null; then
if ! ${CC} ${CFLAGS} "${D}/posix-trivial.c" 2>/dev/null; then
echo "WARNING: failed to compile posix-trivial.c" 1>&2
else
# If the user hasn't disabled runtime checks...
Expand All @@ -31,28 +31,28 @@ else
fi

FIRST=YES
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-msg_nosignal.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-msg_nosignal.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_MSG_NOSIGNAL"
echo "WARNING: POSIX violation: <sys/socket.h> not defining MSG_NOSIGNAL" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-clock_realtime.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-clock_realtime.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_CLOCK_REALTIME"
echo "WARNING: POSIX violation: <time.h> not defining CLOCK_REALTIME" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "$D/posix-inet-addrstrlen.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "${D}/posix-inet-addrstrlen.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_INET_ADDRSTRLEN"
echo "WARNING: POSIX violation: <netinet/in.h> not defining INET_ADDRSTRLEN" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "$D/posix-inet6-addrstrlen.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "${D}/posix-inet6-addrstrlen.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_INET6_ADDRSTRLEN"
echo "WARNING: POSIX violation: <netinet/in.h> not defining INET6_ADDRSTRLEN" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-clock_gettime.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-clock_gettime.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_CLOCK_GETTIME"
echo "WARNING: POSIX violation: <time.h> not declaring clock_gettime()" 1>&2
elif [ "${DISABLE_POSIX_RUNTIME_CHECKS:-0}" -ne "0" ]; then
Expand All @@ -68,30 +68,30 @@ else
# calling process. The "( ./x 2>y ) 2>y" captures both types of error
# message.
if ! ( ./a.out 2>/dev/null ) 2>/dev/null ; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_CLOCK_GETTIME"
echo "WARNING: POSIX violation: clock_gettime() is not linkable" 1>&2
fi
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "$D/posix-stat-st_mtim.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "${D}/posix-stat-st_mtim.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_STAT_ST_MTIM"
echo "WARNING: POSIX violation: struct stat does not contain st_mtim" 1>&2
fi
CFLAGS_C99=""
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-restrict.c" 2>/dev/null; then
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-restrict.c" 2>/dev/null; then
echo "WARNING: POSIX violation: ${CC} does not accept the 'restrict' keyword" 1>&2
if ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -std=c99 "$D/posix-restrict.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -std=c99 "${D}/posix-restrict.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-std=c99"
CFLAGS_C99="-std=c99"
fi
fi
if ! ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DARGNAME="" "$D/posix-abstract-declarator.c" 2>/dev/null; then
if ! ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DARGNAME="" "${D}/posix-abstract-declarator.c" 2>/dev/null; then
echo "WARNING: POSIX violation: ${CC} does not accept qualifiers in an abstract declarator" 1>&2
# Test compile with -DPOSIXFAIL_ABSTRACT_DECLARATOR
if ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DPOSIXFAIL_ABSTRACT_DECLARATOR "$D/posix-abstract-declarator.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DPOSIXFAIL_ABSTRACT_DECLARATOR "${D}/posix-abstract-declarator.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_ABSTRACT_DECLARATOR"
fi
fi
Expand Down
6 changes: 3 additions & 3 deletions POSIX/posix-l.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ -z "${CC}" ]; then
exit 1
fi
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

Expand All @@ -17,8 +17,8 @@ D=$(dirname "$0")

FIRST=YES
for LIB in rt xnet; do
if ${CC} ${CFLAGS} -l${LIB} "$D/posix-trivial.c" 2>/dev/null; then
if [ ${FIRST} = "NO" ]; then
if ${CC} ${CFLAGS} -l"${LIB}" "${D}/posix-trivial.c" 2>/dev/null; then
if [ "${FIRST}" = "NO" ]; then
printf " ";
fi
printf "%s" "-l${LIB}";
Expand Down
10 changes: 5 additions & 5 deletions apisupport/Build/apisupport.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Should be sourced by `command -p sh path/to/apisupport.sh "$PATH"` from
# within a Makefile.
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

Expand Down Expand Up @@ -34,17 +34,17 @@ feature() {

# Check if we can compile this feature (and any required arguments).
printf "Checking if compiler supports %s %s feature..." \
"$PLATFORM" "$FEATURE" 1>&2
"${PLATFORM}" "${FEATURE}" 1>&2
for API_CFLAGS in "$@"; do
if ${CC} ${CPPFLAGS} ${CFLAGS} ${CFLAGS_HARDCODED} \
${API_CFLAGS} "${feature_filename}" ${LDADD_EXTRA} \
${EXTRALIB} 2>>${outcc}; then
${EXTRALIB} 2>>"${outcc}"; then
rm -f a.out
break;
fi
API_CFLAGS=NOTSUPPORTED;
done
case $API_CFLAGS in
case ${API_CFLAGS} in
NOTSUPPORTED)
echo " no" 1>&2
;;
Expand All @@ -53,7 +53,7 @@ feature() {
echo "#define APISUPPORT_${PLATFORM}_${FEATURE} 1"
;;
*)
echo " yes, via $API_CFLAGS" 1>&2
echo " yes, via ${API_CFLAGS}" 1>&2
echo "#define APISUPPORT_${PLATFORM}_${FEATURE} 1"
echo "#ifdef apisupport_dummy"
echo "export CFLAGS_${PLATFORM}_${FEATURE}=\"${API_CFLAGS}\""
Expand Down
10 changes: 5 additions & 5 deletions cpusupport/Build/cpusupport.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Should be sourced by `command -p sh path/to/cpusupport.sh "$PATH"` from
# within a Makefile.
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

Expand Down Expand Up @@ -33,16 +33,16 @@ feature() {

# Check if we can compile this feature (and any required arguments).
printf "Checking if compiler supports %s %s feature..." \
"$ARCH" "$FEATURE" 1>&2
"${ARCH}" "${FEATURE}" 1>&2
for CPU_CFLAGS in "$@"; do
if ${CC} ${CPPFLAGS} ${CFLAGS} ${CFLAGS_HARDCODED} \
${CPU_CFLAGS} "${feature_filename}" 2>>${outcc}; then
${CPU_CFLAGS} "${feature_filename}" 2>>"${outcc}"; then
rm -f a.out
break;
fi
CPU_CFLAGS=NOTSUPPORTED;
done
case $CPU_CFLAGS in
case ${CPU_CFLAGS} in
NOTSUPPORTED)
echo " no" 1>&2
;;
Expand All @@ -51,7 +51,7 @@ feature() {
echo "#define CPUSUPPORT_${ARCH}_${FEATURE} 1"
;;
*)
echo " yes, via $CPU_CFLAGS" 1>&2
echo " yes, via ${CPU_CFLAGS}" 1>&2
echo "#define CPUSUPPORT_${ARCH}_${FEATURE} 1"
echo "#ifdef cpusupport_dummy"
echo "export CFLAGS_${ARCH}_${FEATURE}=\"${CPU_CFLAGS}\""
Expand Down
18 changes: 9 additions & 9 deletions perftests/https/test_https.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pass_test() {
# Initialize to "/" if not specified
path=${2:-"/"}

printf "\n%s\n" "--- Trying to PASS on $hostname $path" >> $output
printf "\n%s\n" "--- Trying to PASS on ${hostname} ${path}" >> "${output}"

./test_https "$hostname" "$path" >> $output
./test_https "${hostname}" "${path}" >> "${output}"
ret=$?

if [ "$ret" -ne "0" ]; then
printf "Failed on %s!\n" "$hostname"
if [ "${ret}" -ne "0" ]; then
printf "Failed on %s!\n" "${hostname}"
failed=$((failed + 1))
fi
}
Expand All @@ -31,13 +31,13 @@ fail_test() {
# Initialize to "/" if not specified
path=${2:-"/"}

printf "%s\n" "--- Trying to FAIL on $hostname $path" >> $output
printf "%s\n" "--- Trying to FAIL on ${hostname} ${path}" >> "${output}"

./test_https "$hostname" "$path" 2>> $output
./test_https "${hostname}" "${path}" 2>> "${output}"
ret=$?

if [ "$ret" -eq "0" ]; then
printf "Failed to fail on %s!\n" "$hostname"
if [ "${ret}" -eq "0" ]; then
printf "Failed to fail on %s!\n" "${hostname}"
failed=$((failed + 1))
fi
}
Expand All @@ -57,6 +57,6 @@ fail_test self-signed.badssl.com
fail_test expired.badssl.com
fail_test untrusted-root.badssl.com

if [ "$failed" -ne 0 ]; then
if [ "${failed}" -ne 0 ]; then
exit 1
fi
14 changes: 7 additions & 7 deletions perftests/network-ssl/test_network_ssl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ fi
pass_test() {
hostname=$1

./test_network_ssl "$hostname"
./test_network_ssl "${hostname}"
ret=$?

if [ "$ret" -ne "0" ]; then
printf "Failed on %s!\n" "$hostname"
if [ "${ret}" -ne "0" ]; then
printf "Failed on %s!\n" "${hostname}"
failed=$((failed + 1))
fi
}

fail_test() {
hostname=$1

./test_network_ssl "$hostname" 2>> $output
./test_network_ssl "${hostname}" 2>> "${output}"
ret=$?

if [ "$ret" -eq "0" ]; then
printf "Failed to fail on %s!\n" "$hostname"
if [ "${ret}" -eq "0" ]; then
printf "Failed to fail on %s!\n" "${hostname}"
failed=$((failed + 1))
fi
}
Expand All @@ -46,6 +46,6 @@ fail_test self-signed.badssl.com
fail_test expired.badssl.com
fail_test untrusted-root.badssl.com

if [ "$failed" -ne 0 ]; then
if [ "${failed}" -ne 0 ]; then
exit 1
fi
Loading

0 comments on commit 635bf6a

Please sign in to comment.