RemoteLog icon indicating copy to clipboard operation
RemoteLog copied to clipboard

Add format attributes to functions to catch format-string bugs

Open leptos-null opened this issue 1 year ago • 0 comments

Consider the following program

#import <Foundation/Foundation.h>
#import "RemoteLog.h"

int main() {
    RLog(@"xyz %@");
}

In current master, this code compiles without warnings. However, we observe there's a bug here, and NSLog would produce a warning for this code.

With the changes in this PR, clang now produces a diagnostic for this code:

5:14: warning: more '%' conversions than data arguments [-Wformat-insufficient-args]
    5 |         RLog(@"xyz %@");
      |                    ~^

Add we're able to resolve this warning; for example:

    RLog(@"xyz %@", @"sample");

This PR uses the same declarations as NSLog and NSLogv respectively.

FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NS_NO_TAIL_CALL;
FOUNDATION_EXPORT void NSLogv(NSString *format, va_list args) NS_FORMAT_FUNCTION(1,0) NS_NO_TAIL_CALL;

For reference

#define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))

leptos-null avatar Jan 22 '25 05:01 leptos-null