aboutsummaryrefslogtreecommitdiff
path: root/test/lint/lint-format-strings.sh
diff options
context:
space:
mode:
authorpracticalswift <practicalswift@users.noreply.github.com>2018-06-20 14:27:00 +0200
committerpracticalswift <practicalswift@users.noreply.github.com>2018-08-07 14:07:21 +0200
commita3e455694901a887e0feef69bd63e3aa122ea44b (patch)
treeac3e584c197578054b074124c81dc78b7e42b781 /test/lint/lint-format-strings.sh
parent8c3c402a5a2da9c4c62655b64a9adceced7e9cf4 (diff)
downloadbitcoin-a3e455694901a887e0feef69bd63e3aa122ea44b.tar.xz
build: Add format string linter
This linter checks that the number of arguments passed to each variadic format string function matches the number of format specifiers in the format string.
Diffstat (limited to 'test/lint/lint-format-strings.sh')
-rwxr-xr-xtest/lint/lint-format-strings.sh40
1 files changed, 40 insertions, 0 deletions
diff --git a/test/lint/lint-format-strings.sh b/test/lint/lint-format-strings.sh
new file mode 100755
index 0000000000..c4041d4b3d
--- /dev/null
+++ b/test/lint/lint-format-strings.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2018 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#
+# Lint format strings: This program checks that the number of arguments passed
+# to a variadic format string function matches the number of format specifiers
+# in the format string.
+
+export LC_ALL=C
+
+FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS=(
+ FatalError,0
+ fprintf,1
+ LogConnectFailure,1
+ LogPrint,1
+ LogPrintf,0
+ printf,0
+ snprintf,2
+ sprintf,1
+ strprintf,0
+ vfprintf,1
+ vprintf,1
+ vsnprintf,1
+ vsprintf,1
+)
+
+EXIT_CODE=0
+if ! python3 -m doctest test/lint/lint-format-strings.py; then
+ EXIT_CODE=1
+fi
+for S in "${FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS[@]}"; do
+ IFS="," read -r FUNCTION_NAME SKIP_ARGUMENTS <<< "${S}"
+ mapfile -t MATCHING_FILES < <(git grep --full-name -l "${FUNCTION_NAME}" -- "*.c" "*.cpp" "*.h" | sort | grep -vE "^src/(leveldb|secp256k1|tinyformat|univalue)")
+ if ! test/lint/lint-format-strings.py --skip-arguments "${SKIP_ARGUMENTS}" "${FUNCTION_NAME}" "${MATCHING_FILES[@]}"; then
+ EXIT_CODE=1
+ fi
+done
+exit ${EXIT_CODE}