Skip to content

Commit ee53f84

Browse files
authored
[runtime] Adopt safer native compiler flags: Wformat-nonliteral (#24090)
Example warning: ```objc nsstring-localization.m:33:46: error: format string is not a string literal [-Werror,-Wformat-nonliteral] 33 | return [NSString localizedStringWithFormat: format, a]; | ^~~~~~ ``` References: * https://developer.apple.com/documentation/xcode/enabling-enhanced-security-for-your-app * https://releases.llvm.org/8.0.0/tools/clang/docs/ReleaseNotes.html#major-new-features (for -ftrivial-auto-var-init=zero) Contributes towards #23023.
1 parent a39e1f0 commit ee53f84

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

mk/rules.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ CFLAGS=\
4343
-Wsemicolon-before-method-body \
4444
-Wsign-compare \
4545
-Wshadow \
46+
-Wformat-nonliteral \
4647
-g \
4748
-I.
4849
SWIFTFLAGS=-g -emit-library

runtime/nsstring-localization.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
#include <pthread.h>
1616

17-
// Silence this warning:
17+
// Silence these warnings:
1818
// nsstring-localization.m:22:46: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
1919
// return [NSString localizedStringWithFormat: format];
2020
#pragma clang diagnostic ignored "-Wformat-security"
21+
// nsstring-localization.m:33:46: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
22+
// 33 | return [NSString localizedStringWithFormat: format, a];
23+
// | ^~~~~~
24+
#pragma clang diagnostic ignored "-Wformat-nonliteral"
2125

2226
extern "C" {
2327

runtime/runtime.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,16 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
13101310
char *formatted = NULL;
13111311

13121312
va_start (args, msg);
1313+
1314+
// Silence this warning:
1315+
// runtime.m:1313:25: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
1316+
// 1313 | vasprintf (&formatted, msg, args);
1317+
// | ^~~~~
1318+
#pragma clang diagnostic push
1319+
#pragma clang diagnostic ignored "-Wformat-nonliteral"
13131320
vasprintf (&formatted, msg, args);
1321+
#pragma clang diagnostic pop
1322+
13141323
va_end (args);
13151324

13161325
return formatted;
@@ -1323,7 +1332,16 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
13231332
char *formatted = NULL;
13241333

13251334
va_start (args, msg);
1335+
1336+
// Silence this warning:
1337+
// runtime.m:1335:25: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
1338+
// 1335 | vasprintf (&formatted, msg, args);
1339+
// | ^~~
1340+
#pragma clang diagnostic push
1341+
#pragma clang diagnostic ignored "-Wformat-nonliteral"
13261342
vasprintf (&formatted, msg, args);
1343+
#pragma clang diagnostic pop
1344+
13271345
if (formatted) {
13281346
PRINT ( PRODUCT ": %s", formatted);
13291347
free (formatted);
@@ -2552,7 +2570,14 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
25522570
void
25532571
xamarin_vprintf (const char *format, va_list args)
25542572
{
2573+
// Silence this warning:
2574+
// runtime.m:2564:56: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
2575+
// 2564 | NSString *message = [[NSString alloc] initWithFormat: [NSString stringWithUTF8String: format] arguments: args];
2576+
// | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2577+
#pragma clang diagnostic push
2578+
#pragma clang diagnostic ignored "-Wformat-nonliteral"
25552579
NSString *message = [[NSString alloc] initWithFormat: [NSString stringWithUTF8String: format] arguments: args];
2580+
#pragma clang diagnostic pop
25562581

25572582
NSLog (@"%@", message);
25582583

0 commit comments

Comments
 (0)