Skip to content

Commit bd9c149

Browse files
committed
Introduce format string checker
Printf-style functions can sometimes have mismatched format strings, leading to unexpected behavior and potential security vulnerabilities. This commit introduces a tool called "fmtscan" that scans C source files, validates format strings, and performs preliminary spell checks. The tool is integrated into the Git pre-commit hook to ensure safer code practices. Change-Id: Ia8ef8d00724a7a141172426eb9f8bf1c9dec2701
1 parent d096595 commit bd9c149

File tree

10 files changed

+1805
-17
lines changed

10 files changed

+1805
-17
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
qtest
2+
fmtscan
23
*.o
34
*.o.d
45
*.dSYM

Diff for: Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CFLAGS += -Wvla
66

77
GIT_HOOKS := .git/hooks/applied
88
DUT_DIR := dudect
9-
all: $(GIT_HOOKS) qtest
9+
all: $(GIT_HOOKS) qtest fmtscan
1010

1111
tid := 0
1212

@@ -53,6 +53,10 @@ qtest: $(OBJS)
5353
$(VECHO) " CC\t$@\n"
5454
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF .$@.d $<
5555

56+
fmtscan: tools/fmtscan.c
57+
$(VECHO) " CC+LD\t$@\n"
58+
$(Q)$(CC) -o $@ $(CFLAGS) $<
59+
5660
check: qtest
5761
./$< -v 3 -f traces/trace-eg.cmd
5862

@@ -76,7 +80,7 @@ valgrind: valgrind_existence
7680
@echo "scripts/driver.py -p $(patched_file) --valgrind -t <tid>"
7781

7882
clean:
79-
rm -f $(OBJS) $(deps) *~ qtest /tmp/qtest.*
83+
rm -f $(OBJS) $(deps) *~ qtest /tmp/qtest.* fmtscan
8084
rm -rf .$(DUT_DIR)
8185
rm -rf *.dSYM
8286
(cd traces; rm -f *~)

Diff for: dudect/fixture.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static bool report(void)
8484
double max_tau = max_t / sqrt(number_traces_max_t);
8585

