Commits

Bill Garrison  committed f941677

Added some input checking to -logWithLevel:format:arguments: I have been seeing crashes in SOLogger when invalid input is received.

  • Participants
  • Parent commits 0e237b8

Comments (0)

Files changed (1)

 - (void) logWithLevel:(int)aslLevel format:(NSString *)format arguments:(va_list)arguments
 {
     if (format == nil) return;
-        
+    
     aslclient asl = [self aslclientRef];
     
-    if (asl == NULL)
+    if (asl == NULL) {
+        NSLog (@"%s failed to get ASL client reference for thread %@", __PRETTY_FUNCTION__, [NSThread currentThread]);
+        return;
+    }
+    
+    
+    aslmsg msg = asl_new (ASL_TYPE_MSG);
+    if (msg == NULL) return;
+    
+    asl_set (msg, ASL_KEY_FACILITY, [[self facility] UTF8String]);
+    
+    /* asl_log() does not handle the %@ format specifier, so process the format and arguments into an NSString first. */
+    
+    NSString *text = [[NSString alloc] initWithFormat:format arguments:arguments];
+    if (text == nil) text = @"could not init NSString for log message with given format and arguments";
+    
+    /* Log the text as UTF-8 string */
+    int err = asl_log (asl, msg, aslLevel, "%s", [text UTF8String]);
+    if (err != 0)
     {
-        NSLog (@"%s failed to get ASL client reference for this thread.", __PRETTY_FUNCTION__);
+        NSLog (@"asl_log() failed to write the message: %s", strerror(err));
     }
-    else
-    {
-        aslmsg msg = asl_new (ASL_TYPE_MSG);    
-        asl_set (msg, ASL_KEY_FACILITY, [[self facility] UTF8String]);
-        
-        /* asl_log() does not handle the %@ format specifier, so process the format and arguments into an NSString first. */
-        
-        NSString *text = [[NSString alloc] initWithFormat:format arguments:arguments];
-        
-        /* Log the text as UTF-8 string */
-        int err = asl_log (asl, msg, aslLevel, "%s", [text UTF8String]);
-        if (err != 0)
-        {
-            NSLog (@"asl_log() failed to write the message: %@", text);
-        }
 #if NO_ARC
-        [text release];
+    [text release];
 #endif
-        text = nil;
-        
-        // Cleanup
-        asl_free (msg);
-    }
+    text = nil;
+    
+    // Cleanup
+    asl_free (msg);
+    
 }
 
 #pragma mark -