gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

GS log macros


From: David Chisnall
Subject: GS log macros
Date: Mon, 30 Mar 2009 14:32:47 +0100

Continuing my adventures in building GNUstep with clang, I'm finding that the GNUstep log macros all generate warnings like this:

warning: format string is not a string literal (potentially insecure)

Looking at the code, I can see it is correct. There are a few (very unlikely) corner cases where the current code will cause stack corruption.

This patch rewrites the macros to use the static string as the format string, which has the extra advantage that the compiler can now perform type checking of the arguments when using these macros.

David

Index: Foundation/NSDebug.h
===================================================================
--- Foundation/NSDebug.h        (revision 27773)
+++ Foundation/NSDebug.h        (working copy)
@@ -283,8 +283,9 @@
 #define NSDebugFLLog(level, format, args...) \
   do { if (GSDebugSet(level) == YES) { \
     NSString *fmt = GSDebugFunctionMsg( \
-       __PRETTY_FUNCTION__, __FILE__, __LINE__, format); \
-    NSLog(fmt , ## args); }} while (0)
+                       __PRETTY_FUNCTION__, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
+    NSLog(@"%@", fmt); }} while (0)

 /**
* This macro is a shorthand for NSDebugFLLog() using then default debug
@@ -293,8 +294,9 @@
 #define NSDebugFLog(format, args...) \
   do { if (GSDebugSet(@"dflt") == YES) { \
     NSString *fmt = GSDebugFunctionMsg( \
-       __PRETTY_FUNCTION__, __FILE__, __LINE__, format); \
-    NSLog(fmt , ## args); }} while (0)
+                       __PRETTY_FUNCTION__, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
+    NSLog(@"%@", fmt); }} while (0)

 /**
  * This macro is like NSDebugLLog() but includes the name and location
@@ -303,8 +305,9 @@
 #define NSDebugMLLog(level, format, args...) \
   do { if (GSDebugSet(level) == YES) { \
     NSString *fmt = GSDebugMethodMsg( \
-       self, _cmd, __FILE__, __LINE__, format); \
-    NSLog(fmt , ## args); }} while (0)
+               self, _cmd, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
+    NSLog(@"%@", fmt); }} while (0)

 /**
* This macro is a shorthand for NSDebugMLLog() using then default debug
@@ -313,8 +316,9 @@
 #define NSDebugMLog(format, args...) \
   do { if (GSDebugSet(@"dflt") == YES) { \
     NSString *fmt = GSDebugMethodMsg( \
-       self, _cmd, __FILE__, __LINE__, format); \
-    NSLog(fmt , ## args); }} while (0)
+               self, _cmd, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
+    NSLog(@"%@", fmt); }} while (0)

 /**
  * This macro saves the name and location of the function in
@@ -362,9 +366,10 @@
 #define GSOnceFLog(format, args...) \
   do { static BOOL beenHere = NO; if (beenHere == NO) {\
     NSString *fmt = GSDebugFunctionMsg( \
-       __PRETTY_FUNCTION__, __FILE__, __LINE__, format); \
+               __PRETTY_FUNCTION__, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
     beenHere = YES; \
-    NSLog(fmt , ## args); }} while (0)
+    NSLog(@"%@", fmt); }} while (0)
 /**
  * Macro to log a message only the first time it is encountered.<br />
  * Not entirely thread safe ... but that's not really important,
@@ -378,9 +383,10 @@
 #define GSOnceMLog(format, args...) \
   do { static BOOL beenHere = NO; if (beenHere == NO) {\
     NSString *fmt = GSDebugMethodMsg( \
-       self, _cmd, __FILE__, __LINE__, format); \
+               self, _cmd, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
     beenHere = YES; \
-    NSLog(fmt , ## args); }} while (0)
+    NSLog(@"%@", fmt); }} while (0)




@@ -431,8 +437,9 @@
 #define NSWarnFLog(format, args...) \
   do { if (GSDebugSet(@"NoWarn") == NO) { \
     NSString *fmt = GSDebugFunctionMsg( \
-       __PRETTY_FUNCTION__, __FILE__, __LINE__, format); \
-    NSLog(fmt , ## args); }} while (0)
+               __PRETTY_FUNCTION__, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
+    NSLog(@"%@", fmt); }} while (0)

 /**
* This macro is like NSWarnLog() but includes the name and location of the
@@ -441,8 +448,9 @@
 #define NSWarnMLog(format, args...) \
   do { if (GSDebugSet(@"NoWarn") == NO) { \
     NSString *fmt = GSDebugMethodMsg( \
-       self, _cmd, __FILE__, __LINE__, format); \
-    NSLog(fmt , ## args); }} while (0)
+               self, _cmd, __FILE__, __LINE__, \
+                       [NSString stringWithFormat: format, ##args]); \
+    NSLog(@"%@", fmt); }} while (0)
 #else
 #define NSWarnLog(format, args...)
 #define NSWarnFLog(format, args...)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]