8686
printf("\033[A\033[2K");
87-
printf("meas: %7.2lf M, ", (number_traces_max_t / 1e6));
87+
printf("measure: %7.2lf M, ", (number_traces_max_t / 1e6));
8888
if (number_traces_max_t < ENOUGH_MEASURE) {
8989
printf("not enough measurements (%.0f still to go).\n",
9090
ENOUGH_MEASURE - number_traces_max_t);

Diff for: linenoise.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ static void refresh_multi_Line(struct line_state *l)
693693
/* Move cursor to right position. */
694694
rpos2 =
695695
(plen + l->pos + l->cols) / l->cols; /* current cursor relative row. */
696-
lndebug("rpos2 %d", rpos2);
696+
lndebug("right position %d", rpos2);
697697

698698
/* Go up till we reach the expected positon. */
699699
if (rows - rpos2 > 0) {

Diff for: qtest.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ static void console_init()
10791079
"Remove from tail of queue. Optionally compare to expected value str",
10801080
"[str]");
10811081
ADD_COMMAND(reverse, "Reverse queue", "");
1082-
ADD_COMMAND(sort, "Sort queue in ascending/descening order", "");
1082+
ADD_COMMAND(sort, "Sort queue in ascending/descending order", "");
10831083
ADD_COMMAND(size, "Compute queue size n times (default: n == 1)", "[n]");
10841084
ADD_COMMAND(show, "Show queue contents", "");
10851085
ADD_COMMAND(dm, "Delete middle node in queue", "");
@@ -1165,11 +1165,11 @@ static bool q_quit(int argc, char *argv[])
11651165

11661166
static void usage(char *cmd)
11671167
{
1168-
printf("Usage: %s [-h] [-f IFILE][-v VLEVEL][-l LFILE]\n", cmd);
1168+
printf("Usage: %s [-h] [-f FILE][-v LEVEL][-l LOG\n", cmd);
11691169
printf("\t-h Print this information\n");
1170-
printf("\t-f IFILE Read commands from IFILE\n");
1171-
printf("\t-v VLEVEL Set verbosity level\n");
1172-
printf("\t-l LFILE Echo results to LFILE\n");
1170+
printf("\t-f FILE Read commands from FILE\n");
1171+
printf("\t-v LEVEL Set verbosity level\n");
1172+
printf("\t-l LOG Echo results to LOG\n");
11731173
exit(0);
11741174
}
11751175

@@ -1208,7 +1208,6 @@ bool commit_exists(const char *commit_hash)
12081208
posix_spawn_file_actions_t actions;
12091209
if (posix_spawn_file_actions_init(&actions) != 0) {
12101210
/* Error initializing spawn file actions */
1211-
perror("posix_spawn_file_actions_init");
12121211
close(pipefd[0]);
12131212
close(pipefd[1]);
12141213
return false;
@@ -1217,7 +1216,6 @@ bool commit_exists(const char *commit_hash)
12171216
/* Redirect child's stdout to the pipe's write end */
12181217
if (posix_spawn_file_actions_adddup2(&actions, pipefd[1], STDOUT_FILENO) !=
12191218
0) {
1220-
perror("posix_spawn_file_actions_adddup2");
12211219
posix_spawn_file_actions_destroy(&actions);
12221220
close(pipefd[0]);
12231221
close(pipefd[1]);
@@ -1227,7 +1225,6 @@ bool commit_exists(const char *commit_hash)
12271225
/* Close unused pipe ends in the child */
12281226
if (posix_spawn_file_actions_addclose(&actions, pipefd[0]) != 0 ||
12291227
posix_spawn_file_actions_addclose(&actions, pipefd[1]) != 0) {
1230-
perror("posix_spawn_file_actions_addclose");
12311228
posix_spawn_file_actions_destroy(&actions);
12321229
close(pipefd[0]);
12331230
close(pipefd[1]);

Diff for: report.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ char *strsave_or_fail(const char *s, const char *fun_name)
255255
check_exceed(len + 1);
256256
char *ss = malloc(len + 1);
257257
if (!ss)
258-
fail_fun("strsave failed in %s", fun_name);
258+
fail_fun("Failed in %s", fun_name);
259259

260260
allocate_cnt++;
261261
allocate_bytes += len + 1;

Diff for: scripts/aspell-pws

+80-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ ioctl
159159
syscall
160160
syscalls
161161
getrandom
162-
dev
163162
urandom
164163
arch
165164
aarch
@@ -309,10 +308,13 @@ lima
309308
github
310309
ih
311310
it
311+
spawnp
312312
AddressSanitizer
313313
asan
314314
dbg
315315
dev
316+
sys
317+
proc
316318
dpkg
317319
apt
318320
siglongjmp
@@ -351,3 +353,80 @@ BitInt
351353
noreturn
352354
pragma
353355
EditorConfig
356+
abcdefghijklmnopqrstuvwxyz
357+
cmd
358+
commitlog
359+
cpp
360+
crit
361+
dereference
362+
dereferenced
363+
dict
364+
errno
365+
fmt
366+
fmtscan
367+
init
368+
lX
369+
ld
370+
lf
371+
llX
372+
lld
373+
llu
374+
llx
375+
lx
376+
malloc
377+
pCn
378+
pCr
379+
pEc
380+
pEh
381+
pEo
382+
pEs
383+
pF
384+
pGg
385+
pGv
386+
pISb
387+
pISc
388+
pISf
389+
pISh
390+
pISl
391+
pISn
392+
pISpc
393+
pK
394+
pMF
395+
pMR
396+
pNF
397+
pSR
398+
pUL
399+
pUl
400+
pV
401+
param
402+
pbl
403+
pf
404+
phC
405+
phN
406+
piSb
407+
piSc
408+
piSf
409+
piSh
410+
piSl
411+
piSn
412+
piSpc
413+
pmR
414+
posix
415+
rdtsc
416+
cntvct
417+
mrs
418+
arg
419+
args
420+
tau
421+
unallocated
422+
unsorted
423+
waitpid
424+
workspace
425+
zd
426+
zu
427+
oneline
428+
str
429+
pws
430+
el
431+
fd
432+
hv

Diff for: scripts/driver.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ def run(self, tid=0):
146146
sys.exit(1)
147147

148148
def usage(name):
149-
print("Usage: %s [-h] [-p PROG] [-t TID] [-v VLEVEL] [--valgrind] [-c]" % name)
149+
print("Usage: %s [-h] [-p PROG] [-t TID] [-v LEVEL] [--valgrind] [-c]" % name)
150150
print(" -h Print this message")
151151
print(" -p PROG Program to test")
152152
print(" -t TID Trace ID to test")
153-
print(" -v VLEVEL Set verbosity level (0-3)")
153+
print(" -v LEVEL Set verbosity level (0-3)")
154154
print(" -c Enable colored text")
155155
sys.exit(0)
156156

Diff for: scripts/pre-commit.hook

+16-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ for FILE in $FILES; do
207207
done
208208

209209
if [ ! -z "${FILES[*]}" ]; then
210-
echo "Following files need to be cleaned up:"
210+
echo "Following files were changed:"
211211
echo "${FILES[*]}"
212212
fi
213213

@@ -234,6 +234,21 @@ do
234234
fi
235235
done
236236

237+
# format string checks
238+
if [ ! -f fmtscan ]; then
239+
make fmtscan
240+
if [ ! -f fmtscan ]; then
241+
throw "Fail to build 'fmtscan' tools"
242+
fi
243+
fi
244+
if git diff --cached --name-only | grep -qiE "\.(c|h|cpp|hpp)$"; then
245+
echo "Running fmtscan..."
246+
./fmtscan
247+
if [ $? -ne 0 ]; then
248+
throw "Check format strings for spelling"
249+
fi
250+
fi
251+
237252
# static analysis
238253
echo "Running static analysis..."
239254
$CPPCHECK $CPPCHECK_OPTS >/dev/null

0 commit comments

Comments
 (0)