Skip to content

Commit e28daf2

Browse files
committed
Merge branch 'jk/banned-function'
It is too easy to misuse system API functions such as strcat(); these selected functions are now forbidden in this codebase and will cause a compilation failure. * jk/banned-function: banned.h: mark strncpy() as banned banned.h: mark sprintf() as banned banned.h: mark strcat() as banned automatically ban strcpy()
2 parents e4095da + e488b7a commit e28daf2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

banned.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef BANNED_H
2+
#define BANNED_H
3+
4+
/*
5+
* This header lists functions that have been banned from our code base,
6+
* because they're too easy to misuse (and even if used correctly,
7+
* complicate audits). Including this header turns them into compile-time
8+
* errors.
9+
*/
10+
11+
#define BANNED(func) sorry_##func##_is_a_banned_function
12+
13+
#undef strcpy
14+
#define strcpy(x,y) BANNED(strcpy)
15+
#undef strcat
16+
#define strcat(x,y) BANNED(strcat)
17+
#undef strncpy
18+
#define strncpy(x,y,n) BANNED(strncpy)
19+
20+
#undef sprintf
21+
#undef vsprintf
22+
#ifdef HAVE_VARIADIC_MACROS
23+
#define sprintf(...) BANNED(sprintf)
24+
#define vsprintf(...) BANNED(vsprintf)
25+
#else
26+
#define sprintf(buf,fmt,arg) BANNED(sprintf)
27+
#define vsprintf(buf,fmt,arg) BANNED(sprintf)
28+
#endif
29+
30+
#endif /* BANNED_H */

git-compat-util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,4 +1239,10 @@ extern void unleak_memory(const void *ptr, size_t len);
12391239
#define UNLEAK(var) do {} while (0)
12401240
#endif
12411241

1242+
/*
1243+
* This include must come after system headers, since it introduces macros that
1244+
* replace system names.
1245+
*/
1246+
#include "banned.h"
1247+
12421248
#endif

0 commit comments

Comments
 (0)