Skip to content

Commit 6b6b663

Browse files
authored
CI: Introduce Smatch for static analysis (#125)
Smatch[1][2] is a pluggable static analysis for C. It may help us find out the potential problem of the example code. Doing with smatch, if set the --file-output flag, it will generate the {}.c.smatch report for each c file. This will make a little bit complicated to collect all the report messages. So, here we stay at the default setting, stdout for the smatch messages. For more information, see: - https://lwn.net/Articles/696624/ - https://elinux.org/images/d/d3/Bargmann.pdf Also, fix the warning from Smatch: Smatch failed: 1 warning(s), 1 error(s) lkmpg/examples/procfs2.c:57 procfile_write() error: buffer overflow 'procfs_buffer' 1024 <= 1024 lkmpg/examples/kbleds.c:58 kbleds_init() warn: argument 5 to %lx specifier is cast from pointer Furthermore, the effect of the write operation in procfs2.c is too implied. So after writing, print the buffer every time. Close #122 [1] https://github.com/error27/smatch [2] https://repo.or.cz/w/smatch.git
1 parent 6714971 commit 6b6b663

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

.ci/static-analysis.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ function do_sparse()
4646
make sparse || exit 1
4747
sudo make INST_PROGRAMS=sparse PREFIX=/usr install || exit 1
4848
popd
49+
local SPARSE=$(which sparse)
4950

50-
make -C examples C=2 2> sparse.log
51+
make -C examples C=2 CHECK="$SPARSE" 2> sparse.log
5152

5253
local WARNING_COUNT=$(cat sparse.log | egrep -c " warning:" )
5354
local ERROR_COUNT=$(cat sparse.log | egrep -c " error:" )
@@ -81,7 +82,33 @@ function do_gcc()
8182
make -C examples CONFIG_STATUS_CHECK_GCC=y STATUS_CHECK_GCC=$GCC clean
8283
}
8384

85+
function do_smatch()
86+
{
87+
wget -q https://repo.or.cz/smatch.git/snapshot/refs/heads/master.tar.gz
88+
if [ $? -ne 0 ]; then
89+
echo "Failed to download smatch."
90+
exit 1
91+
fi
92+
tar -xzf master.tar.gz
93+
pushd smatch-master-*
94+
make smatch || exit 1
95+
local SMATCH=$(pwd)/smatch
96+
popd
97+
98+
make -C examples C=2 CHECK="$SMATCH -p=kernel" > smatch.log
99+
local WARNING_COUNT=$(cat smatch.log | egrep -c " warn:" )
100+
local ERROR_COUNT=$(cat smatch.log | egrep -c " error:" )
101+
local COUNT=`expr $WARNING_COUNT + $ERROR_COUNT`
102+
if [ $COUNT -gt 0 ]; then
103+
echo "Smatch failed: $WARNING_COUNT warning(s), $ERROR_COUNT error(s)"
104+
cat smatch.log | grep "warn:\|error:"
105+
exit 1
106+
fi
107+
make -C examples clean
108+
}
109+
84110
do_cppcheck
85111
do_sparse
86112
do_gcc
113+
do_smatch
87114
exit 0

examples/kbleds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ static int __init kbleds_init(void)
5555
for (i = 0; i < MAX_NR_CONSOLES; i++) {
5656
if (!vc_cons[i].d)
5757
break;
58-
pr_info("poet_atkm: console[%i/%i] #%i, tty %lx\n", i, MAX_NR_CONSOLES,
59-
vc_cons[i].d->vc_num, (unsigned long)vc_cons[i].d->port.tty);
58+
pr_info("poet_atkm: console[%i/%i] #%i, tty %p\n", i, MAX_NR_CONSOLES,
59+
vc_cons[i].d->vc_num, (void *)vc_cons[i].d->port.tty);
6060
}
6161
pr_info("kbleds: finished scanning consoles\n");
6262

examples/procfs2.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ static ssize_t procfile_write(struct file *file, const char __user *buff,
5454
if (copy_from_user(procfs_buffer, buff, procfs_buffer_size))
5555
return -EFAULT;
5656

57-
procfs_buffer[procfs_buffer_size] = '\0';
57+
procfs_buffer[procfs_buffer_size & (PROCFS_MAX_SIZE - 1)] = '\0';
58+
pr_info("procfile write %s\n", procfs_buffer);
59+
5860
return procfs_buffer_size;
5961
}
6062

0 commit comments

Comments
 (0)