Skip to content

Add format attributes to functions to catch format-string bugs#3

Open
leptos-null wants to merge 1 commit into
Muirey03:masterfrom
leptos-null:master
Open

Add format attributes to functions to catch format-string bugs#3
leptos-null wants to merge 1 commit into
Muirey03:masterfrom
leptos-null:master

Conversation

@leptos-null
Copy link
Copy Markdown

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)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant