Skip to content

Commit cc8fdae

Browse files
peffgitster
authored andcommitted
banned.h: mark sprintf() as banned
The sprintf() function (and its variadic form vsprintf) make it easy to accidentally introduce a buffer overflow. If you're thinking of using them, you're better off either using a dynamic string (strbuf or xstrfmt), or xsnprintf if you really know that you won't overflow. The last sprintf() call went away quite a while ago in f0766bf (fsck: use for_each_loose_file_in_objdir, 2015-09-24). Note that we respect HAVE_VARIADIC_MACROS here, which some ancient platforms lack. As a fallback, we can just "guess" that the caller will provide 3 arguments. If they do, then the macro will work as usual. If not, then they'll get a slightly less useful error, like: git.c:718:24: error: macro "sprintf" passed 3 arguments, but takes just 2 That's not ideal, but it at least alerts them to the problem area. And anyway, we're primarily targeting people adding new code. Most developers should be on modern enough platforms to see the normal "good" error message. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1b11b64 commit cc8fdae

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

banned.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@
1515
#undef strcat
1616
#define strcat(x,y) BANNED(strcat)
1717

18+
#undef sprintf
19+
#undef vsprintf
20+
#ifdef HAVE_VARIADIC_MACROS
21+
#define sprintf(...) BANNED(sprintf)
22+
#define vsprintf(...) BANNED(vsprintf)
23+
#else
24+
#define sprintf(buf,fmt,arg) BANNED(sprintf)
25+
#define vsprintf(buf,fmt,arg) BANNED(sprintf)
26+
#endif
27+
1828
#endif /* BANNED_H */

0 commit comments

Comments
 (0)