[cfe-commits] r56962 - in /cfe/trunk: lib/Sema/Sema.cpp lib/Sema/Sema.h lib/Sema/SemaChecking.cpp test/Sema/format-strings.c

Daniel Dunbar daniel at zuster.org
Thu Oct 2 11:44:07 PDT 2008


Author: ddunbar
Date: Thu Oct  2 13:44:07 2008
New Revision: 56962

URL: http://llvm.org/viewvc/llvm-project?rev=56962&view=rev
Log:
Add support for format string checking of object-size checking
versions of sprintf and friends.
 - Added FIXME that this mechanism should be generalized.

Modified:
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=56962&r1=56961&r2=56962&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Thu Oct  2 13:44:07 2008
@@ -88,17 +88,22 @@
   // do extra checking.  
   IdentifierTable &IT = PP.getIdentifierTable();  
 
-  KnownFunctionIDs[id_printf]    = &IT.get("printf");
-  KnownFunctionIDs[id_fprintf]   = &IT.get("fprintf");
-  KnownFunctionIDs[id_sprintf]   = &IT.get("sprintf");
-  KnownFunctionIDs[id_snprintf]  = &IT.get("snprintf");
-  KnownFunctionIDs[id_asprintf]  = &IT.get("asprintf");
-  KnownFunctionIDs[id_NSLog]     = &IT.get("NSLog");
-  KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
-  KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
-  KnownFunctionIDs[id_vfprintf]  = &IT.get("vfprintf");
-  KnownFunctionIDs[id_vsprintf]  = &IT.get("vsprintf");
-  KnownFunctionIDs[id_vprintf]   = &IT.get("vprintf");
+  KnownFunctionIDs[id_printf]        = &IT.get("printf");
+  KnownFunctionIDs[id_fprintf]       = &IT.get("fprintf");
+  KnownFunctionIDs[id_sprintf]       = &IT.get("sprintf");
+  KnownFunctionIDs[id_sprintf_chk]   = &IT.get("__builtin___sprintf_chk");
+  KnownFunctionIDs[id_snprintf]      = &IT.get("snprintf");
+  KnownFunctionIDs[id_snprintf_chk]  = &IT.get("__builtin___snprintf_chk");
+  KnownFunctionIDs[id_asprintf]      = &IT.get("asprintf");
+  KnownFunctionIDs[id_NSLog]         = &IT.get("NSLog");
+  KnownFunctionIDs[id_vsnprintf]     = &IT.get("vsnprintf");
+  KnownFunctionIDs[id_vasprintf]     = &IT.get("vasprintf");
+  KnownFunctionIDs[id_vfprintf]      = &IT.get("vfprintf");
+  KnownFunctionIDs[id_vsprintf]      = &IT.get("vsprintf");
+  KnownFunctionIDs[id_vsprintf_chk]  = &IT.get("__builtin___vsprintf_chk");
+  KnownFunctionIDs[id_vsnprintf]     = &IT.get("vsnprintf");
+  KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
+  KnownFunctionIDs[id_vprintf]       = &IT.get("vprintf");
 
   SuperID = &IT.get("super");
 

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=56962&r1=56961&r2=56962&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu Oct  2 13:44:07 2008
@@ -127,16 +127,20 @@
 
   // Enum values used by KnownFunctionIDs (see below).
   enum {
-    id_printf,
+    id_NSLog,
+    id_asprintf,
     id_fprintf,
-    id_sprintf,
+    id_printf,
     id_snprintf,
-    id_asprintf,
-    id_NSLog,
-    id_vsnprintf,
+    id_snprintf_chk,
+    id_sprintf,
+    id_sprintf_chk,
     id_vasprintf,
-    id_vfprintf,
+    id_vfprintf,    
+    id_vsnprintf,
+    id_vsnprintf_chk,
     id_vsprintf,
+    id_vsprintf_chk,
     id_vprintf,
     id_num_known_functions
   };

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=56962&r1=56961&r2=56962&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Oct  2 13:44:07 2008
@@ -66,7 +66,11 @@
     if (SemaBuiltinObjectSize(TheCall.get()))
       return true;
   }
-  
+
+  // FIXME: This mechanism should be abstracted to be less fragile and
+  // more efficient. For example, just map function ids to custom
+  // handlers.
+
   // Search the KnownFunctionIDs for the identifier.
   unsigned i = 0, e = id_num_known_functions;
   for (; i != e; ++i) { if (KnownFunctionIDs[i] == FnInfo) break; }
@@ -81,17 +85,21 @@
     
     switch (i) {
     default: assert(false && "No format string argument index.");
-    case id_printf:    format_idx = 0; break;
-    case id_fprintf:   format_idx = 1; break;
-    case id_sprintf:   format_idx = 1; break;
-    case id_snprintf:  format_idx = 2; break;
-    case id_asprintf:  format_idx = 1; break;
-    case id_NSLog:     format_idx = 0; break;
-    case id_vsnprintf: format_idx = 2; HasVAListArg = true; break;
-    case id_vasprintf: format_idx = 1; HasVAListArg = true; break;
-    case id_vfprintf:  format_idx = 1; HasVAListArg = true; break;
-    case id_vsprintf:  format_idx = 1; HasVAListArg = true; break;
-    case id_vprintf:   format_idx = 0; HasVAListArg = true; break;
+    case id_NSLog:         format_idx = 0; break;
+    case id_asprintf:      format_idx = 1; break;
+    case id_fprintf:       format_idx = 1; break;
+    case id_printf:        format_idx = 0; break;
+    case id_snprintf:      format_idx = 2; break;
+    case id_snprintf_chk:  format_idx = 4; break;
+    case id_sprintf:       format_idx = 1; break;
+    case id_sprintf_chk:   format_idx = 3; break;
+    case id_vasprintf:     format_idx = 1; HasVAListArg = true; break;
+    case id_vfprintf:      format_idx = 1; HasVAListArg = true; break;
+    case id_vsnprintf:     format_idx = 2; HasVAListArg = true; break;
+    case id_vsnprintf_chk: format_idx = 4; HasVAListArg = true; break;
+    case id_vsprintf:      format_idx = 1; HasVAListArg = true; break;
+    case id_vsprintf_chk:  format_idx = 3; HasVAListArg = true; break;
+    case id_vprintf:       format_idx = 0; HasVAListArg = true; break;
     }
     
     CheckPrintfArguments(TheCall.get(), HasVAListArg, format_idx);       

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=56962&r1=56961&r2=56962&view=diff

==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Thu Oct  2 13:44:07 2008
@@ -22,9 +22,13 @@
   vasprintf(&b,s,ap); // no-warning
   sprintf(buf,s); // expected-warning {{format string is not a string literal}}
   snprintf(buf,2,s); // expected-warning {{format string is not a string lit}}
+  __builtin___sprintf_chk(buf,0,-1,s); // expected-warning {{format string is not a string literal}}
+  __builtin___snprintf_chk(buf,2,0,-1,s); // expected-warning {{format string is not a string lit}}
   vsprintf(buf,s,ap); // no-warning
   vsnprintf(buf,2,s,ap); // no-warning
   vsnprintf(buf,2,global_fmt,ap); // expected-warning {{format string is not a string literal}}
+  __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // no-warning
+  __builtin___vsnprintf_chk(buf,2,0,-1,global_fmt,ap); // expected-warning {{format string is not a string literal}}
 }
 
 void check_writeback_specifier()





More information about the cfe-commits mailing list