summaryrefslogtreecommitdiffstats
path: root/clang/test/Sema/format-strings.c
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2007-08-14 17:39:48 +0000
committerTed Kremenek <kremenek@apple.com>2007-08-14 17:39:48 +0000
commite68f1aad65f88369b03e6e7fe0c3f57bb70ebb5a (patch)
treeb8a3b32e1fd5ac4b361392bbbb085c7bc3e39219 /clang/test/Sema/format-strings.c
parent367260c0df4dc90e9f3403f67a40c6eb869310ae (diff)
downloadbcm5719-llvm-e68f1aad65f88369b03e6e7fe0c3f57bb70ebb5a.tar.gz
bcm5719-llvm-e68f1aad65f88369b03e6e7fe0c3f57bb70ebb5a.zip
Added support for additional format string checking for the printf
family of functions. Previous functionality only included checking to see if the format string was a string literal. Now we check parse the format string (if it is a literal) and perform the following checks: (1) Warn if: number conversions (e.g. "%d") != number data arguments. (2) Warn about missing format strings (e.g., "printf()"). (3) Warn if the format string is not a string literal. (4) Warn about the use se of '%n' conversion. This conversion is discouraged for security reasons. (5) Warn about malformed conversions. For example '%;', '%v'; these are not valid. (6) Warn about empty format strings; e.g. printf(""). Although these can be optimized away by the compiler, they can be indicative of broken programmer logic. We may need to add additional support to see when such cases occur within macro expansion to avoid false positives. (7) Warn if the string literal is wide; e.g. L"%d". (8) Warn if we detect a '\0' character WITHIN the format string. Test cases are included. llvm-svn: 41076
Diffstat (limited to 'clang/test/Sema/format-strings.c')
-rw-r--r--clang/test/Sema/format-strings.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/clang/test/Sema/format-strings.c b/clang/test/Sema/format-strings.c
index f71cd586458..403da07b0f9 100644
--- a/clang/test/Sema/format-strings.c
+++ b/clang/test/Sema/format-strings.c
@@ -21,3 +21,44 @@ void check_string_literal( FILE* fp, const char* s, char *buf, ... ) {
vsnprintf(buf,2,s,ap); // expected-warning {{mat string is not a string lit}}
}
+void check_writeback_specifier()
+{
+ int x;
+ char *b;
+
+ printf("%n",&x); // expected-warning {{'%n' in format string discouraged}}
+ sprintf(b,"%d%%%n",1, &x); // expected-warning {{'%n' in format string dis}}
+}
+
+void check_invalid_specifier(FILE* fp, char *buf)
+{
+ printf("%s%lb%d","unix",10,20); // expected-warning {{lid conversion '%lb'}}
+ fprintf(fp,"%%%l"); // expected-warning {{lid conversion '%l'}}
+ sprintf(buf,"%%%%%ld%d%d", 1, 2, 3); // no-warning
+ snprintf(buf, 2, "%%%%%ld%;%d", 1, 2, 3); // expected-warning {{sion '%;'}}
+}
+
+void check_null_char_string(char* b)
+{
+ printf("\0this is bogus%d",1); // expected-warning {{string contains '\0'}}
+ snprintf(b,10,"%%%%%d\0%d",1,2); // expected-warning {{string contains '\0'}}
+ printf("%\0d",1); // expected-warning {{string contains '\0'}}
+}
+
+void check_empty_format_string(char* buf)
+{
+ va_list ap;
+ va_start(ap,buf);
+ vprintf("",ap); // expected-warning {{format string is empty}}
+ sprintf(buf,""); // expected-warning {{format string is empty}}
+}
+
+void check_wide_string()
+{
+ char *b;
+ va_list ap;
+ va_start(ap,b);
+
+ printf(L"foo %d",2); // expected-warning {{should not be a wide string}}
+ vasprintf(&b,L"bar %d",2); // expected-warning {{should not be a wide string}}
+}
OpenPOWER on IntegriCloud