@@ -65,45 +65,55 @@ CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
65
65
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
66
66
67
67
RETURN=0
68
+
69
+ # Disallow non-ASCII characters in workspace path
70
+ workspace=$( git rev-parse --show-toplevel)
71
+ if echo " $workspace " | grep -q " [一-龥]" ; then
72
+ echo " [!] The workspace path '$workspace ' contains non-ASCII characters." >&2
73
+ exit 1
74
+ fi
75
+
68
76
CLANG_FORMAT=$( which clang-format)
69
77
if [ $? -ne 0 ]; then
70
- echo " [!] clang-format not installed. Unable to check source file format policy." >&2
71
- exit 1
78
+ echo " [!] clang-format not installed. Unable to check source file format policy." >&2
79
+ exit 1
72
80
fi
73
81
74
82
CPPCHECK=$( which cppcheck)
75
83
mkdir -p .out
76
84
if [ $? -ne 0 ]; then
77
- echo " [!] cppcheck not installed. Unable to perform static analysis." >&2
78
- exit 1
85
+ echo " [!] cppcheck not installed. Unable to perform static analysis." >&2
86
+ exit 1
79
87
fi
80
88
81
- # Expected Cppcheck version is 1.90+
82
- # First, Cppcheck 2.x
83
- if [ -z " $( $CPPCHECK --version | grep -E ' ^Cppcheck\s2' ) " ]; then
84
- # Second, Cppcheck 1.x
85
- CPPCHECK_VER=$( $CPPCHECK --version | sed -Ee ' s/Cppcheck 1.([0-9]+)/\1/;q' )
86
- if [ $CPPCHECK_VER -lt 90 ]; then
87
- echo " [!] cppcheck version must be at least 1.90." >&2
88
- echo -e " Check 'Developer Info' for building Cppcheck from source:\n" \
89
- " https://cppcheck.sourceforge.net/devinfo/" >&2
90
- exit 1
91
- fi
89
+ # Check that cppcheck's version is at least 1.90.
90
+ cppcheck_ver=$( " $CPPCHECK " --version)
91
+ if echo " $cppcheck_ver " | grep -qE ' ^Cppcheck\s2' ; then
92
+ : # Version 2.x is acceptable.
93
+ else
94
+ # For version 1.x, extract the minor version and compare.
95
+ minor_version=$( echo " $cppcheck_ver " | sed -Ee ' s/Cppcheck 1\.([0-9]+)/\1/;q' )
96
+ if [ " $minor_version " -lt 90 ]; then
97
+ echo " [!] cppcheck version must be at least 1.90." >&2
98
+ echo -e " See Developer Info for building cppcheck from source:\n"
99
+ echo -e " https://cppcheck.sourceforge.io/devinfo/" >&2
100
+ exit 1
101
+ fi
92
102
fi
93
103
94
104
ASPELL=$( which aspell)
95
105
if [ $? -ne 0 ]; then
96
- echo " [!] aspell not installed. Unable to do spelling check." >&2
97
- exit 1
106
+ echo " [!] aspell not installed. Unable to do spelling check." >&2
107
+ exit 1
98
108
fi
99
109
if [ -z " $( aspell dump dicts | grep -E ' ^en$' ) " ]; then
100
- echo " [!] aspell-en not installed. Unable to do spelling check." >&2
101
- exit 1
110
+ echo " [!] aspell-en not installed. Unable to do spelling check." >&2
111
+ exit 1
102
112
fi
103
113
104
114
DIFF=$( which colordiff)
105
115
if [ $? -ne 0 ]; then
106
- DIFF=diff
116
+ DIFF=diff
107
117
fi
108
118
109
119
if command -v sha1sum > /dev/null 2>&1 ; then
@@ -115,42 +125,63 @@ else
115
125
exit 1
116
126
fi
117
127
128
+ # Get staged filenames (added, copied, or modified) into an array.
129
+ FILES=($( git diff --cached --name-only --diff-filter=ACM) )
130
+ binary_files=()
131
+
132
+ for file in " ${FILES[@]} " ; do
133
+ # Get MIME info for the file.
134
+ mime_info=$( file --mime " $file " )
135
+ # Extract a friendly filename (everything before the colon).
136
+ name=$( file " $file " | cut -d " :" -f1)
137
+
138
+ if echo " $mime_info " | grep -qi binary; then
139
+ binary_files+=(" $name " )
140
+ echo " [!] '$name ' appears to be a binary blob."
141
+ fi
142
+ done
143
+
144
+ if [ " ${# binary_files[@]} " -gt 0 ]; then
145
+ echo " WARNING: Binary data found"
146
+ fi
147
+
118
148
FILES=$( git diff --cached --name-only --diff-filter=ACMR | grep -E " \.(c|cpp|h)$" )
119
149
for FILE in $FILES ; do
120
- nf=$( git checkout-index --temp $FILE | cut -f 1)
121
- tempdir=$( mktemp -d) || exit 1
122
- newfile=$( mktemp ${tempdir} /${nf} .XXXXXX) || exit 1
123
- basename=$( basename $FILE )
124
-
125
- source=" ${tempdir} /${basename} "
126
- mv $nf $source
127
- cp .clang-format $tempdir
128
- $CLANG_FORMAT $source > $newfile 2>> /dev/null
129
- $DIFF -u -p -B --label=" modified $FILE " --label=" expected coding style" \
130
- " ${source} " " ${newfile} "
131
- r=$?
132
- rm -rf " ${tempdir} "
133
- if [ $r != 0 ] ; then
134
- echo " [!] $FILE does not follow the consistent coding style." >&2
135
- RETURN=1
136
- fi
137
- if [ $RETURN -eq 1 ]; then
138
- echo " " >&2
139
- echo " Make sure you indent as the following:" >&2
140
- echo " clang-format -i $FILE " >&2
141
- echo
142
- fi
150
+ nf=$( git checkout-index --temp $FILE | cut -f 1)
151
+ tempdir=$( mktemp -d) || exit 1
152
+ newfile=$( mktemp ${tempdir} /${nf} .XXXXXX) || exit 1
153
+ basename=$( basename $FILE )
154
+
155
+ source=" ${tempdir} /${basename} "
156
+ mv $nf $source
157
+ cp .clang-format $tempdir
158
+ $CLANG_FORMAT $source > $newfile 2>> /dev/null
159
+ $DIFF -u -p -B \
160
+ --label=" modified $FILE " --label=" expected coding style" \
161
+ " ${source} " " ${newfile} "
162
+ r=$?
163
+ rm -rf " ${tempdir} "
164
+ if [ $r != 0 ] ; then
165
+ echo " [!] $FILE does not follow the consistent coding style." >&2
166
+ RETURN=1
167
+ fi
168
+ if [ $RETURN -eq 1 ]; then
169
+ echo " " >&2
170
+ echo " Make sure you indent as the following:" >&2
171
+ echo " clang-format -i $FILE " >&2
172
+ echo
173
+ fi
143
174
done
144
175
145
176
if [ ! -z " ${FILES[*]} " ]; then
146
- echo " Following files need to be cleaned up:"
147
- echo " ${FILES[*]} "
177
+ echo " Following files need to be cleaned up:"
178
+ echo " ${FILES[*]} "
148
179
fi
149
180
150
181
$SHA1SUM -c scripts/checksums 2> /dev/null > /dev/null
151
182
if [ $? -ne 0 ]; then
152
- echo " [!] You are not allowed to change the header file queue.h or list.h" >&2
153
- exit 1
183
+ echo " [!] You are not allowed to change the header file queue.h or list.h" >&2
184
+ exit 1
154
185
fi
155
186
156
187
# Prevent unsafe functions
@@ -159,26 +190,26 @@ banned="([^f]gets\()|(sprintf\()|(strcpy\()"
159
190
status=0
160
191
for file in $( git diff --staged --name-only | grep -E " \.(c|cc|cpp|h|hh|hpp)\$ " )
161
192
do
162
- filepath=" ${root} /${file} "
163
- output=$( grep -nrE " ${banned} " " ${filepath} " )
164
- if [ ! -z " ${output} " ]; then
165
- echo " Dangerous function detected in ${filepath} "
166
- echo " ${output} "
167
- echo
168
- echo " Read 'Common vulnerabilities guide for C programmers' carefully."
169
- echo " https://security.web.cern.ch/security/recommendations/en/codetools/c.shtml"
170
- RETURN=1
171
- fi
193
+ filepath=" ${root} /${file} "
194
+ output=$( grep -nrE " ${banned} " " ${filepath} " )
195
+ if [ ! -z " ${output} " ]; then
196
+ echo " Dangerous function detected in ${filepath} "
197
+ echo " ${output} "
198
+ echo
199
+ echo " Read 'Common vulnerabilities guide for C programmers' carefully."
200
+ echo " https://security.web.cern.ch/security/recommendations/en/codetools/c.shtml"
201
+ RETURN=1
202
+ fi
172
203
done
173
204
174
205
# static analysis
175
206
echo " Running static analysis..."
176
207
$CPPCHECK $CPPCHECK_OPTS > /dev/null
177
208
if [ $? -ne 0 ]; then
178
- RETURN=1
179
- echo " " >&2
180
- echo " Fail to pass static analysis." >&2
181
- echo
209
+ RETURN=1
210
+ echo " " >&2
211
+ echo " Fail to pass static analysis." >&2
212
+ echo
182
213
fi
183
214
184
215
# non-ASCII filenames are not allowed.
187
218
if test $( git diff --cached --name-only --diff-filter=A -z $against |
188
219
LC_ALL=C tr -d ' [ -~]\0' | wc -c) ! = 0
189
220
then
190
- cat << \EOF
221
+ cat << \EOF
191
222
ERROR: Attempt to add a non-ASCII file name.
192
223
This can cause problems if you want to work with people on other platforms.
193
224
To be portable it is advisable to rename the file.
194
225
EOF
195
- RETURN=1
226
+ RETURN=1
196
227
fi
197
228
198
229
exit $RETURN
0 commit comments